Reputation: 71
I want to assert that a string of text is present on a page before moving on and am having trouble.
I am using Selenium Webdriver
with Java
. Here is the code I've tried:
String str2 = driver.findElement(By.id("ctl00$MainContent$cklRepair$10")).getText();
Assert.assertTrue(str2.contains("text"), "Lubrication");
and
boolean textFound = true;
try {
driver.findElement(By.id("ctl00_MainContent_cklRepair_10"));
textFound = true;
}catch (Exception e) {
textFound = false;
}
I want to validate that Lubrication exists.
HTML:
<span class="label">
<input id="ctl00_MainContent_cklRepair_10" type="checkbox" value="Lubrication,600" onclick "javascript:setTimeout('__doPostBack(\'ctl00$MainContent$cklRepair$10\',\'\')', 0)" name="ctl00$MainContent$cklRepair$10">
<label for="ctl00_MainContent_cklRepair_10">Lubrication</label>
Upvotes: 1
Views: 2559
Reputation: 16201
str2
already returns the string to compare with whereas contains()
does a search if the str2
contains the Lubrication
text.
Assert.assertTrue(str2.contains("Lubrication"));
And, just looked at the selector and looks the selector you are using is not returning the text. You can use the following xpath
instead
//input[contains(@id,'MainContent_cklRepair')]//..//label
The complete code block should look like:
By byXpath = By.xpath)("//input[contains(@id,'MainContent_cklRepair')]//..//label");
String str2 = driver.findElement(byXpath).getText();
Assert.assertTrue(str2.contains("Lubrication"));
Upvotes: 2