我使用selenium来测试单页应用。我用@beforeClass
方法加载页面。然后在课堂上运行所有测试。然后用@afterClass
方法拆除驱动程序。
我使用基类中的@factory
将我的构造函数注释为dataprovider
方法。 dataprovider
用于设置不同浏览器的集合。
我使用crossbrowsertesting.com来运行测试,而我正在努力控制并行化。我需要能够控制任何时候运行的最大并发线程数(我们当前的计划只允许2个同时测试)。我想确保在数据提供者级别进行并行化。 (即我想为每个浏览器创建一个最多2个线程的线程。)
我正在使用testng.xml文件。
<suite name="functional_tests" parallel="classes" thread-count="2">
<test name="com.mapov.functional_tests suite">
<classes>
<class name="com.mapov.functional_tests.KiteTests" />
</classes>
</test>
</suite>
使用基类 -
public class BaseTest {
public BaseTest (String browser, String platform, String resolution, String browserDescription) {
userName = "myUsername";
accessKey = "myKey";
pageUrl = "http://test.mapov.com/hotels/benidorm";
this.browserDescription = browserDescription;
caps = new DesiredCapabilities();
caps.setCapability("browser_api_name", browser);
caps.setCapability("os_api_name", platform);
caps.setCapability("screen_resolution", resolution);
caps.setCapability("name", this.browserDescription);
caps.setCapability("record_video", "false");
caps.setCapability("record_network", "false");
caps.setCapability("record_snapshot", "false");
}
@DataProvider(parallel=true)
public static Object[][] getBrowsers() {
return new Object[][]{
new Object[]{"MblSafari8.0", "iPadAir-iOS8Sim", "1024x768", "Ipad air, with safari 8"},
new Object[]{"IE11", "Win10", "1280x1024", "IE11 on windows 10"},
new Object[]{"Safari9", "Mac10.11", "1280x1024", "Safari 9 on Mac osx10.11"},
};
}
@BeforeClass
public void SetupDriver() {
long id = Thread.currentThread().getId();
try {
URL remoteDriverURL = new URL("http://" + userName + ":" + accessKey + "@hub.crossbrowsertesting.com:80/wd/hub");
driver = new RemoteWebDriver(remoteDriverURL, caps);
driver.get(pageUrl);
WebElement pager = (new WebDriverWait(driver, 30)).until(ExpectedConditions.visibilityOfElementLocated(By.id("pager")));
} catch (MalformedURLException ex) {
System.out.println(ex.getMessage());
}
}
@AfterClass
public void disposeOfDriver() {
// quit the driver
}
}
然后是带有测试的继承类。
public class KiteTests extends BaseTest {
@Factory(dataProvider = "getBrowsers")
public KiteTests(String browser, String platform, String resolution, String description) {
super(browser, platform, resolution, "Kite tests on " + description);
}
@Test
public void testKitesAppear() {
// test.
}
@Test
public void testHotelNamePresent() {
// test
}
}
如果我从xml的套件节点中删除parallel="classes" thread-count="2"
属性,那么一切运行正常(只是不并行)。
如果我保留参数,那么我会收到以下错误 -
FAILED CONFIGURATION: @BeforeClass SetupDriver
org.openqa.selenium.WebDriverException: This account has reached it maxiumum number of concurrent selenium tests - please wait for a test to finish before starting a new test.
Command duration or timeout: 668 milliseconds
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'Ewen-Macbook.lan', ip: '192.168.1.65', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:247)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:129)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:156)
at com.mapov.functional_tests.BaseTest.SetupDriver(BaseTest.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:1)
我认为使用TestNG的参数概念是获得所需结果的更好方法。
的testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="functional_tests Suite" parallel="tests"
thread-count="2">
<test name="functional_tests suite - Ipad air, with safari 8">
<parameter name="browser" value="MblSafari8.0" />
<parameter name="os" value="iPadAir-iOS8Sim" />
<parameter name="screen" value="1024x768" />
<parameter name="desc" value="Ipad air, with safari 8" />
<classes>
<class name="com.mapov.functional_tests.KiteTests" />
</classes>
</test>
<test name="functional_tests suite - IE11 on windows 10">
<parameter name="browser" value="IE11" />
<parameter name="os" value="Win10" />
<parameter name="screen" value="1024x768" />
<parameter name="desc" value="IE11 on windows 10" />
<classes>
<class name="com.mapov.functional_tests.KiteTests" />
</classes>
</test>
<test name="functional_tests suite - Safari 9 on Mac osx10.11">
<parameter name="browser" value="Safari9" />
<parameter name="os" value="Mac10.11" />
<parameter name="screen" value="1024x768" />
<parameter name="desc" value="Safari 9 on Mac osx10.11" />
<classes>
<class name="com.mapov.functional_tests.KiteTests" />
</classes>
</test>
BaseClass的
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
public class BaseTest {
@Parameters({ "browser", "os", "screen", "desc" })
@BeforeClass
public void SetupDriver(String browser, String os, String screen, String desc) {
// connect to the remote server and load the page.
System.out.println(browser + os + screen + desc);
}
@AfterClass
public void disposeOfDriver() {
// quit the driver
}
}
测试类
import org.testng.annotations.Test;
public class KiteTests {
@Test
public void testKitesAppear() {
// test.
}
@Test
public void testHotelNamePresent() {
// test
}
}
此结构提供控制以从XML文件配置测试流而不更改JAVA代码。
希望这可以帮助你!!
答案 1 :(得分:0)
尝试使用:
data-provider-thread-count="2"
代替thread-count="2"