gpt4 book ai didi

java - 为什么 TestNG Selenium 并行方法不起作用?

转载 作者:行者123 更新时间:2023-12-02 06:23:05 24 4
gpt4 key购买 nike

我有大约 13 个测试类,并且此设置可以很好地并行运行类:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Office Suite" thread-count="5" parallel="classes">
<test name="Office Test" allow-return-values="true">
<packages>
<package name="testSuite.*"/>
</packages>
</test>
</suite>

但是,当我尝试在方法级别运行时,我遇到了问题:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Office Suite" thread-count="5" parallel="methods">
<test name="Office Test" allow-return-values="true">
<packages>
<package name="testSuite.*"/>
</packages>
</test>
</suite>

我开始看到org.openqa.selenium.NoSuchElementException:5秒后超时。无法找到该元素

此设置也不起作用:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Office Suite" thread-count="5" parallel="methods">
<test name="Office Test" allow-return-values="true">
<classes>
<class name="testSuite.CustomerProfileBillingTest"/>
<class name="testSuite.CustomerProfileTest"/>
<class name="testSuite.CustomerSearchPageTest"/>
<class name="testSuite.HomePageTest"/>
<class name="testSuite.HomeZoneTest"/>
<class name="testSuite.ImportLogTest"/>
<class name="testSuite.InfleetPageTest"/>
<class name="testSuite.LoginPageTest"/>
<class name="testSuite.MembersArea1Test"/>
<class name="testSuite.MembersArea2Test"/>
<class name="testSuite.MembersArea3Test"/>
<class name="testSuite.PromoPageTest"/>
<class name="testSuite.TicketsTest"/>
</classes>
</test>
</suite>

这是测试:

@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {
DriverFactory.french = false;
WEB_DRIVER_THREAD_LOCAL.set(new EventFiringWebDriver(DriverFactory.getDriver(TestUtil.getTargetBrowser())).register(new WebEventListener()));

on = new Pages(WEB_DRIVER_THREAD_LOCAL.get());
wait = new WebDriverWait(WEB_DRIVER_THREAD_LOCAL.get(), TestUtil.ELEMENT_PRESENT_WAIT);
}

@Test(groups = {"registration"})
public void verifyCustomerCanRectifyError() throws Exception{
on.MembersRegistrationPage()
.typeIn(on.MembersRegistrationPage().firstNameInput, customer.getFirstName())
.click(on.MembersRegistrationPage().continueButton)
.waitForVisibilityOfElement(on.MembersRegistrationPage().requestDrRecordButtonXPATH);

assertTrue(on.MembersRegistrationPage().requestDrRecordHeader.isDisplayed());
}

知道为什么类可以并行化,但方法不能并行化吗?

最佳答案

NoSuchElementException 表示驱动程序未找到元素并且正在超时(这似乎与并行运行测试无关。我怀疑您的问题与实例化的方式有关浏览器,但我真的无法判断,因为您引用了似乎位于其他地方的代码(例如:WEB_DRIVER_THREAD_LOCAL)。这对我来说效果很好(使用 gradle 进行了测试):

public class Sample {
private static ThreadLocal<RemoteWebDriver> drivers = new ThreadLocal<>();

@BeforeMethod
public void setUp() throws MalformedURLException {
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), DesiredCapabilities.chrome());
drivers.set(driver);
}

@Test
public void testOne() {
System.out.println("Launching Thread [" + Thread.currentThread().getId() + "]");
getDriver().get("https://www.google.com");
}

@Test
public void testTwo() {
System.out.println("Launching Thread [" + Thread.currentThread().getId() + "]");
getDriver().get("https://www.stackoverflow.com");
}

@AfterMethod
public void tearDown() {
getDriver().quit();
}

@AfterClass
public void removeThread() {
drivers.remove();
}

private RemoteWebDriver getDriver() {
return drivers.get();
}
}

此外,在您的 gradle 文件中,请确保定义您正在使用的 testng XML 文件的名称:

test {
useTestNG() {
useDefaultListeners = true
suites 'testng.xml' //location of the testng file
}
}

测试NG文件:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >

<suite name="Sample" parallel="methods" thread-count="2" >
<test name="SampleTest" >
<classes>
<class name="Sample" />
</classes>
</test>
</suite>

关于java - 为什么 TestNG Selenium 并行方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55817650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com