Skip to content
Advertisement

Selenium web driver assert webelement contains text and display actual vs expected

I have a demo selenium cucumber project that just runs some maths expressions in google search box and validates the results.

I want the reports to show the actual value vs expected when it fails.

I’m storing the returned text as string with ..

String resultText = driver.findElement(By.cssSelector("div[class='dDoNo vrBOv vk_bk']")).getText();

and then asserting against expected which gets passed from my feature files with …

assertTrue(resultText.equals(expectedResult));

but the reports show …

java.lang.AssertionError: expected [true] but found [false]

I want it to display the actual expected result vs actual i.e.

java.lang.AssertionError: expected [200] but found [201]

I’ve also tried …

Assert.assertEquals(expectedResult, resultText);

I’m using … org.testng.Assert

Thanks

Advertisement

Answer

With the assertion you can use your customized error text. Something like this:

Assert.assertEquals(resultText, expectedResult, "Expected: " + expectedResult + " but found " + resultText);

Or this

Assert.assertTrue(expectedResult.equals(resultText), "Expected: " + expectedResult + " but found " + resultText);

In case of non-equal parameters it will present you output like this for the first case:

java.lang.AssertionError: Expected: expectedResult but found resultText
Expected :expectedResult
Actual :resultText

And for the second case:

java.lang.AssertionError: Expected: expectedResult but found resultText
Expected :true
Actual :false

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement