I have this little piece of code that just checks for a particular text at a location on the page and if that text is found, it will check for something else..Here is the snippet:
if (!selenium.isTextPresent("//div[contains(concat('',@class,''),'contentLg')]/h2")) { System.out.println("The About Us text is not present"); } else { verifyEquals(aboutText, "//*[@class='content contentLg']/p[3]"); }
Here is the generated output that I am trying to grab the text "About Us" from. I am trying to see if the page has "About Us" text (which is in
tag). If there is, just match one of the "p" tag with predefined text:
<!-- MAIN CONTENT CONTAINER -->
<!-- MAIN CONTENT CONTAINER -->
About Us
<p><span class="faqQuestion">About The Site</span><br>
this is about the site</p>
<p>test one two three</p>
<p>this test test test test test test</p>
<br>
<p><span class="faqQuestion">About Company</span><br>
This is about the compan开发者_运维技巧y</p>
<p>fafasdfsafasdfafasfasfasasfasfasfafsasf</p>
<p>afasfasdfasdffafffffffffffffffffffffffffffffffffffffffffffffffffffffffffff</p>
<br>
<p><span class="faqQuestion">Helping Businesses</span><br>
Helping business
<a href="/businesses.asp">click here</a> to learn more.</p>
Here is what I wrote in a CustomSelenium class that extends DefaultSelenium.
public boolean waitForTextPresent(final String text, long timeout) {
Wait wait = new Wait() {
@Override
public boolean until() {
try {
return CustomSelenium.this.isTextPresent(text);
} catch (SeleniumException e) {
return false;
}
}
};
try {
wait.wait("Error: text " + text + " not present in the page", timeout, 50);
} catch (NumberFormatException e) {
log.error(e.getMessage(), e);
throw e;
} catch (WaitTimedOutException e) {
log.warn(e.getMessage(), e);
return false;
}
return true;
}
Just call this before you call isTextPresent. This is a Selenium bug.
精彩评论