JUnit是一个用来创建 Java的 Selenium WebDriver测试非常流行的框架。我们可以使用 JUnit4参数化的特性来创建 Selenium WebDriver的数据驱动测试.
import java.util.Arrays;
import java.util.Collection;import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
@RunWith(value=Parameterized.class)
public class SeleniumDataDriver {
private static WebDriver driver;
private String baseUrl = "http://www.baidu.com";
private String searchValue;
public SeleniumDataDriver(String searchValue) {
this.searchValue = searchValue;
}
@Parameters
public static Collection testData() {
return Arrays.asList(new Object[][] { { "QTP"}, { "Selenium"},{ "LoadRunner"}});
}
@Test
public void teatSearch() {
driver = new FirefoxDriver();
driver.get(baseUrl);
waitPageToLoad(3000);
WebElement query = driver.findElement(By.id("kw"));
query.sendKeys(searchValue);
waitPageToLoad(3000);
WebElement clickSearchButton = driver.findElement(By.id("su"));
clickSearchButton.click();
waitPageToLoad(3000);
driver.close();
}
public static void waitPageToLoad(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@After
public void tearDown(){
driver.quit();
}
}