如下为结合testNG进行的数据驱动测试
package cn.gloryroad;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.ie.InternetExplorerDriver;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
public class DataProviderTest {
private static WebDriver driver;
@DataProvider(name = "searchWords")
public static Object[][] words(){
return new Object [][]{{"蝙蝠侠","主演","迈克尔"},{"超人","导演","唐纳"},{"生化危机","编剧","安德森"}};
}
@Test(dataProvider = "searchWords")
public void test(String searchWord1,String searchWord2,String SearchResult) {
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
//设定等待时间10s
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String baseUrl = "http://www.baidu.com/";
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com");
driver.findElement(By.id("kw")).sendKeys(searchWord1+""+searchWord2);
driver.findElement(By.id("su")).click();
//等待三秒显示搜索结果
try{
Thread.sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}
//判断搜索结果的页面是否包含测试数据中期望的关键字
Assert.assertTrue(driver.getPageSource().contains(SearchResult));
driver.quit();
}
}
selenium WebDriver:使用TestNG进行数据驱动
原文:http://www.cnblogs.com/Treasa/p/5027106.html