运行 并行测试时如何使用 driver.quit()

How to use driver.quit() when running parallel tests

我是 运行 4 并行使用 testNG 和 Selenium。我的 Java 文件如下所示:

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");
    }
}

通过一次打开 4 个浏览器 windows 和 运行 测试,它可以正常工作,但问题是它们永远不会结束。 @AfterTest 块只被触发一次,因此 windows 中的一个确实关闭了,但其余的都保持打开状态,我必须手动关闭它们。完成每个测试后,我如何 close/quit 每个驱动程序?

编辑:这是我的 testng.xml 文件:

<?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>

您可以尝试使用注解 @AfterClass 而不是 @AfterTest 来终止您的驱动程序。

@AfterClass
    public void browserclose (){
        driver.quit();
        System.out.println("TestCase : Browser was closed");
    }

由于您在一个 java class 中进行并行执行但不同的方法(例如一个 @Test 注释一个方法)您需要调用 @AfterMethod 钩子关闭所有驱动程序(在每个方法/@Test 注释方法之后)。

你应该完全re-design你的class..首先在设计代码时避免使用静态字段运行并行.静态字段是 class 的 属性,不是对象的。因此被实例化时,它会在所有并行线程之间共享,因此除非同步,否则会导致意外结果。

因此您需要删除 static 修饰符并在 @BeforeTest 中创建您的驱动程序对象(而不是在测试代码中),然后在 @AfterTest.

中退出您的驱动程序