2010-03-01 146 views
38

如何獲取Selenium中的HTTP狀態碼?檢查Selenium中的HTTP狀態碼

E.g.所以我可以測試,如果瀏覽器請求/ user/27並且沒有ID = 27的用戶存在,HTTP 404將被返回?

我的主要興趣是Selenium RC,但如果有人知道「正常」硒的答案,我可以很容易地將它翻譯成RC。

/皮特

+0

的可能的複製[檢查的HttpResponse OK(200)與硒的webdriver(http://stackoverflow.com/questions/14537336/checking-httpresponse-ok-200-with-selenium-webdriver) – Kzqai

+2

這個問題並不完全相同,因爲當我在6年前問這個問題時,我在談論Selenium RC,它早於WebDriver。但是這也使得這個問題變得毫無意義,因爲我不認爲人們會再使用舊的API。 – Pete

回答

10

這可能不是硒的這種類型的測試的最佳利用。有必要需要載入瀏覽器時,你可以這樣做,有一個更快的運行測試

[Test] 
[ExpectedException(typeof(WebException), UserMessage = "The remote server returned an error: (404) Not Found")] 
public void ShouldThrowA404() 
{ 
    HttpWebRequest task; //For Calling the page 
    HttpWebResponse taskresponse = null; //Response returned 
    task = (HttpWebRequest)WebRequest.Create("http://foo.bar/thiswontexistevenifiwishedonedayitwould.html"); 
    taskresponse = (HttpWebResponse)task.GetResponse(); 
} 

如果測試404硒期間重定向到另一個頁面可以查看在最後一頁有你所期望的。

1

您可能想要查看captureNetworkTraffic()調用。現在它只能與Firefox工作可靠,除非你手動設置IE/Safari瀏覽器/等,以通過端口代理業務4444

要在以後使用它,只需調用selenium.start(「captureNetworkTraffic =真」),然後在您的腳本中,您可以調用selenium.captureNetworkTraffic(「...」),其中「...」是「普通」,「xml」或「json」。

5

由於Selenium 2包含HtmlUnit,因此您可以利用它來直接訪問響應。

public static int getStatusCode(long appUserId) throws IOException { 
    WebClient webClient = new WebClient(); 
    int code = webClient.getPage(
      "http://your.url/123/" 
    ).getWebResponse().getStatusCode(); 
    webClient.closeAllWindows(); 
    return code; 
} 
+5

不幸的是,它不適用於Selenium的C#版本。 – Pete

5

我知道這是一個令人震驚的黑客攻擊,但是這是我做了什麼:

protected void AssertNotYellowScreen() 
    { 
     var selenium = Selenium; 

     if (selenium.GetBodyText().Contains("Server Error in '/' Application.")) 
     { 
      string errorTitle = selenium.GetTitle(); 

      Assert.Fail("Yellow Screen of Death: {0}", errorTitle); 
     } 
    } 

它得到我需要它的情況下所做的工作,雖然我接受它的效果並不理想...

+0

我甚至只是在正文中尋找單詞「錯誤」。不理想,但它完成了工作。 –

+0

是的,我有這個想法,並且正在尋找更好的東西,但是這看起來像我現在必須去的方式。 –

-2

如果一切都失敗了,你可以在測試過程中在頁面元素適應你的服務器端代碼,輸出HTTP狀態:

例如,我的403權限被拒絕頁面上,我有:

<h1 id="web_403">403 Access Denied</h1> 

可以通過API的webdriver容易確認:

public boolean is403(WebDriver driver) { 
     try { 
      driver.findElement(By.id("web_403")); 
      return true; 
     } catch (NoSuchElementException e) { 
      return false; 
     } 
    } 

http://www.ninthavenue.com.au/how-to-get-the-http-status-code-in-selenium-webdriver

-2

嘗試th是,人們

WebClient wc = new WebClient(); 
int countRepeats = 120; // one wait = 0.5 sec, total 1 minute after this code 
boolean haveResult = false; 
try { 
    HtmlPage pageHndl = wc.getPage(Urls); 
    for(int iter=0; iter<countRepeats; iter++){ 
     int pageCode = pageHndl.getWebResponse().getStatusCode(); 
     System.out.println("Page status "+pageCode); 
     if(pageCode == 200){ 
      haveResult = true; 
      break; 
     } 
     else{ 
      Thread.sleep(500); 
     } 
    } 
} catch (IOException e) { 
     e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
} catch (InterruptedException e) { 
     e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
}