如何检查webdriver中的404页面崩溃?

时间:2017-08-09 10:17:58

标签: java selenium selenium-webdriver

我必须使用webdriver编写一个用于测试404页崩溃的脚本。我希望webdriver访问网站的每个链接,如果遇到404错误,请截取屏幕截图。

它应该在循环中工作。

1 个答案:

答案 0 :(得分:0)

添加此签入脚本并传递您期望在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();
           }
   }