Java中黄瓜步骤之间共享状态的实现

时间:2019-03-14 15:28:35

标签: java cucumber cucumber-jvm cucumber-java

我有两页对象,分别称为File INVF021 is newer 2019-03-19 00:00:00 in X:\Directory2\ File INVF032 is newer 2019-03-19 00:00:00 in X:\Directory2\ File SLS0234 is newer 2019-03-19 00:00:00 in X:\Directory2\ File SLS3211 is newer 2019-03-19 00:00:00 in X:\Directory2\ OrderSelection。另外,我有OrderDetails类和SharedStateOrderSelectionStepDef。我在OrderDetailsStepDef中为OrderSelectionOrderDetails声明了两个变量。但是,它们没有在SharedState的构造函数中初始化。     在SharedStateOrderSelectionStepDef类中,我声明了它们的构造函数并传递了OrderDetailsStepDef对象。

SharedState

当我在public OrderSelectionStepDef(SharedState sharedState) { this.sharedState = sharedState; } public OrderDetailsStepDef(SharedState sharedState) { this.sharedState = sharedState; } sharedState.orderDetails内呼叫OrderDetailsStepDef时,抛出了OrderSelectionStepDef

然后,我在NullPointerException构造函数中初始化了OrderSelectionOrderDetails类对象。然后问题解决了。但是,这种实现是否可以采用黄瓜微微容器的概念?。

1 个答案:

答案 0 :(得分:3)

步骤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必须符合您的黄瓜版本

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

希望这会有所帮助。

相关问题