<?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>
<?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>
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();
}
}