首页 > 解决方案 > 获取 Scenario Context- cucumber pico 容器的空值

问题描述

尝试在第二种情况中给出的步骤中获取值时获取空值。但是,当我尝试相同的步骤时,我能够获得价值。

Feature File
Feature: 01_Permission_Accept

  Scenario: 01_Is AUT installed and click on chapter 1
    Given Check AUT is installed  --> Here I am setting the value to context
    When Click on chapter - Chapter 1: App Fundamentals --> Here the value is retrieved

  Scenario: 02_Click on chapter 2
    When Click on demo chapter - Chapter 2: User Interface --> Here the value is null

定义文件:

public class MainScreenStepDefs {

TestContext testContext;

public MainScreenStepDefs(TestContext context) {
    this.testContext = context;
}

@Given("Check AUT is installed")
public void checkAUTIsInstalled() {
    boolean abc = testContext.getAppiumDriver().isAppInstalled(testContext.getConfiguration().androidAppPackage());
    SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(abc).as("Application \"" +testContext.getConfiguration().androidAppPackage()+"\" is not installed.").isTrue());
    testContext.getScenarioContext().setContext(ContextEnum.DEMO, "demo"); // Setting value to context
}

@When("Click on chapter - Chapter 1: App Fundamentals")
public void clickOnChapter1() {
    testContext.getPageObjectManager().getMainScreen().selectChapter1();
    String abc  = (String) testContext.getScenarioContext().getContext(ContextEnum.DEMO);
    System.out.println("Data :--------------- \t" + abc); //value getting successfully
}

@When("Click on demo chapter - Chapter 2: User Interface")
public void clickOnDemoChapter2() {
    testContext.getPageObjectManager().getMainScreen().selectChapter2();
    String abc  = (String) testContext.getScenarioContext().getContext(ContextEnum.DEMO);
    System.out.println("Data :--------------- \t" + abc); // ----null value
}}

测试运行器文件。我正在使用 TestNg 作为 Runner。在@Beforesuite 里面我打电话给司机。这里使用@BeforeSuite,以避免在每个场景之后关闭测试。

使用 Lombok 作为 getter 和 setter。

public class TestRunner extends AbstractTestNGCucumberTests {

    @Getter(AccessLevel.PROTECTED)
    private DriverManager mobileDriverManager;

    public static AppiumDriver<MobileElement> appiumDriver; // making static so this can be accessed directly
    Configuration configuration = ConfigurationManager.getConfiguration();

    @BeforeSuite(alwaysRun = true)
    public void setup() {
        mobileDriverManager = DriverFactory.getMobileDriverManager(DeviceTypeEnum.ANDROID);
        appiumDriver = mobileDriverManager.getMobileDriver(configuration.androidPlatformAndVersion(), configuration.androidDeviceUDID(), configuration.androidSystemPort(), AutomationName.ANDROID_UIAUTOMATOR2);
    }

    @AfterSuite(alwaysRun = true)
    public void tearDown() {
        getMobileDriverManager().quitAppiumDriver();
    }
}

TestContext.java 在初始化 PageObjectManager 之前,我设置了 AppiumDriver。不这样做会出错。

public class TestContext {

    @Getter @Setter
    private AppiumDriver<MobileElement> appiumDriver;

    @Getter
    private ScenarioContext scenarioContext;

    @Getter
    private PageObjectManager pageObjectManager;

    @Getter
    private Configuration configuration = ConfigurationManager.getConfiguration();

    public TestContext() {
        setAppiumDriver(TestRunner.appiumDriver); // This gives error, If I remove this.
        pageObjectManager = new PageObjectManager(getAppiumDriver());
        scenarioContext = new ScenarioContext();
    }
}

请让我知道我是否在执行正确的 pico-container 实现。

这是示例应用程序 github 链接:https ://github.com/dipakkumar1225/DemoCucumberPicoContainerTestng.git

标签: testngcucumber-jvmcucumber-javapicocontainer

解决方案


对象沿单个场景持续存在。您在一种情况下设置值并尝试在另一种情况下读取它。这就是你得到null.

依赖注入允许您在步骤之间共享状态- 而不是在场景之间。

如果您需要为每个场景设置一些东西,则需要处理Background


推荐阅读