英文:
How to use driver.quit() when running parallel tests
问题
我正在使用testNG
和Selenium并行运行4个任务。我的Java文件如下:
public class SecondNGTest {
// ...(其他代码)
@Test
public void executSessionOne() throws MalformedURLException{
// ...(测试1的代码)
}
@Test
public void executeSessionTwo() throws MalformedURLException{
// ...(测试2的代码)
}
@Test
public void executSessionThree() throws MalformedURLException{
// ...(测试3的代码)
}
@Test
public void executSessionFour() throws MalformedURLException{
// ...(测试4的代码)
}
@AfterTest
public void browserclose (){
driver.quit();
System.out.println("TestCase : Browser was closed");
}
}
问题在于测试执行后未关闭浏览器窗口。您可以尝试使用@AfterMethod
注解而不是@AfterTest
注解来确保每个测试方法执行后都会关闭浏览器。此外,由于您在每个测试方法中都创建了一个新的driver
实例,可以将该实例声明为局部变量,这将有助于更好地隔离不同测试之间的状态。
编辑后的代码如下:
public class SecondNGTest {
// ...(其他代码)
@Test
public void executSessionOne() throws MalformedURLException{
WebDriver driver = null;
try {
// ...(测试1的代码)
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
public void executeSessionTwo() throws MalformedURLException{
WebDriver driver = null;
try {
// ...(测试2的代码)
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
public void executSessionThree() throws MalformedURLException{
WebDriver driver = null;
try {
// ...(测试3的代码)
} finally {
if (driver != null) {
driver.quit();
}
}
}
@Test
public void executSessionFour() throws MalformedURLException{
WebDriver driver = null;
try {
// ...(测试4的代码)
} finally {
if (driver != null) {
driver.quit();
}
}
}
}
这样每个测试方法执行后都会关闭其对应的浏览器窗口。请注意,为了避免空指针异常,我在每个测试方法中使用了try-finally
块来确保即使测试中发生异常,浏览器也会被关闭。
英文:
i am running 4 parallel using testNG
and Selenium. My Java file looks like this:
public class SecondNGTest {
public static WebDriver driver;
public static ChromeOptions chromeOptions;
public static final String USERNAME = PRIVATE";
public static final String AUTOMATE_KEY = "PRIVATE";
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
public static DesiredCapabilities caps;
@Test
public void executSessionOne() throws MalformedURLException{
//First session of WebDriver
caps = new DesiredCapabilities();
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80.0 beta");
caps.setCapability("browserstack.local", "false");
caps.setCapability("browserstack.selenium_version", "3.5.2");
caps.setCapability("name", "selenium test");
driver = new RemoteWebDriver(new URL(URL), caps);
chromeOptions = new ChromeOptions();
String chromeDriverPath = "resources/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
//Goto guru99 site
driver.get("http://google.com/");
}
@Test
public void executeSessionTwo() throws MalformedURLException{
//Second session of WebDriver
caps = new DesiredCapabilities();
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80.0 beta");
caps.setCapability("browserstack.local", "false");
caps.setCapability("browserstack.selenium_version", "3.5.2");
caps.setCapability("name", "selenium test");
driver = new RemoteWebDriver(new URL(URL), caps);
chromeOptions = new ChromeOptions();
String chromeDriverPath = "resources/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
//Goto guru99 site
driver.get("http://youtube.com/");
}
@Test
public void executSessionThree() throws MalformedURLException{
//Third session of WebDriver
caps = new DesiredCapabilities();
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80.0 beta");
caps.setCapability("browserstack.local", "false");
caps.setCapability("browserstack.selenium_version", "3.5.2");
caps.setCapability("name", "selenium test");
driver = new RemoteWebDriver(new URL(URL), caps);
chromeOptions = new ChromeOptions();
String chromeDriverPath = "resources/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
//Goto guru99 site
driver.get("http://slack.com/");
}
@Test
public void executSessionFour() throws MalformedURLException{
//Third session of WebDriver
caps = new DesiredCapabilities();
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80.0 beta");
caps.setCapability("browserstack.local", "false");
caps.setCapability("browserstack.selenium_version", "3.5.2");
caps.setCapability("name", "selenium test");
driver = new RemoteWebDriver(new URL(URL), caps);
chromeOptions = new ChromeOptions();
String chromeDriverPath = "resources/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
//Goto guru99 site
driver.get("http://yahoo.com/");
}
@AfterTest
public void browserclose (){
driver.quit();
System.out.println("TestCase : Browser was closed");
}
}
It works as it should by opening up 4 browser windows at once and running the tests but the problem is they never end. The @AfterTest
block is only being triggered once, so one of the windows does close but the rest are left open and I have to manually close them. How can I close/quit each driver when each test is done?
EDIT: here is my testng.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="TestSuite" >
<test name="testGuru" thread-count="4" parallel="methods">
<classes>
<class name="selerixautomation.SecondNGTest">
</class>
</classes>
</test>
</suite>
答案1
得分: 2
由于您正在同一个Java类的不同方法(例如,一个带有@Test注解的方法)中进行并行执行,您需要调用**@AfterMethod**钩子来关闭所有驱动程序(在每个方法或@Test注解方法之后)。
英文:
Since you are doing your parallel execution inside one java class but different methods (e.g. one @Test annotation one method) you need to call @AfterMethod hook to close all drivers (after every method / @Test annotation method).
答案2
得分: 0
你可以尝试使用注解 @AfterClass 而不是 @AfterTest 来关闭驱动程序。
@AfterClass
public void browserclose(){
driver.quit();
System.out.println("TestCase: 浏览器已关闭");
}
英文:
You can try to kill your driver with annotation @AfterClass instead of @AfterTest.
@AfterClass
public void browserclose (){
driver.quit();
System.out.println("TestCase : Browser was closed");
}
答案3
得分: 0
你应该彻底重新设计你的类。首先,在设计代码以便并行运行时,避免使用静态字段。静态字段是类的属性,而不是对象的属性。因此,在被实例化时,它在所有并行线程之间共享,这可能会导致意外的结果,除非进行同步。
因此,你需要移除 static
修饰符,并在 @BeforeTest
中创建你的驱动对象(不要在测试代码中创建),然后在 @AfterTest
中退出你的驱动器。
英文:
You should completely re-design your class.. First of all avoid using static fields when design your code to be running in parallel. Static field is a property of a class, not of an object. So being instantiated it is shared between all parallel threads and thus that can lead to unexpected results unless synchronized.
So you need to remove static
modifier and create your driver objects in @BeforeTest
(not in test code) and then quit your drivers in @AfterTest
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论