use*_*405 2 testng selenium selenium-grid
我有两个具有相同配置的节点(Win7,ie9).我将testNG设置为同时对每个节点执行两次测试.Selenium Grid同时打开两个浏览器(每个节点一个),但测试似乎只在一个浏览器中执行 - 用户名在同一个浏览器中输入两次.如果我设置thread-count=1,两个测试都会成功.
testNG测试套件xml文件:
<suite name="ExampleTest" parallel="tests" thread-count="2">
<test name="ExampleTest1">
<classes>
<class name="com.mycompany.testsuites.GridTest">
<methods>
<include name="test1"></include>
</methods>
</class>
</classes>
</test>
<test name="ExampleTest2">
<classes>
<class name="com.mycompany.testsuites.GridTest">
<methods>
<include name="test2"></include>
</methods>
</class>
</classes>
</test>
Run Code Online (Sandbox Code Playgroud)
测试类GridTest:
public class GridTest {
private WebDriver driver;
String hubUrl = "http://12.11.14.15:4444/wd/hub";
@BeforeClass
public void beforeClass() throws MalformedURLException {
DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
driver = new RemoteWebDriver(new URL(hubUrl), capability);
Page.setUp(driver); //Page is the base class of all page objects.
//The static setUp method assign a WebDriver object
//to static Page.driver, which shared by all pages.
}
@AfterClass
public void afterClass() {
driver.quit();
}
@Test
public void test1() {
driver.get("http://12.11.15.16");
LoginPage login = new LoginPage();
login.setUp(); // set up the Login Page
login.txtUsername().clear(); //clear the username input box
login.txtUsername().sendKeys("username"); // input user name
// ...
}
@Test
public void test2() {
driver.get("http://12.11.15.16");
LoginPage login = new LoginPage();
login.setUp();
login.txtUsername().clear();
login.txtUsername().sendKeys("username");
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
我使用selenium-server-standalone-2.41.0.jar作为集线器和节点.
你看到的行为是正确的.
驱动程序在@beforeClass中创建,并且只执行一次,因此只创建一个浏览器,两个测试同时使用.
如果你要更改为@beforeMethod,那么每次测试都会创建一个浏览器但是这仍然会因为使用相同的变量(驱动程序)来存储驱动程序实例而失败.您会看到两个浏览器打开但两个测试都在第二个中执行.
要在测试级别上并行运行,您需要确保每个测试都是完全封装的,例如在自己的上下文中创建自己的驱动程序,这不允许您使用设置和拆除.
通常我会在类级别上并行运行,因为这种方法允许您仍然使用设置和拆除来创建驱动程序实例.我建议更改XML以运行parallel = classes.显然你必须有多个班级,但我认为实际上你有两个以上的测试要运行!
| 归档时间: |
|
| 查看次数: |
6273 次 |
| 最近记录: |