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