我尝试了以下代码,但它无效。 我试图在多个浏览器上并行运行它。 我也使用过Testng。但是当我执行下面的代码行时,它将在一个浏览器上执行,如(firefox)并运行所有代码,然后它将打开chrome浏览器并运行所有代码。
但我的要求是同时运行这三个浏览(同时)。
```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
<test name="Test1">
<parameter name="browser" value="Firefox"/>
<classes>
<class name="parallel_script.Demo_parallel_script"/>
</classes>
</test>
<test name="Test2">
<parameter name="browser" value="chrome"/>
<classes>
<class name="parallel_script.Demo_parallel_script"/>
</classes>
</test>
<test name="Test3">
<parameter name="browser" value="IE"/>
<classes>
<class name="parallel_script.Demo_parallel_script"/>
</classes>
</test>
</suite> <!-- Suite -->
```
```
public class Demo_parallel_script {
private WebDriver driver;
String baseURL = "http://www.google.com/";
@Parameters({ "browser" })
@Test
public void openBrowser(String browser) {
try {
if (browser.equalsIgnoreCase("Firefox")) {
System.setProperty("webdriver.gecko.driver","D:\\selenium driver\\geckodriver.exe");
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "D:\\selenium driver\\chromedriver.exe");
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("IE")) {
System.setProperty("webdriver.ie.driver", "D:\\selenium driver\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
} catch (WebDriverException e) {
System.out.println(e.getMessage());
}
driver.navigate().to(baseURL);
String title_name=driver.getTitle();
System.out.println(title_name);
driver.findElement(By.xpath("//input[@id='lst-ib']")).sendKeys("selenium");
driver.findElement(By.xpath("(//input[@type='submit'])[1]")).click();
}}
```
答案 0 :(得分:0)
为套件标记设置 threa-count 属性值为 3 ,如下所示,以并行运行所有测试。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="3">
<test name="Test1">
<parameter name="browser" value="Firefox"/>
<classes>
<class name="parallel_script.Demo_parallel_script"/>
</classes>
</test>
<test name="Test2">
<parameter name="browser" value="chrome"/>
<classes>
<class name="parallel_script.Demo_parallel_script"/>
</classes>
</test>
<test name="Test3">
<parameter name="browser" value="IE"/>
<classes>
<class name="parallel_script.Demo_parallel_script"/>
</classes>
</test>
</suite> <!-- Suite -->