gpt4 book ai didi

java - java中 cucumber 步骤间共享状态的实现

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:29 26 4
gpt4 key购买 nike

我有一个名为 OrderSelectionOrderDetails 的两页对象。此外,我还有 SharedState 类以及 OrderSelectionStepDefOrderDetailsS​​tepDef。我在 SharedState 中为 OrderSelectionOrderDetails 声明了两个变量。但是,它们没有在 SharedState 的构造函数中初始化。 在 OrderSelectionStepDefOrderDetailsS​​tepDef 类中,我声明了它们的构造函数并传递了 SharedState 对象。

public OrderSelectionStepDef(SharedState sharedState) {
this.sharedState = sharedState;
}
public OrderDetailsStepDef(SharedState sharedState) {
this.sharedState = sharedState;
}

当我在 OrderDetailsS​​tepDefOrderSelectionStepDef 中调用 sharedState.orderDetails 时,抛出了一个 NullPointerException

然后,我在 SharedState 构造函数中初始化了 OrderSelectionOrderDetails 类对象。然后问题就解决了。但是这个实现是否符合 Cucumber pico 容器的概念?

最佳答案

第 1 步。OrderSelectionStepDef 和 OrderDetailsS​​tepDef 如下所示(请根据您的实现更改名称)

/**
* Step Definition implementation class for Cucumber Steps defined in Feature file
*/

public class HomePageSteps extends BaseSteps {

TestContext testContext;

public HomePageSteps(TestContext context) {
testContext = context;
}

@When("^User is on Brand Home Page (.+)$")
public void user_is_on_Brand_Home_Page(String siteName) throws InterruptedException {
homePage = new HomePage().launchBrandSite(siteName);
testContext.scenarioContext.setContext(Context.HOMEPAGE, homePage);
}

@Then("^Clicking on Sign In link shall take user to Sign In Page$")
public void clicking_on_Sign_In_link_shall_take_user_to_Sign_In_Page() {
homePage = (HomePage) testContext.scenarioContext.getContext(Context.HOMEPAGE);
signInPage = homePage.ecommSignInPageNavigation();
testContext.scenarioContext.setContext(Context.SIGNINPAGE, signInPage);
}

供您引用

public class BaseSteps {

protected HomePage homePage;
protected PLPPage plpPage;
protected PDPPage pdpPage;
protected ShoppingBagPage shoppingBagPage;
protected ShippingPage shippingPage;

More implementation goes here.....

}

第 2 步。请在您的框架下添加以下 2 个类 -

首先,Java文件名——ScenarioContext.java

public class ScenarioContext {

private Map<String, Object> scenarioContext;

public ScenarioContext(){
scenarioContext = new HashMap<String, Object>();
}

public void setContext(Context key, Object value) {
scenarioContext.put(key.toString(), value);
}

public Object getContext(Context key){
return scenarioContext.get(key.toString());
}

public Boolean isContains(Context key){
return scenarioContext.containsKey(key.toString());
}
}

二、Java文件名——TestContext.java

public class TestContext {

public ScenarioContext scenarioContext;

public TestContext(){
scenarioContext = new ScenarioContext();
}

public ScenarioContext getScenarioContext() {
return scenarioContext;
}
}

第 3 步。POM 依赖性 - picocontainer 应根据您的 cucumber 版本

   <dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>${cucumber.version}</version>
</dependency>

希望这对您有所帮助。

关于java - java中 cucumber 步骤间共享状态的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55166364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com