Count number of redirects for a given URL using Selenium Webdriver

5,256 views
Skip to first unread message

JK

unread,
Aug 5, 2013, 8:14:56 PM8/5/13
to webd...@googlegroups.com

Hi Everyone,

I am using Selenium and Webdriver to verify that a webpage or a URL has certain element present for example a drop down box.

However I have seen that on landing on certain page,it gets redirected to another and there is one more redirection after this and after landing on this final page I could then verify the element is present or not.

Now my question is If I open one URL using webdriver,how do I count how many redirects were done before reaching the final URL.

The redirect on URL could be implemented by 301 or 302 response code,by meta refresh or by Javascript Redirects. I could find one code that checks but not sure whether it handles counting of all kind of redirects.

           HttpURLConnection con = (HttpURLConnection)(new URL( myURL  ).openConnection());     
    ((HttpURLConnection) con).setInstanceFollowRedirects( false );
    con.connect();
    int responseCode = ((HttpURLConnection) con).getResponseCode();
    System.out.println("Original Url"+""+ myURL+responseCode);
     int numberHops =0;
    while (responseCode!=200)           

    {
        String newUrl = con.getHeaderField("Location");
        HttpURLConnection conn = (HttpURLConnection) new URL(newUrl).openConnection();
        con.setInstanceFollowRedirects( false );
        responseCode = conn.getResponseCode();          
        //System.out.println(newUrl + responseCode);
        numberHops++;
        System.out.println("location is" + newUrl);
        System.out.println("number of Hoops before Reaching " +conn.getURL()+"is"+numberHops );
        if(numberHops >2)
            break;
    }

Also this is Java code.Is there a way to do this using webdriver code and cover all the three possible way of counting the redirects. If not,then how to count the number of redirects using Java code.

Thanks

David

unread,
Aug 6, 2013, 5:42:30 PM8/6/13
to webd...@googlegroups.com
No, WebDriver doesn't offer this type of testing. Best you could probably do is repeatedly read/get the URL (from the address bar) using WebDriver (perhaps into an array/list) when first visiting the redirecting page until the point where you know it has stopped redirecting (perhaps by checking the final URL or some element on the final page). Then check how many entries of the URL are unique (different URLs not matching each other) in that list. The unique count = approximate # of redirects.

For a real / better solution, you'd want to use other tools or WebDriver combined with other tools (e.g. proxy, Wireshark network capture, Firefox HAR file capture extensions, Java code like above to manually issue HTTP requests on the URL and track redirects through that).

JK

unread,
Aug 6, 2013, 6:25:44 PM8/6/13
to webd...@googlegroups.com
Thanks David,

The above code works fine for the redirected implemented with 301 ro 302 response code.However I am not sure if the same code can work for meta refresh or by Javascript Redirects.
I did not understand how to implement the suggestion you made i.e repeatedly read/get the URL from address bar.
Do you mean to do a driver.getCurrentURL() and do that till we get a response code of 200 ?.

Thanks again.

David

unread,
Aug 7, 2013, 1:58:56 PM8/7/13
to webd...@googlegroups.com
For the Selenium only solution I mention, it should work for all types of redirects, but whether it works well or not is the question. And yes, I did mean to use driver.getCurrentUrl() but you don't keep doing it until get response code 200, because Selenium doesn't check response codes (though that would be the correct check if monitoring HTTP responses only). So you keep doing it until you land on the right page you are expecting (by checking current URL = expected URL or that some expected element on the page exists). If you don't know what the final page to expect is then you have a problem.

For the Selenium + other tools option, to handle all types of redirects, your best option may be to use the proxy, or network sniffer, or (Firefox browser extension) HAR file capture method. The sample code you posted (using Java HTTP library) only follows server redirects, and not the client side ones like meta refresh & javascript since it's not a browser/HTML parser.
Reply all
Reply to author
Forward
0 new messages