Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert - 2 Exact Same String Comparing Returns Error

I am comparing two strings using this code:

    Assert.assertTrue(driver.findElement(By.cssSelector(cssSelector)).getText().equals(text));

Both String outputs are exactly the same in the output:

    Merci ! Votre commande a été envoyée.
    Merci ! Votre commande a été envoyée.

Tests does run many times, and it always worked until this one situation. There are no spaces inside the strings. Special characters does not cause problems in any other cases.

Any Thoughts? Thank you!

UPDATE:

I am getting the same error when comparing these strings, maybe have something in common that breaks the test:

Merci ! Votre commande a été envoyée.
Merci ! Votre commande a été envoyée.

Référence de la commande : {0}
Référence de la commande : {0}

Contactez-moi !
Contactez-moi !

Quelle est la probabilité que vous recommandiez nous à un ami ou un collègue ?
Quelle est la probabilité que vous recommandiez nous à un ami ou un collègue ?

I noticed there is a space before symbol in each case (_! / _? / _:). This (the space before symbol) never occurs in other passing assertions. I am testing like 500 of them. May be the cause?

like image 505
fabik Avatar asked Nov 24 '17 10:11

fabik


2 Answers

My guess is, that whitespace around the text are trimmed or added at getText.

Assert.assertTrue(driver.findElement(By.cssSelector(cssSelector)).getText().equals(text));

Probably disregard this difference:

Assert.assertTrue(driver.findElement(By.cssSelector(cssSelector)).getText().trim()
    .equals(text.trim()));

An other property of Unicode is that é can be either one char or two (e + accent). And java internally uses Unicode for text. Actually one should normalize text before comparing.

s = Normalizer.normalize(s, Normalizer.Form.NFKC); // Most compact

By the way instead of assertTrue the method assertEquals with expected and actual values will give better error messages.

like image 177
Joop Eggen Avatar answered Oct 21 '22 04:10

Joop Eggen


Let us see whats happening in your code. You have used :

Assert.assertTrue(driver.findElement(By.cssSelector(cssSelector)).getText().equals(text));

Within Assert.assertTrue you are comparing 2 values. The first value is driver.findElement(By.cssSelector(cssSelector)).getText() which can return text (w/out white space characters) or NULL. The second value is a pure text.

Now, if you look at the javadoc of equals it is defined as :

boolean java.lang.String.equals(Object anObject)
//Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

This is the root case of your failure. This will return True only if the given object represents a String equivalent to this string

Again, if you look at the javadoc of assertTrue it is defined as :

void org.testng.Assert.assertTrue(boolean condition)
//Asserts that a condition is true. If it isn't, an AssertionError is thrown.

Solution:

The solution would be to use either contains, equalsIgnoreCase or contentEquals instead of equals as follows :

Assert.assertTrue(driver.findElement(By.cssSelector(cssSelector)).getText().contains(text));
//OR
Assert.assertTrue(driver.findElement(By.cssSelector(cssSelector)).getText().equalsIgnoreCase(text));
//OR
Assert.assertTrue(driver.findElement(By.cssSelector(cssSelector)).getText().contentEquals(text));

Update 1:

I will suggest use getAttribute("innerHTML") instead of getText() as follows :

Assert.assertTrue(driver.findElement(By.cssSelector(cssSelector)).getAttribute("innerHTML").contains(text));
//OR
Assert.assertTrue(driver.findElement(By.cssSelector(cssSelector)).getAttribute("innerHTML").equalsIgnoreCase(text));
//OR
Assert.assertTrue(driver.findElement(By.cssSelector(cssSelector)).getAttribute("innerHTML").contentEquals(text));

Update 2:

As your test is still failing, I would suggest you to reduce the expected string to something substantial instead of the entire string. Example:

String text = "Votre commande a été";

Update 3:

As a last resort, you should be able to expand the expected string as follows :

String text = "Merci ! Votre commande a été envoyée.";

And perform your Test successfully.

like image 44
undetected Selenium Avatar answered Oct 21 '22 03:10

undetected Selenium