【发布时间】:2013-05-21 03:34:21
【问题描述】:
我正在尝试使用 webdriver 验证加载的页面上是否存在一行文本。 我创建了一个函数 - isTextPresent,然后在同一方法中调用该函数。
Eclipse 提示我错误:方法 IsTrue(boolean) 未定义类型 Assert。
- 请告诉我为什么它不起作用以及我应该如何解决它。
-
验证网页上的文本呈现的最佳方法是什么?
2a。是否可以在第一个
@Test代码片段中验证文本呈现?2b。在这种情况下我应该使用哪种类型##标题##方法(公共、私有或受保护)?
我的代码片段:
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"));
}
}
我为调用函数 isElementPresent 所做的修改 在 searchElements() 方法
中添加 assertTrue() 方法 assertTrue(isTextPresent(txtValue));
方法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;
}
【问题讨论】:
-
@user1177636 感谢您的回复。我确实在函数调用的代码中使用了 assertTrue() 方法。它按预期工作。
标签: webdriver