在使用 Spring 使用 testNG(Selenium) 运行测试时无法维护两个并行会话?



我在使用 Spring 框架使用 TestNG 和 Selenium 运行简单测试时遇到问题,问题是它无法启动两个并行会话(没有并行它工作正常没有任何问题(。 总体目标是启动两个并行浏览器,每个会话由springIOC管理。没有 Spring 使用静态 threadLocal 很容易做到,但是对于 spring,我想维护两个由 spring 本身管理的独立 IOC 容器。

请帮助我解决此问题。代码可在以下链接中找到, https://github.com/priyankshah217/AutomationFrameworkUsingSpringDI.git

测试.xml

<test name="search-engine-test">
<classes>
<class name="com.test.framework.tests.TestAmazonWeb"/>
</classes>
</test>

测试配置.java

@Configuration
@ComponentScan("com.test.framework")
public class TestConfig {
WebDriver webDriver;
@Bean
public WebDriver getDriver() {
if (webDriver == null || ((ChromeDriver) webDriver).getSessionId() == null) {
webDriver = new ChromeDriver();
}
return webDriver;
}
}

基础测试.java

@ContextConfiguration(classes = {TestConfig.class})
public class BaseTest extends AbstractTestNGSpringContextTests {
@Autowired
private WebDriver webDriver;
@AfterMethod
public void tearDown() {
webDriver.quit();
}
}

谷歌主页.java

@PageObject
public class GoogleHomePage extends BasePage {
@FindBy(name = "q")
private WebElement searchTextbox;
public void enterGoogleSearch() {
hardWait();
searchTextbox.sendKeys("Selenium");
searchTextbox.sendKeys(Keys.RETURN);
}
}

所有 Page 对象都是带有 WebDriver 的弹簧组件(自动连线(。

代码库中存在一些阻止并发支持的问题。

  1. autowiredWebDriver实例应已定义为范围prototype而不是singleton(这是默认范围(。这样,每个页面对象基本上都会获得自己的 WebDriver 实例。不过,当您的@Test方法跨越多个页面时,这在某种程度上是一种令人费解的方法。
  2. 如果您要使用原型范围的 Web 驱动程序,您处理
  3. 后处理的方式(其中您正在初始化页面工厂(也必须更改,因为您现在负担不起自动连接 Web 驱动程序的费用,但现在必须从页面对象类中提取它。

为了以更易于理解的方式解释这些更改,我针对您的存储库提出了一个拉取请求。

请参阅 https://github.com/priyankshah217/AutomationFrameworkUsingSpringDI/pull/2

了解全套更改。

最新更新