Search code examples
seleniuminternleadfoot

Intern / Leadfoot: How to get HTTP status code


How can I determine the HTTP status after moving to some page using intern?

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

Solution

  • So here's the hack..Use BrowserMob proxy and send your requests via BrowserMob Proxy and these proxy will give you all the relevant status codes you wish to have.
    Here is a sample of code that uses browsermob proxy to save last response code to a variable:

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

    And the reason why status codes will never be supported in selenium Webdriver is here: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/141