在Eclipse中使用Maven配置WebDriver+Testng(2)

    xiaoxiao2022-07-15  164

    建立一个简单的 测试项目 package net.Maventest; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class WebDriverDemoTest{ private  WebDriver driver; private String baseUrl; @BeforeClass public void setUp() throws Exception { System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); driver = new FirefoxDriver(); baseUrl = "http://www.baidu.com/"; } @Test public void testSearch() throws Exception { driver.get(baseUrl); Thread.sleep(1000); WebElement input = driver.findElement(By.id("kw1")); input.sendKeys("天气预报"); Thread.sleep(1000); WebElement search = driver.findElement(By.id("su1")); search.click(); Thread.sleep(1000); Assert.assertTrue(driver.getTitle().contains("天气预报"),"Title is incorrect."); System.out.println(driver.getTitle()); //      (new WebDriverWait(driver,10)).until(new ExpectedCondition<Boolean>(){ //          public Boolean apply(WebDriver d){ //                 return d.getTitle().startsWith("天气预报"); //          } //      }); } @AfterClass public void tearDown() throws Exception { driver.quit(); } }   加入Thread.sleep是由于有时候执行的太快了以至于页面还没显示完整,以至于报错。应该有更好的方法,稍后再编辑这里。   注释掉的部分是另外一种判断方法,关于WebDriver的等待方式,可以参考这篇文章http://blog.csdn.net/pf20050904/article/details/20052485   testng.xml配置如下: <?xml version="1.0" encoding="UTF-8"?> <suite name ="MavenTest" verbose="2"> <test name ="baiduTest"> <classes> <class name="net.Maventest.WebDriverDemoTest"> <methods> <includ name="setUp"/> <includ name="testSearch"/> <includ name="tearDown"/> </methods> </class> </classes> </test> </suite> 最新内容请见作者的GitHub页:http://qaseven.github.io/ 相关资源:敏捷开发V1.0.pptx
    最新回复(0)