Shared WebDriver 在使用 PicoContainer 的第二种情况下变为空

Shared WebDriver becomes null on second scenario using PicoContainer(Shared WebDriver 在使用 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 的第二种情况下变为空

基础教程推荐