2

Hi Guys!

Please take a look at my code, i'm trying to verify that my current webpage will be redirected to external site, in my case Google Play(open Google Play in the same tab). I guess i'm doing it wrong way cuz it pass with FireFox but failing with Chrome, here is my code...

String currentURL1 = driver.getCurrentUrl();

profilePage.clickOnGooglePlayLink();

String currentURL2 = driver.getCurrentUrl();

Thread.sleep(3000);

Assert.assertNotEquals(currentURL1, currentURL2, "Failed Redirected to Google Play");

log.info("Redirected to Google Play!");

I'm trying to assert it as NotEquals, is it any other solutin for this case? Thanks for your time! My code

4
  • Is new page is opening on same tab or diffrent ? Commented Dec 28, 2017 at 18:42
  • You can try to get title of those web page using driver.gettitle() Commented Dec 28, 2017 at 19:04
  • Hi Ankur, web page opening in the same tab. Thanks. Commented Dec 29, 2017 at 18:50
  • You can verifiy title also why are you checking for URL ? Commented Dec 29, 2017 at 19:10

2 Answers 2

2

It is pretty correct when the following line Fails :

Assert.assertNotEquals(currentURL1, currentURL2, "Failed Redirected to Google Play");

Analysis

If you look at the documentation of assertNotEquals, the signature is either of the following :

  • void org.testng.Assert.assertNotEquals(Object actual1, Object actual2)
  • void org.testng.Assert.assertNotEquals(Object actual1, Object actual2, String message)

Here both the assertNotEquals() method takes Object as an argument and performs evaluation.

But in your code, instead of an Object you have passed currentURL1 and currentURL2 and both of them are String as follows :

String currentURL1 = driver.getCurrentUrl();
//
String currentURL2 = driver.getCurrentUrl();
//
Assert.assertNotEquals(currentURL1, currentURL2, "Failed Redirected to Google Play");

So when both the Strings are casted to Objects, the comparison Fails as assertNotEquals() method doesn't takes String as an argument.

Solution

If you want to compare two Strings, a better option would be either of the following :

  • void org.testng.Assert.assertEquals(String actual, String expected)

  • void org.testng.Assert.assertEquals(String actual, String expected, String message)

Sign up to request clarification or add additional context in comments.

Comments

1

Assert.assertEquals(actual, expected, message):- assertEquals assertion is useful to compare two string, boolean, byte[], char, double, float, int, etc.. and based on assertion result.

Assert.assertNotEquals(actual, expected, message) :- assertion in selenium WebDriver is assertNotEquals assertion. It's function is opposite to assertEquals assertion. Means if both sides values will not match then this assertion will pass else it will fail. Here you can write your own message for failure condition.

Please refer this link

Comments

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.