testng - 获取 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
解决方案
对象沿单个场景持续存在。您在一种情况下设置值并尝试在另一种情况下读取它。这就是你得到null
.
依赖注入允许您在步骤之间共享状态- 而不是在场景之间。
如果您需要为每个场景设置一些东西,则需要处理Background。
推荐阅读
- c# - SystemEvents.SessionSwitch 不会在 ac# wpf 应用程序中触发
- r - 如何在R中的多个调查问题上设置for循环
- apache-flink - Flink:所有流量都流向一个任务管理器(集群中有 1 个作业管理器 + 2 个任务管理器)
- javascript - 将对象数组中的正数更改为负数
- azure-devops - 无法将 Blocked 作为列添加到 Azure Dev Ops 看板
- python - 函数可以有属性吗?
- unity3d - 它是否对 apk 的大小有限制,我在 Unity 上构建了一个 apk。但是当我安装并打开它时它崩溃了。这是我的日志。我的 Apk 大小超过 2gb
- clickhouse - ClickHouse 按列输出,而不是按行输出(实际上没有转置的转置?)
- python - 我的 GUI 中嵌入了一个绘图,当我将鼠标悬停在绘图上时,我想添加一个光标来显示 x 和 y 坐标
- javascript - 在 HTML/JS 和 CSS 中显示一个打开弹出窗口的按钮?