为 Scenario Context-cucumber pico 容器获取空值
Getting null value for the Scenario Context- cucumber pico container
尝试在第二种情况下给出的步骤中获取值时获取空值。但是,当我尝试相同的步骤时,我能够获得价值。
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 link:https://github.com/dipakkumar1225/DemoCucumberPicoContainerTestng.git
对象沿单个场景持久化。您在一种情况下设置值并尝试在另一种情况下读取它。这就是为什么你得到 null
.
Dependency Injection allows you to share the state between steps - not between scenarios.
如果您需要为每个场景设置一些东西,您需要处理 Background。
尝试在第二种情况下给出的步骤中获取值时获取空值。但是,当我尝试相同的步骤时,我能够获得价值。
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 link:https://github.com/dipakkumar1225/DemoCucumberPicoContainerTestng.git
对象沿单个场景持久化。您在一种情况下设置值并尝试在另一种情况下读取它。这就是为什么你得到 null
.
Dependency Injection allows you to share the state between steps - not between scenarios.
如果您需要为每个场景设置一些东西,您需要处理 Background。