user755806
user755806

Reputation: 6825

Selenium find page is loaded or not?

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!

Upvotes: 1

Views: 6479

Answers (3)

Zach
Zach

Reputation: 1006

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

Upvotes: 0

Jeremiah
Jeremiah

Reputation: 1145

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.

Upvotes: 0

Simon
Simon

Reputation: 31

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

Upvotes: 2

Related Questions