Selenium + Java = Assertion error

I already have this kind of error, and I still don't know why. What am I doing wrong?

I need to assert true if I get a text in the page source.

So here is my method:

public boolean AssertSearch() {                 
    return driver.getPageSource().contains("Item found");               
}

And here is my assert:

assertTrue(buscarnok.validabuscaNOK());

And I keep receiving the message "Assertion Error". I don't know why. If I change the "return driver.getPageSource().contains("Item found");"to driver.findelement(by.id("someID")).isdisplayed();it works fine, so why isn't it working with getpagesource?

Upvotes: 1

Views: 3936

Answers (1)

blurfus
blurfus

Reputation: 14031

If the text you are looking for is not initially in the page or if it is hidden, it may not find it.

Try something like this:

String bodyText = driver.findElement(By.tagName("body")).getText();
Assert.assertTrue("Item Found", bodyText.contains(text));

You can narrow down the search by selecting a different tag name or even a div by its class or id

Upvotes: 1

Related Questions