1

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?

3
  • 1
    is the "Item Found" contained in the source or added later (after the search is done)? Commented Oct 13, 2015 at 23:40
  • After the search. Once the search is successfully done, the page displays the message "item found" with the item 's details Commented Oct 13, 2015 at 23:58
  • 1
    Then the text is not found in the page source, is it? What you want is to find text (on the HTML) Commented Oct 14, 2015 at 0:02

1 Answer 1

1

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

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

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.