Jagadeesh
Jagadeesh

Reputation: 71

Testing URL Redirect Using Selenium

I want test if a web site URL will redirect to secured site or not. For example, if I type example.com in the address bar, it should redirect to https://example.com.

From Selenium, I tried using both get("") and navigate("") with no luck. It shows an exception as wrong URL. How can I test this or proceed another way?

Even Javascript will not work.

Upvotes: 4

Views: 20677

Answers (1)

Jayesh Doolani
Jayesh Doolani

Reputation: 1233

It's very easy to achieve this using get() & getCurrentUrl(). You should type the actual URL, like www.example.com instead of just using example.com. Even tough you type the URL without the www, the browser makes that change automatically but not Selenium, hence it throws an exception. Try something like this:

driver.get("www.example.com");

//add wait for page to load completely

if(driver.getCurrentUrl().startsWith("https"))
    System.out.println("Success");
else
    System.out.println("Failure");

Upvotes: 5

Related Questions