Reputation: 485
I am trying to verify a line of text is present on a loaded page using webdriver. I created a function - isTextPresent, then invoked the function within the same method.
Eclipse prompted me the error: the method IsTrue(boolean) is undefined for the type Assert.
whatis the best approach to verify the text presentation on a web page?
2a. is it possible to verify the text presentation within the first @Test
code fragment?
2b. which type## Heading ## of method shall I use in this case (public, private or protected)?
My code fragment:
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.junit.Before;
import org.junit.After;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class selftechyTestng
{
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception
{
driver = new FirefoxDriver();
baseUrl = "http://selftechy.com/";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
//First Test Method
@Test
public void searchElements() throws Exception{
driver.get(baseUrl);
driver.findElement(By.xpath("//a[@title='Selenium']")).click();
}
@Test
public boolean isTextPresent(String txtValue){
try{
boolean b = driver.getPageSource().contains(txtValue);
return b;
}
catch (Exception e){
return false;
}
Assert.IsTrue(isTextPresent("TestNG (Next Generation Testing Framework) – Understanding Annotations"));
}
}
Modification I have done to make the call to the function isElementPresent work Adding assertTrue() method within searchElements() methods
assertTrue(isTextPresent(txtValue));
Method isElementPresent
public boolean isTextPresent(String str1)
{
try
{
driver.get(baseUrl);
driver.findElement(By.xpath("//a[@title='Selenium']")).click();
b = driver.getPageSource().contains(str1);
if(b){
System.out.println("text presented on the page");
}
else{
System.out.println("text did not present on the page");
}
return b;
}
catch (Exception e)
{
System.out.println(e.getMessage());
return b;
}
//return b;
}
Upvotes: 0
Views: 12231
Reputation: 1
Example for C#
String OrgName = driver.FindElement(By.Id("")).GetAttribute("")
Assert.True(driver.FindElement(By.XPath("")).Text.Contains(OrgName));
Upvotes: -2
Reputation: 14748
or you can do this. First, remove the @Test
annotation in the method and remove the Assert
at the end:
public boolean isTextPresent(String txtValue){
boolean b = false;
try{
b = driver.getPageSource().contains(txtValue);
return b;
}
catch (Exception e){
System.out.println(e.getMessage());
}
finally{
return b;
}
}
Then your test will look like this
@Test
public void searchElements() throws Exception{
driver.get(baseUrl);
driver.findElement(By.xpath("//a[@title='Selenium']")).click();
Assert.IsTrue(isTextPresent("TestNG (Next Generation Testing Framework) – Understanding Annotations"));
}
Upvotes: 2
Reputation: 7525
I am just giving you the clue...You can assert text in like this.
assertEquals(txtValue, "TestNG (Next Generation Testing Framework) – Understanding Annotations");
Refer this link for more information on Assertions: http://testng.org/javadoc/org/testng/Assert.html
You can use assertEquals (String1, String2)
. Refer this link: Java: Is assertEquals(String, String) reliable?
-Vikram
Upvotes: 1