2016-08-23 89 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