检查Selenium中的HTTP状态代码

时间:2010-03-01 09:30:13

标签: selenium selenium-rc

如何在Selenium中获取HTTP状态代码?

E.g。所以我可以测试一下,如果浏览器请求/ user / 27并且没有ID = 27的用户,则返回HTTP 404?

我的主要兴趣是Selenium RC,但如果有人知道“正常”硒的答案,我可以很容易地将其翻译成RC。

/皮特

7 个答案:

答案 0 :(得分:10)

对于这种类型的测试,这可能不是Selenium的最佳用途。您可以在不需要加载浏览器的情况下进行更快速的运行测试

[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 Selenium期间重定向到另一个页面,则可以检查最终页面是否符合您的预期。

答案 1 :(得分:6)

我知道这是一个令人震惊的黑客,但这就是我所做的:

    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);
        }
    }

在我需要它的情况下完成工作,虽然我接受它并不理想......

答案 2 :(得分: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;
}

答案 3 :(得分:1)

您可能想要查看captureNetworkTraffic()调用。现在它只适用于Firefox,除非您手动设置IE / Safari / etc以通过端口4444代理流量。

要使用它,只需调用selenium.start(“captureNetworkTraffic = true”),然后在脚本中调用selenium.captureNetworkTraffic(“...”),其中“...”为“plain” ,“xml”或“json”。

答案 4 :(得分:0)

我还没有尝试过,但如果您不介意将自己限制在Firefox,安装Firebug和Netexport,那么Selenium可以访问页面状态代码(以及Firebug的Net面板中的所有其他内容):{{ 3}}

答案 5 :(得分:-2)

如果所有其他方法都失败了,您可以在测试期间调整服务器端代码,以便在页面中输出HTTP状态作为元素:

例如,在我的403 Permission Denied页面上,我有:

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

可以通过WebDriver API轻松检查:

    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

答案 6 :(得分:-2)

试试这个,人们

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.
}