我需要针对70多个功能相同但外观不同的网站进行相同的测试.它们都是通过不同的URL访问的.
使用TestNG和Java,将URL传递给测试的有效方法是什么,这样我可以:
a)针对每个站点运行每个测试并报告相同的结果
b)并行执行测试以节省时间(将来需要)
我想以某种格式存储URL,以便最终用户可以访问并由他们配置.理想情况下,它应位于.csv中,也可以位于testng.xml文件中.我在考虑@DataProvider或@Factory,但不确定如何以有效且可维护的方式使用这些参数来从外部源获取参数,或者在我当前的模型中哪种方法最合适?我遇到的困难是,我不想将数据传递给@Test,而一次传递一个值(URL)并针对所有@Test带注释的方法运行.
我当前的简单设置如下:
testngxml:
我的验收测试:
public class EndToEndTest extends DriverBase{
private HomePage home;
private String url;
@Factory(dataProvider = "urls", dataProviderClass = URLProvider.class)
public EndToEndTest(String url) {
this.url = url;
}
@BeforeSuite
public void stuff(){
newDriver();
}
@BeforeClass
public void setup(){
home = new HomePage(driver, url);
}
@Test (priority = 1)
@Parameters({"from","to"})
public void searchForARoute(String from, String to) throws InterruptedException {
home.selectWhereFrom(from);
home.selectWhereTo(to);
//some assertions...
我的PageObject:
public class HomePage extends SeleniumBase {
public HomePage(WebDriver driver, String url) {
super(driver, url);
try {
visit(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
driver.manage().window().maximize();
}
public void someMethodOrOther(){
//some methods...
我的硒方法BasePage:
public class SeleniumBase implements Config {
public WebDriver driver;
public String url;
public SeleniumBase(WebDriver driver) {
this.driver = driver;
this.url = url;
}
public void visit() throws MalformedURLException {
driver.get(url);
} //more methods...
我的驱动程序基本页面:
public class DriverBase implements Config {
public static WebDriver driver;
public static String url;
protected void newDriver() {
if (host.equals("localhost")) {
if (browser.equals("firefox")) {
System.setProperty("webdriver.gecko.driver",
System.getProperty("user.dir") + "\drivers\geckodriver.exe");
FirefoxProfile fp = new FirefoxProfile();
//more stuff
URL Provider类:
public class URLProvider {
@DataProvider(name = "urls")
public static Object[][] dataProviderMethod()
{
return new Object[][] {{"http://siteone.com"},
{"http://sitetwo.com"},
{"http://sitethree.com"}
};
}
}
最后,一个当前仅包含BaseUrl的Config:
public interface Config {
String baseUrl = System.getProperty("baseUrl",
String browser = System.getProperty("browser", "chrome");
String host = System.getProperty("host", "localhost");
}