Java 使用单个驱动程序实例的TestNG并行执行
在我的测试套件中,我有一个基类,在这个基类中我声明了一个静态WebDriver,然后在一个打开基本url的方法中生成一个ChromeDriver、FirefoxDriver等。在我所有的测试用例中,我的BeforeMethod中都使用了这个方法,因此在每次测试执行中,以及在测试用例中使用的一系列可重用方法中,都使用了驱动程序的一个静态实例,例如,登录方法 这似乎是一个问题,现在我正试图引入并行执行,因为我似乎需要为我想要同时运行的任何脚本提供单独的驱动程序实例,以避免线程干扰。有没有什么方法可以使我的脚本并行运行,而不必重新构造整个套件,使每个脚本完全独立编写 我尝试声明一个WebDriver数组,然后根据LaunchSuite XML发送的参数选择一个特定的索引,但这没有帮助。第二个浏览器打开,但没有发生任何事情 这是我的基类:Java 使用单个驱动程序实例的TestNG并行执行,java,selenium,testng,Java,Selenium,Testng,在我的测试套件中,我有一个基类,在这个基类中我声明了一个静态WebDriver,然后在一个打开基本url的方法中生成一个ChromeDriver、FirefoxDriver等。在我所有的测试用例中,我的BeforeMethod中都使用了这个方法,因此在每次测试执行中,以及在测试用例中使用的一系列可重用方法中,都使用了驱动程序的一个静态实例,例如,登录方法 这似乎是一个问题,现在我正试图引入并行执行,因为我似乎需要为我想要同时运行的任何脚本提供单独的驱动程序实例,以避免线程干扰。有没有什么方法可以
public class Base {
public static WebDriver driver = null;
//Input client under test
public static final String Client = "Client1";
//Input default environment under test
public static final String DefaultEnvironment = "Chrome_Hub";
//CALL WEB BROWSER AND OPEN WEBSITE
public static void openURL(String environment) throws InterruptedException, IOException {
try{
if(environment.equals("Chrome_Hub")) {
System.setProperty("webdriver.chrome.driver", "/Users/rossdonohoe/Desktop/SeleniumJava/Drivers/chromedriver");
driver = new ChromeDriver();
}
if(environment.equals("Firefox_Hub")) {
System.setProperty("webdriver.gecko.driver", "/Users/rossdonohoe/Desktop/SeleniumJava/Drivers/geckodriver");
driver = new FirefoxDriver();
}
driver.get(DataFinder.ReadData("front end url"));
}
catch(Exception E) {
E.printStackTrace();
}
}
}
下面是一个示例脚本:
public class LoginLogoutScript extends Base {
@Test (priority = 2)
public void login() throws InterruptedException, IOException {
LoginLogout.loginFrontEnd();
Assert.assertTrue(driver.getPageSource().contains("My Account") || driver.getPageSource().contains("My Dashboard"));
}
@Parameters({ "Environment" })
@BeforeClass
public void setUp(@Optional(DefaultEnvironment) String Environment) throws InterruptedException, IOException {
Base.openURL(Environment);
}
@AfterClass public void tearDown() {
Base.driver.quit();
}
}
以下是该脚本中调用的方法:
public class LoginLogout extends Base {
public static void loginFrontEnd () throws InterruptedException, IOException {
Thread.sleep(5000);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement login2 = wait.until(ExpectedConditions.elementToBeClickable(By.linkText(DataFinder.ReadData("sign in link"))));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", login2);
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
driver.findElement(By.id("email")).sendKeys(DataFinder.ReadData("username entry"));
driver.findElement(By.id("pass")).sendKeys("I$$\"Cnewton1");
Thread.sleep(5000);
driver.findElement(By.id("send2")).click();
}
}
下面是另一个脚本:
@Listeners(CustomListener.class)
public class CreateDeleteCustomerAccountScript extends Base {
@Test (priority = 1)
public void createAccount() throws InterruptedException, IOException {
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText(DataFinder.ReadData("navigate to register account 1"))));
driver.findElement(By.partialLinkText(DataFinder.ReadData("navigate to register account 1"))).click();
driver.findElement(By.cssSelector(DataFinder.ReadData("navigate to register account 2"))).click();
driver.findElement(By.name("firstname")).sendKeys("Testy");
driver.findElement(By.name("lastname")).sendKeys("McTester");
driver.findElement(By.cssSelector("#email_address")).sendKeys(Misc.generateRandomString() + "@thepixel.com");
driver.findElement(By.name("password")).sendKeys("I$$\"Cnewton1");
driver.findElement(By.name(DataFinder.ReadData("password confirmation"))).sendKeys("I$$\"Cnewton1");
//verify account created
Thread.sleep(5000);
Assert.assertTrue(driver.findElement(By.tagName("body")).getText().contains(DataFinder.ReadData("account created message")));
}
@Parameters({ "Environment" })
@BeforeClass
public void setUp(@Optional(DefaultEnvironment) String Environment) throws InterruptedException, IOException {
Base.openURL(Environment, "front end");
}
@AfterClass public void tearDown() {
Base.driver.quit();
}
}
这是我的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="FullRegressionSuite" parallel="tests" thread-count="2">
<listeners>
</listeners>
<test name="Test1">
<parameter name ="DriverNo" value="1"/>
<classes>
<class name="userManagement.LoginLogoutScript"/>
</classes>
</test>
<test name="Test2">
<parameter name ="DriverNo" value="2"/>
<classes>
<class name="userManagement.CreateDeleteCustomerAccountScript"/>"/>
</classes>
</test>
<!-- Test -->
</suite> <!-- Suite -->
您可以使用ThreadLocalWebDriver,如下所示
public class Base {
public static ThreadLocal<WebDriver> driver = new ThreadLocal<>();
//Input client under test
public static final String Client = "Client1";
//Input default environment under test
public static final String DefaultEnvironment = "Chrome_Hub";
//CALL WEB BROWSER AND OPEN WEBSITE
public static void openURL(String environment) throws InterruptedException, IOException {
try{
if(environment.equals("Chrome_Hub")) {
System.setProperty("webdriver.chrome.driver", "/Users/rossdonohoe/Desktop/SeleniumJava/Drivers/chromedriver");
driver.set(new ChromeDriver());
}
if(environment.equals("Firefox_Hub")) {
System.setProperty("webdriver.gecko.driver", "/Users/rossdonohoe/Desktop/SeleniumJava/Drivers/geckodriver");
driver.set(new FirefoxDriver());
}
driver.get(DataFinder.ReadData("front end url"));
}
catch(Exception E) {
E.printStackTrace();
}
}
}
如果您通过Maven执行测试,您可以重新考虑,这样每个TestNG线程都将在一个单独的实例中执行
<forkCount>2</forkCount> <!- amend this to be equal to the number of TestNG threads -->
<reuseForks>false</reuseForks>
然而,它只是掩盖了问题,更大的限制是您正在使用,它肯定违反了这可能有助于作为参考,或者如果您决定进行框架重写,它是一个有用的基础,感谢您指出了真正的问题。。。这只会掩盖问题。
<forkCount>2</forkCount> <!- amend this to be equal to the number of TestNG threads -->
<reuseForks>false</reuseForks>