2016-08-23 46 views
1

如何在使用intern转到某个页面后确定HTTP状态?实习生/ Leadfoot:如何获取HTTP状态代码

this.remote.get('http://google.de') // status 200 
this.remote.get('http://google.de/alsdflasdf') // 404 Not Found 
+0

更好始终断言的方式是在启动Web应用程序或检查页面标题后检查页面上是否存在某些控件。 Selenium不返回任何状态码。 [虽然有黑客得到它。但从来没有推荐] –

+0

嗯,我需要为某个目的声明一个页面返回404 Not Found。该页面上没有可以检查的可靠元素... – Kiechlus

回答

2

因此,这里的hack..Use BrowserMob代理,并通过BrowserMob代理和代理这些发送您的请求将会给你所有你希望有相关的状态代码。
这里是一个代码示例使用browsermob代理最后响应代码保存到 变量:

int lastResponseCode; 

    ProxyServer server = new ProxyServer(8888); 
    server.start(); 

    server.addResponseInterceptor(new HttpResponseInterceptor() { 
     @Override 
     public void process(HttpResponse response, HttpContext context) throws HttpException, 
IOException { 
     lastResponseCode = response.getStatusLine().getStatusCode(); 
     } 
    }); 

    DesiredCapabilities caps = new DesiredCapabilities(); 
    caps.setCapability(CapabilityType.PROXY, server.seleniumProxy()); 
    WebDriver driver = new FirefoxDriver(caps); 

为什么状态代码永远不会在硒的webdriver支持的原因在这里:https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/141