我用testng的并行测试,其中一个浏览器运行,测试成功。其他浏览器打开但无法继续

问题描述 投票:0回答:0

**我的 xml 是这样的。**在所有代码不并行时完全准确地工作

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="tests" thread-count="2">
    <test verbose="2" preserve-order="true" name="Test1">
        <parameter name="browser" value="Edge"/>
        <classes>
            <class name="ThreadLokal.Tests">
                <methods>
                    <include name="test1"/>
                </methods>
            </class>
        </classes>
    </test>
    <test verbose="2" preserve-order="true" name="Test2">
        <parameter name="browser" value="edge"/>
        <classes>
            <class name="ThreadLokal.Tests">
                <methods>
                    <include name="test1"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

我的 POM 是这样的。 在所有代码不并行时完全准确地工作

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>B2TestNG</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.8.0</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.7.1</version>
        </dependency>

        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.3.2</version>
        </dependency>

        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>5.0.9</version>
        </dependency>

    </dependencies>


</project>

我的 ThreadLocal 驱动程序类是这样的。 在所有代码不并行时完全准确地工作

package Utils;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;

import static Utils.Browsers.CHROME;

public class ThreatDriver {
    //private static WebDriver driver;
    private static ThreadLocal<WebDriver> drivers = new ThreadLocal<>();

    public static WebDriver getDriver(){
        return getDriver(Browsers.CHROME);
    }

    public static WebDriver getDriver(Browsers browser){
       
        if (drivers.get() == null){
            switch (browser){
                case FIREFOX:
                    WebDriverManager.firefoxdriver().setup();
                    //driver = new FirefoxDriver();
                    drivers.set(new FirefoxDriver());
                    break;
                case EDGE:
                    WebDriverManager.edgedriver().setup();
                    drivers.set(new EdgeDriver());
                    break;
                default:
                    WebDriverManager.chromedriver().setup();
                    ChromeOptions options = new ChromeOptions();
                    options.addArguments("--remote-allow-origins=*");
                    drivers.set(new ChromeDriver(options));
                    break;
            }
        }
        return drivers.get();
    }

    public static void quitDriver(){
        if (drivers.get() != null) {
            drivers.get().quit();
            drivers.set(null);
        }
    }

}

我的基础测试是这样的。 在所有代码不平行时完全准确地工作

public class BaseTest {

static WebDriver driver;
static WebDriverWait wait;
    @BeforeTest
    @Parameters("browser")
    public void setup(@Optional String browser){
        if (driver==null) {
            driver=ThreatDriver.getDriver(Browsers.valueOf(browser.toUpperCase()));
        }
        driver.manage().window().maximize();
        wait=new WebDriverWait(driver, Duration.ofMillis(10_000));
        Element.driver=driver;
        Element.wait=wait;
    }

    @AfterTest
    public void tearDown() {
       driver.quit();
    }
}

我的测试是这样的。

public class Tests extends BaseTest {


    @Test
    public void test1() throws InterruptedException {
        _open(URL);
        Thread.sleep(2000);
        waitFor(driver,Conditions.urlContains,null,"login");
        $(xpath(ALL_INPUT,"Username")).sendKeys("Admin");
        $(xpath(ALL_INPUT,"Password")).sendKeys("admin123").submit();
        waitFor(driver,Conditions.urlContains,null,"index");
        $(xpath(ALL_SPAN_2,"Admin")).click();

    }

}

有时,当我在运行的浏览器中执行 sendkeys 时,我看到它发送了一个变量两次。这就像并行运行,但在一个浏览器中有两个驱动程序。

multithreading maven selenium-webdriver testng testng.xml
© www.soinside.com 2019 - 2024. All rights reserved.