isTextPresent in Selenium 2 WebDriver?

one common question I get is:


What is the correct way to assert the existence of some text on a page with WebDriver?


Solution:
AssertEquals(driver.getPageSource().contains(text), true);

Comments

  1. ... which is a bad solution. E.g. does not reflect what is displayed on the page.

    From the Selenium FAQ:

    "Q: What's the Selenium WebDriver API equivalent to TextPresent ?
    A:

    driver.findElement(By.tagName("body")).getText()
    will get you the text of the page. To verifyTextPresent/assertTextPresent, from that you can use your favourite test framework to assert on the text. To waitForTextPresent, you may want to investigate the WebDriverWait class."

    ReplyDelete
  2. I agree with artoo that it's not very nice solution. I believe that selenium test should act like
    live person: when I open page and want to verify that one of headlines equals 'isTextPresent WebDriver', I will take a look in the particular place of page, I don't need to open page source or read all text in the page from header to footer.

    So I will check only that block which I need, e.g.: AssertEquals(getDriver().findElement(By.tagName("h1")).contains(text), true);

    ReplyDelete

  3. Hi, Reach Mr.Vishwa for Best Selenium and Coded UI Training in Chennai with real time project assistance. This people will teach everything from the basics up to advanced level scenarios from frameworks.

    I learned from Mr.Vishwa and Raj they are very good automation people for Selenium Frameworks. After joined with them I learned many things and now I am writing automation scripts in own project. Vishwa 9003085882
    selenium training in chennai

    ReplyDelete
  4. Selenium training in Chennai
    We provide best selenium training in Chennai with real time scenarios.For real time training reach us 8122241286 and become experts in selenium.
    selenium training in chennai

    ReplyDelete

Post a Comment

Popular Posts