Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium 2.0 Web Driver: implementation of isTextPresent

Tags:

junit

selenium

I'm looking for a working implementation of this. The best I've come up with is:

    public boolean isTextPresent(String string) {

        for (WebElement e : drv.findElements(By.cssSelector("*"))) {

            if (e.isDisplayed() && e.getText().contains(string)) {
                return true;
            }

        }
        return false;
    }
like image 627
Alex Collins Avatar asked Oct 20 '11 10:10

Alex Collins


1 Answers

A faster way to do it would be something like this:

public boolean isTextPresent(string str)
{
    IWebElement bodyElement = driver.FindElement(By.TagName("body"));
    return bodyElement.Text.contains(str);
}

It's in C# but it's the same concept. Getting the text of the body tag automatically returns the text of all the nested elements. The only thing I'm not sure about is if hidden elements are included or not.

like image 173
prestomanifesto Avatar answered Oct 06 '22 00:10

prestomanifesto