跨测试项目使用cucumber steps 并实现数据共享(封装步骤库)

2018-12-19  文晶 

昨天参考如下内容【https://univers-architecture.com/2018/05/22/cucumber-jvm-reuse-step-spring-boot/】学习了下测试步骤及步骤间测试数据共享的实现方式,但是对于目前测试工作来说不需要一定得依赖spring boot 而且 如果不发布服务的话,上下游业务之间也实现不了步骤共用,数据共享的需求。所以今天继续search 了一下,最终在【

http://toolsqa.com/selenium-cucumber-framework/share-data-between-steps-in-cucumber-using-scenario-context/

】得到了参考解决,不得不感叹能人真多,为自己的一知半解羞愧。如下是未评审版只是自我记录


首先依赖DIjar包



           info.cukes
           cucumber-picocontainer
           1.2.5
           test
       



1、封装TestContext供各个项目组测试依赖存储需要共享数据。TestContext相当于一个父类存活于整个测试活动中。


public class TestContext {
    public ScenarioContext scenarioContext;


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


    public ScenarioContext getScenarioContext() {
        return scenarioContext;
    }
}


ScenarioContext:用于场景存放需要传递数据



public class ScenarioContext {


    private  Map scenarioContext;


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


    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());
    }


}

打包发不成工具包



3、业务测试项目依赖该jar,并在脚本实现步骤中增加构造方法


 TestContext testContext ;


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


    public LoginStepsDefinitions() {
    }


4、在A项目需要save数据地方通过如下方式传递数据



 @Given("login success")
    public void login() {
        testContext.scenarioContext.setContext(Context.USER_NAME, "HEBY");
    }


将   A项目打包发布


5、B项目中需要依赖A项目封装的步骤实现后续测试(登陆例子不是很合理),B项目依赖A的JAR

通过@CucumberOptions的glue属性指定需要使用A项目的步骤包路径如下,缺点:必须同时指定自己当前项目steps位置,否则找不到步骤定义


@RunWith(Cucumber.class)
@CucumberOptions(glue = {"steps.loginfo","stepsdefine.homeinfo"} ,features = "src/test/features/", plugin = {"pretty", "html:target/cucumber",
        "json:target/cucumber.json"}, monochrome = true)
public class IntergrationTesthome {
}


6、B项目feature文件中需要明确的写出要引用步骤



Feature: go to home page


Scenario: after login success then should be redirected to the home page


Given login success
Then i should be redirected to the home page
And i should be greeted


7、B项目的steps 步骤中需要引入TestContext



public class HomePageSteps {
    private final Logger log = Logger.getLogger("HomePageSteps");
    TestContext testContext;
    public HomePageSteps(TestContext context) {
        testContext =context;
    }


    public HomePageSteps() {
    }


    @Then("^i should be redirected to the home page")
    public void redircttohome() {
        log.info("i have got home page");
    }


    @And("^i should be greeted")
    public void greeted() throws Exception {
        log.info("wellcome: "+testContext.getScenarioContext().getContext(Context.USER_NAME));


    }
}


运行B项目,可在日志中看到A项目set的username值

项目地址:git@github.com:faffenjingbo/reuse-steps-between-Projects-with-cucumber-.git


395°/3956 人阅读/0 条评论 发表评论

登录 后发表评论