Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

Suppose I have an add question facility but can't add same question in the form. So need to check whether the question is already visible on the screen, if not then add it.

I am not getting how to match a string value to the content of a form. is it possible to do so with selenium webdriver. Or if there is any other way to achieve this? I just need to check a text whether displayed on the screen or not.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

When you use driver.getPageSource() it gives you entire page source. To be more efficient, use something like this:

WebElement element = driver.findElement(By.xpath("//*[contains(.,.)]"));

return element.getText.contains(data);

share|improve this answer

Something like this should work:

public boolean isTextOnPagePresent(String text) {
    WebElement body = driver.findElement(By.tagName("body"));
    String bodyText = body.getText();
    return bodyText.contains(text);
}

Currently I have no possibility to test that. If it should not work, you could try to use getPageSource() instead of get the text of the body tag. But this does not furfill the "on the visible html page" part of your question.

share|improve this answer
    
In this case I think the visible part is irrelevant unless all questions are in the body allready. Looping thru all visible elements and checking getting the text is more complex and time consuming. +1 for quick and dirty :) –  Niels van Reijmersdal Jul 15 at 7:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.