[Cucumber-JVM] How to identify trigger tag from Scenario api

已查看 25 次
跳至第一个未读帖子

Yun Jing

未读,
2017年7月12日 06:36:082017/7/12
收件人 Cukes
Hi there,

I have a scenario which applies to different sizes of browser. According to the trigger tag, I want to instantiate a different implementation which implements the same interface. 

For example, there are three implementations (LoginSmallImpl, LoginMediumImpl and LoginBigImpl), I would like to instantiate Login login = new LoginSmallImpl(); if the trigger tag is @small.

I have tried the @Before hook, but it applies to all tags on the scenario. I'm wondering if it's possible to detect the trigger tags?

Cheers.


@small @medium @large
Scenario: Login
Given I am on the login page
When I login with a valid username and password
Then login should be successful


public class LoginSteps
{

@Before("@small")
public void small(Scenario sc)
{

}

@Before("@large")
public void big(Scenario sc)
{

}

@Given("^I am on the login page$")
public void i_am_on_the_login_page() throws Throwable {

}

@When("^I login with a valid username and password$")
public void i_login_with_a_valid_username_and_password() throws Throwable {

}

@Then("^login should be successful$")
public void login_should_be_successful() throws Throwable {

}
}

Justin Radcliffe

未读,
2017年7月21日 08:49:232017/7/21
收件人 Cukes
    public static Login runThrough(Scenario scenario) {

        Login login = null;

       
for (String tag : scenario.getSourceTagNames()) {
           
if (tag.startsWith("@small")) {
                login = new LoginSmallImpl();
            }

            if (tag.startsWith("@medium")) {
                login = new LoginMediumImpl ();
            
}
            if (tag.startsWith("@big")) {
                login = new LoginBigImpl();
            
}
       
}

       
return login;
   
}

You could try something like this but you may need to know if you already used one of the tags if you want to test this 3 times with different login types or have 3 scenrios with one of those tags each.


Enter code here...
回复全部
回复作者
转发
0 个新帖子