2017-08-09 79 views
-1

我必须编写一个脚本来测试404页面崩溃使用webdriver.I想webdriver去到每个网站的链接,如果遇到404错误,然后截图相同。如何检查webdriver中的404页面崩溃?

它应该在循环中工作。

+0

为了达到这个目的,你需要编写一些代码。你能分享你的工作吗? – DebanjanB

+1

使用Selenium没有意义。抓取网站并检查响应代码。 404页面大概总是看起来一样,所以谁在乎截图? – Michael

+2

与@Michael类似的想法得到一个404页面的屏幕截图将是没有价值的_所有看起来same_。只需抓取网站并检查响应代码即可。 – DebanjanB

回答

0

在脚本中添加这样的检查,通过元素定位器(IE),你的期待在404页

if(driver.findElements(By.xpath("value")).size() != 0) { 
      customScreenshot(driver); 
     } 

注: - 必须更换的XPath按你的元素在上面的代码

在你的课堂上添加此功能: -

public static void customScreenshot(WebDriver driver){ 
     try { 
      File screenShot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
      // extracting date for folder name. 
      SimpleDateFormat dateFormatForFoldername = new SimpleDateFormat("yyyy-MM-dd");//dd/MM/yyyy 
      Date currentDate = new Date(); 
      String folderDateFormat = dateFormatForFoldername.format(currentDate); 
      // extracting date and time for snapshot file 
      SimpleDateFormat dateFormatForFileName = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//dd/MM/yyyy 
      String fileDateFormet = dateFormatForFileName.format(currentDate); 
      String filefolder="./test-output"+"/Snap/"+folderDateFormat+"/"; 
      // Creating folders and files 
      File screenshot = new File(filefolder+fileDateFormet+".jpeg"); 
      FileUtils.copyFile(screenShot, new File(screenshot.getPath())); 
      } 
      catch (Exception ex) { 
       System.err.println("Unable to capture screenshot..."); 
       ex.printStackTrace(); 
      } 
    }