Shared WebDriver becomes null on second scenario using PicoContainer(Shared WebDriver 在使用 PicoContainer 的第二种情况下变为空)
问题描述
我已经使用了接受的解决方案
特点:
特征:FeatureA场景:场景A给定什么时候然后场景:场景B给定什么时候然后
基本步长:
公共类 BaseStep {受保护的 WebDriver 驱动程序 = null;私有静态布尔 isInitialized = false;@前公共 void setUp() 抛出异常 {if (!isInitialized) {driver = SeleniumUtil.getWebDriver(ConfigUtil.readKey("browser"));isInitialized = true;}}@后公共无效拆卸(){driver.quit();}}
步骤A:
公共类 StepA {私有BaseStep baseStep = null;私有 WebDriver 驱动程序 = null;//PicoContainer 注入 BaseStep 类公共步骤A(BaseStep baseStep){this.baseStep = baseStep;}@Given("^我在登录页面$")公共无效给定IAmAtTheLoginPage()抛出异常{驱动程序 = baseStep.driver;driver.get(ConfigUtil.readKey("base_url"));}@什么时候@什么时候@然后@然后}
但是,驱动程序在场景 A 的 tearDown()
之后死亡",并在场景 B 的 Given 步骤中变为 null(两种场景都使用相同的 Given).我没有使用 Maven.
是因为这行:
私有静态布尔isInitialized = false;
对于每个场景,cucumber 为每个涉及的步骤文件创建一个新实例.因此,当场景开始时,BaseStep
中的driver
始终为null.
静态 isInitialized
布尔值不是实例的一部分,它绑定到它所在的类,并且在 JVM 关闭之前它一直处于活动状态.第一个场景将其设置为 true
,这意味着当第二个场景开始时它仍然是 true
并且它不会在 setUp()
方法.
您可能希望将 driver
设为静态,以便在两个场景中共享同一个实例.
I have used the accepted solution here and came up with the following code:
Referenced Libraries:
Feature:
Feature: FeatureA
Scenario: ScenarioA
Given
When
Then
Scenario: ScenarioB
Given
When
Then
BaseStep:
public class BaseStep {
protected WebDriver driver = null;
private static boolean isInitialized = false;
@Before
public void setUp() throws Exception {
if (!isInitialized) {
driver = SeleniumUtil.getWebDriver(ConfigUtil.readKey("browser"));
isInitialized = true;
}
}
@After
public void tearDown() {
driver.quit();
}
}
StepA:
public class StepA {
private BaseStep baseStep = null;
private WebDriver driver = null;
// PicoContainer injects BaseStep class
public StepA(BaseStep baseStep) {
this.baseStep = baseStep;
}
@Given("^I am at the Login page$")
public void givenIAmAtTheLoginPage() throws Exception {
driver = baseStep.driver;
driver.get(ConfigUtil.readKey("base_url"));
}
@When
@When
@Then
@Then
}
However, the driver "dies" after tearDown()
of ScenarioA and becomes null on Given step of ScenarioB (both scenarios use the same Given). I am not using Maven.
It's because of this line:
private static boolean isInitialized = false;
For each scenario, cucumber creates a new instance for every step file involved. Hence, the driver
in BaseStep
is always null when a scenario starts.
The static isInitialized
boolean is not part of an instance, it's bound to the class it lives in and it's alive until the JVM shuts down. The first scenario sets it to true
, meaning that when the second scenario starts it's still true
and it does not reinitialize the driver in the setUp()
method.
You probably want to make driver
static to share the same instance with both scenarios.
这篇关于Shared WebDriver 在使用 PicoContainer 的第二种情况下变为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Shared WebDriver 在使用 PicoContainer 的第二种情况下变为空
基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01