2017-08-09 78 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(); 
      } 
    }