【发布时间】:2016-01-06 19:06:21
【问题描述】:
当我使用 TestNG 和 selenium-webdriver 运行并行测试用例时,有一些浏览器实例打开为空并且即使在执行完成后也不会关闭。我在不同的论坛上阅读了几个主题,询问类似的问题,但似乎都不是同一个问题。
当我向套件添加更多测试时,似乎打开了更多空浏览器。我不确定以下哪些信息与此问题相关,但我会尽力提供我所拥有的完整信息:
- 我正在使用(如前所述)TestNG - webdriver。
- 我正在使用页面对象模式。
- 我正在从 javafx 界面运行自动化,它允许用户选择一些选项。
- 我正在使用 ChromeDriver 来模拟移动设备。
- 我正在使用驱动程序的本地线程,它允许我为每个测试使用专有的静态驱动程序。
- 当我不并行运行时,没有打开任何空的浏览器。
这是我的 TestNg.xml:
<suite name="Suite" parallel="classes" thread-count="4">
<test name="Test">
<groups>
<run>
<!-- Use include to specify what you want to run or exclude to not run the mentioned group(s) -->
<include name="fullRegression"/>
<exclude name="brokenOrDevelopment"/>
</run>
</groups>
<packages>
<!-- Packages who contains the test cases classes -->
<package name="Test"/>
</packages>
这是创建驱动程序实例的类
public class TestCaseConfiguration {
protected HomePageObjects homePage = null;
protected DesiredCapabilities capabilities;
protected ExtentReports REPORTMANAGER;
protected ExtentTest REPORT;
public static ThreadLocal<ChromeDriver> driver;
@BeforeMethod(alwaysRun = true)
public void setup() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "src/Tools/chromedriver.exe");//chrome driver version 2.20 | We need to have a relative path since no all the user will have the driver on C:
capabilities = DesiredCapabilities.chrome();
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", Constants.DEVICE);
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
/**
* dirver is a ThreadLocal<ChromeDriver>. This allows the driver to be static and available for the complete project, and exclusive for each thread.
*/
driver=new ThreadLocal<ChromeDriver>()
{
@Override
protected ChromeDriver initialValue()
{
return new ChromeDriver(capabilities);
}
};
driver.get().manage().deleteAllCookies();
driver.get().manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.get().navigate().to(Constants.DOMAIN);
homePage = PageFactory.initElements(driver.get(), HomePageObjects.class);
}
@AfterMethod(alwaysRun = true)
public void close() throws InterruptedException {
driver.get().close();
driver.get().quit();
}
}
这个类被每个测试用例类扩展。测试按预期运行,唯一的问题似乎是额外的浏览器实例。
没有人知道我该如何避免这个问题或者我做错了什么? 感谢您的帮助。
【问题讨论】:
标签: browser parallel-processing webdriver testng