2009-09-21 116 views
16

使用Selenium WebDriver檢查URL GET是否成功返回(HTTP 200)最方便的方法是什麼?如何使用Selenium WebDriver檢查404的URL?

在這種特殊情況下,我最感興趣的是驗證當前頁面沒有任何圖像被破壞。

+0

不應該這個問題的標題真的是「如何檢查URL是否與Selenium WebDriver一起在網頁上加載所有圖像?」?被接受的答案與HTTP響應代碼無關,這就排除了類似的問題。 – 0112 2017-03-27 17:01:32

+0

類似的問題(用於檢查響應代碼)在這裏被詢問/回答:http://stackoverflow.com/a/14547168/1596460 – 0112 2017-03-27 17:06:03

回答

10

試試這個:

List<WebElement> allImages = driver.findElements(By.tagName("img")); 
for (WebElement image : allImages) { 
    boolean loaded = ((JavaScriptExecutor) driver).executeScript(
     "return arguments[0].complete", image); 
    if (!loaded) { 
    // Your error handling here. 
    } 
} 
+3

我還需要使用Dave Hunt建議的naturalWidth檢查,所以加載的表達式現在變成: ()返回參數[0] .complete && typeof arguments [0] .naturalWidth!= \「undefined \」&& arguments [0] .naturalWidth> 0「,image); – paulcm 2010-08-10 17:09:20

5

您可以使用getEval命令驗證頁面上每個圖像的以下JavaScript返回的值。

@Test 
public void checkForBrokenImages() { 
    selenium.open("http://www.example.com/"); 
    int imageCount = selenium.getXpathCount("//img").intValue(); 
    for (int i = 0; i < imageCount; i++) { 
     String currentImage = "this.browserbot.getUserWindow().document.images[" + i + "]"; 
     assertEquals(selenium.getEval("(!" + currentImage + ".complete) ? false : !(typeof " + currentImage + ".naturalWidth != \"undefined\" && " + currentImage + ".naturalWidth == 0);"), "true", "Broken image: " + selenium.getEval(currentImage + ".src")); 
    } 
} 

更新時間:

新增TestNG的測試/ Java的例子。

2

我不認爲第一反應會工作。當我src一個錯誤的圖像,它會拋出一個404錯誤的預期。但是,當我在firebug中檢查javascript時,該(已損壞)圖像已被設置爲true。所以,這是一個完整的404,但仍然是一個破碎的圖像。

第二個響應似乎更準確,因爲它檢查它是否完整,然後檢查圖像是否有一些寬度。

我做了一個Python版本的第二個響應,適用於我。可以清理一下,但希望它會有所幫助。

def checkForBrokenImages(self): 
    sel = self.selenium 
    imgCount = int(sel.get_xpath_count("//img")) 
    for i in range(0,imgCount): 
     isComplete = sel.get_eval("selenium.browserbot.getCurrentWindow().document.images[" + str(i) + "].complete") 
     self.assertTrue(isComplete, "Bad Img (!complete): "+sel.get_eval("selenium.browserbot.getCurrentWindow().document.images[" + str(i) + "].src")) 
     typeOf = sel.get_eval("typeof selenium.browserbot.getCurrentWindow().document.images[" + str(i) + "].naturalWidth") 
     self.assertTrue(typeOf != 'undefined', "Bad Img (w=undef): "+sel.get_eval("selenium.browserbot.getCurrentWindow().document.images[" + str(i) + "].src")) 
     natWidth = int(sel.get_eval("selenium.browserbot.getCurrentWindow().document.images[" + str(i) + "].naturalWidth")) 
     self.assertTrue(natWidth > 0, "Bad Img (w=0): "+sel.get_eval("selenium.browserbot.getCurrentWindow().document.images[" + str(i) + "].src")) 
0

其中一種替代解決方案是在測試執行後分析Web服務器日誌。這種方法不僅可以捕捉錯過的圖像,還可以捕捉到CSS,腳本和其他資源。

如何做到這一點的說明是here

0

而不是在Java中遍歷,可能會更快地爲所有圖像調用JavaScript一次。

boolean allImgLoaded = (Boolean)((JavascriptExecutor) driver).executeScript(
    "return Array.prototype.slice.call(document.images).every(" 
    + "function (img) {return img.complete && img.naturalWidth > 0;});"); 
-1

豐達用於檢查404:

基本上404可以通過URL的HTTP響應進行檢查。

步驟1:爲HTTPTestAPI導入庫
步驟2:創建HTTPRequest。

String URL="www.abc.com"; 
HTTPRequest request = new HTTPRequest(URL); 

//Get the response code of the URL 
int response_code = request.getResponseCode(); 

//Check for 404: 
if(response_code == 404) 
    FAIL -- THE URL is leading to 404. 
else 
    PASS 
+0

此答案不解決Selenium,而是使用單獨的HTTP連接來測試 – 2016-12-27 12:52:18

相關問題