我正在编写基于 Java 的 selenium-web-driver 测试以使用 testng 运行并行跨浏览器测试。 我已将测试设置为在我的 xml 文件上并行运行。该文件如下所示:
<suite name="TestSuite" thread-count="2" parallel="tests" >
<test name="ChromeTest">
<parameter name="browser" value="Chrome" />
<classes>
<class name="test.login"/>
<class name="test.main"/>
<class name="test.logout"/>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="Firefox" />
<classes>
<class name="test.login"/>
<class name="test.main"/>
<class name="test.logout"/>
</classes>
</test>
但是当我运行测试时,两个浏览器实例都会打开(Chrome 首先打开并开始执行,然后在延迟后打开 Firefox)。 在那种情况下,驱动程序对象被 Firefox 驱动程序覆盖,chrome 停止执行。测试继续在 Firefox 和 成功完成。
项目的结构是这样的:
- 创建了一个 driverbase.class 以加载与具有我的 @Beforesuite 的浏览器相对应的驱动程序。
- 为页面创建单独的类。(例如:login.class、main.class 等)只有@Test 方法并扩展了驱动程序基类以获取驱动程序。
当我在 xml 文件上将 parallel 设置为 none 时,测试运行成功
<suite name="TestSuite" thread-count="2" parallel="none" >
我该如何克服这个问题?如何在没有这个问题的情况下并行运行测试?
驱动基类是这样的:
public class driverbase {
private String baseUrl;
private String nodeUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
public static WebDriver driver = null;
/**
* This function will execute before each Test tag in testng.xml
* @param browser
* @throws Exception
*/
@BeforeSuite
@Parameters("browser")
public WebDriver setup(String browser) throws Exception{
//Check if parameter passed from TestNG is 'firefox'
if(browser.equalsIgnoreCase("firefox")){
System.out.println("Browser : "+browser);
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
//create firefox instance
driver = new FirefoxDriver(profile);
}
//Check if parameter passed as 'chrome'
else if(browser.equalsIgnoreCase("chrome")){
System.out.println("Browser : "+browser);
//set path to chromedriver.exe You may need to download it from http://code.google.com/p/selenium/wiki/ChromeDriver
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
//create chrome instance
driver = new ChromeDriver(options);
}
else{
//If no browser passed throw exception
System.out.println("Browser is incorrect");
throw new Exception("Browser is not correct");
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
return driver;
}
感谢您的帮助:)
最佳答案
@BeforeSuite
方法不应返回某些内容。 => 替换为void
- 您的 testng 有 2 个不同的测试,但
@BeforeSuite
将始终按套件运行一次,您的评论显示您不希望它。 => 替换为@BeforeTest
- 当您在//中运行时,有 2 个线程正在设置驱动程序值(一个用于 firefox,一个用于 chrome),这解释了您的问题。
你可以试试这样的:
public class driverbase {
private String baseUrl;
private String nodeUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
public WebDriver driver;
@BeforeTest
@Parameters("browser")
public void setup(String browser) throws Exception {
if(browser.equalsIgnoreCase("firefox")) {
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
driver = new FirefoxDriver(profile);
} else if(browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
driver = new ChromeDriver(options);
} else {
throw new Exception("Browser is not correct");
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
你应该看看 http://fluentlenium.org/也是。
关于java - 当测试并行运行时,Webdriver 对象被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33385176/