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

holgip5t  于 2021-07-12  发布在  Java
关注(0)|答案(3)|浏览(407)

我正在运行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个浏览器窗口并运行测试,但问题是它们永远不会结束。这个 @AfterTest 块只被触发一次,所以其中一个窗口确实关闭了,但其余的窗口仍然打开,我必须手动关闭它们。每次测试完成后,如何关闭/退出每个驱动程序?
编辑:这是我的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>
fslejnso

fslejnso1#

由于您在一个java类中执行并行执行,但方法不同(例如,一个@test annotation one method),因此需要调用@aftermethod hook来关闭所有驱动程序(在每个方法/@test annotation method之后)。

zy1mlcev

zy1mlcev2#

你可以尝试用注解@afterclass而不是@aftertest杀死你的驱动程序。

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

htzpubme3#

你应该完全重新设计你的班级。。首先,在设计并行运行的代码时,避免使用静态字段。静态字段是类的属性,而不是对象的属性。因此,在示例化时,它在所有并行线程之间共享,因此除非同步,否则可能会导致意外的结果。
所以你需要移除 static 修改器并在中创建驱动程序对象 @BeforeTest (不在测试代码中)然后在中退出驱动程序 @AfterTest .

相关问题