Yes, you can do what boolean returns. The following java code in WebDriver with TestNG or JUnit can execute:
protected boolean isTextPresent(String text){ try{ boolean b = driver.getPageSource().contains(text); return b; } catch(Exception e){ return false; } }
Now call the above method as shown below:
assertTrue(isTextPresent("Your text"));
Or, there is another way. I think this is the best way:
private StringBuffer verificationErrors = new StringBuffer(); try { assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]* Your text here\r\n\r\n[\\s\\S]*$")); } catch (Error e) { verificationErrors.append(e.toString()); }
user2027659
source share