针对大量网站运行相同的测试

我需要针对70多个网站运行相同的测试,这些网站function相同,但皮肤不同。 但是,它们都通过不同的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... 

我的Selenium方法基页:

 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提供程序类:

 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"); } 

您可以在测试类EndToEndTest的构造函数上使用@Factory批注,并为其提供数据提供者。

 private String url; @Factory(dataProvider = "urls", dataProviderClass = URLProvider.class) public EndToEndTest(String url) { this.url = url; } 

您需要提供URL数据提供程序类的实现来访问excel或您想要的任何文件并返回Object [] []。

需要修改HomePage和SeleniumBase的构造函数,以便从@BeforeClass方法传入您调用的url。

这应该为每个url字符串的此测试类创建一个单独的实例,并调用@Test方法。

如果您需要传递比字符串URL更多的数据,则可以使用对象。 我可以看到你在testng.xml中为方法传递了4个参数。

并行性应该非常简单,猜测你想在单个线程中为给定的url运行所有@Test方法。 并行选项是“类”。