1

I am using Selenium web driver. I have below method to navigate to page.

public String navigate(String url){
        driver = new FirefoxDriver();
        driver.get(url);
        return "Success";
    }

Above code works fine if the server is up. some times server might be down then the page will not be loaded. Now how can I return "failure" string if the page is not loaded?

Thanks!

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
user755806
  • 6,565
  • 27
  • 106
  • 153

3 Answers3

2

You can't directly test that a get() failed because the navigator always displays a page. You can either check that this page is a known error page, or check that you are not in the expected page.

First solution

It depends on the navigator. Chrome displays a special page when it can't find an url, firefox another page, etc.. You can test the title of those pages. For example firefox error page title is something like "Page load error" or "Problem loading page". Then all you have to do is something like :

if(driver.getTitle().equals("Problem loading page"))
   return "failure";

Second solution

You must check the non-existence of an element that is present in every pages of your website (for example a logo or a home button). Say the ID of this element is "foo", you can do something like :

if(driver().findElements(By.id("foo")).isEmpty())
   return "failure";
Simon
  • 31
  • 2
  • The solution 1) only covers one situation... the problem is that a failed page may display anything such as "not found", "server not responding", "forbidden", and so on as each site implements its own error handler. There's no easy solution to cover for errors but it's a starting point. The solution 2) only works for sure if you are the owner of that page. we all know how it goes: everyone wants to keep changing things... I'm dealing with the two problems and could not come up with anything better than lists of possible errors I can imagine and possible/expected things to test for success. – AlexD Nov 30 '17 at 19:45
0

If using the Page Object Model, leveraging the LoadableComponentClass can help in determining whether the page is loaded or not either as a result of server down or something else. Here's the link

https://code.google.com/p/selenium/wiki/LoadableComponent

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Zach
  • 986
  • 7
  • 19
0

Dave Haeffner has a good solution for checking status codes using a proxy with the webdriver configuration.

http://elementalselenium.com/tips/17-retrieve-http-status-codes

The examples are in python, but the API is pretty close between python and java. I've not had much difficulty finding the java-analagous methods from the tips I've implemented myself.

That site has a lot of good information.

Jeremiah
  • 1,145
  • 6
  • 8