4

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.

3
  • Just do driver.get("www.example.com") and then use driver.getCurrentUrl() to check if it has moved to secure site... If you want to do this faster you can look at a plain java unit test using the URL and HttpUrlConnection classes - mkyong.com/java/java-httpurlconnection-follow-redirect-example Commented Apr 19, 2017 at 16:53
  • Please read How to Ask and How much research effort is expected? Please provide the code you have tried and the execution result including any error messages, etc. Commented Apr 19, 2017 at 18:35
  • corrected a typo. Commented Apr 21, 2017 at 17:15

1 Answer 1

5

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");
Sign up to request clarification or add additional context in comments.

5 Comments

But point here is i'm not suppose to pass www. . My input will be example.com . And test redirected URL contains https
Well you can't do it that way. Selenium doesn't recognize example.com as a valid URL. It needs a fully valid URL which either starts with www or http(s)
@JayeshDoolani example.com is a valid URL. My website, for instance, does not work if you go to the www subdomain.
No www subdomain means that you'd still have to specify the protocol however. So example.com. you can test the https protocol was redirected
getCurrentUrl seems to be depricated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.