在我的测试套件中,我有一个基类,在该基类中声明一个静态WebDriver,然后以打开基URL的方法将其生成为ChromeDriver,FirefoxDriver等。在我的所有测试用例中,此方法都用在我的BeforeMethod中,因此,在每次测试执行中,以及在测试用例中使用的一系列可重用方法(例如,登录方法)中,都使用单个静态驱动程序实例。 / p>
现在,我试图引入并行执行,这似乎是一个问题,因为对于我想同时运行的任何脚本,我都需要单独的驱动程序实例,以避免线程干扰。有什么方法可以使我的脚本并行运行而无需重新构建整个套件,从而使每个脚本完全独立地编写?
我尝试声明一个WebDrivers数组,然后根据从启动套件XML发送的参数选择一个特定的索引,但这没有帮助。第二个浏览器打开,但是里面什么也没有。
这是我的基础课:
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 -->
答案 0 :(得分:1)
如果您通过Maven执行测试,则可以重新考虑Fork Options,以便每个TestNG线程将在单独的JVM实例中执行
<forkCount>2</forkCount> <!- amend this to be equal to the number of TestNG threads -->
<reuseForks>false</reuseForks>
无论如何,它只能掩盖问题,更大的限制是您正在使用static modifier for WebDriver instance,它肯定违反了Parallel Testing Best Practices
答案 1 :(得分:0)
您可以使用如下所示的threadlocal webdriver。
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();
}
}
}
您可以使用driver.get()方法获取它的实例。
public class LoginLogoutScript extends Base {
@Test (priority = 2)
public void login() throws InterruptedException, IOException {
LoginLogout.loginFrontEnd();
Assert.assertTrue(driver.getPageSource().contains("My Account") || driver.get().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.get().quit();
}
}