1

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
Kiechlus
  • 1,167
  • 12
  • 21
  • Better way of always asserting is to check if certain control exists on the page after launching web app or check for title of the page. Selenium does not returns any status code. [There are hacks to get it though. but never recommended] – Mrunal Gosar Aug 23 '16 at 13:29
  • Hm, I need to assert for some purpose that a page returns a 404 Not Found. There are no reliable elements on that page I could check with... – Kiechlus Aug 23 '16 at 13:46

1 Answers1

2

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

Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71