如何使用java从黄瓜中的场景大纲中获取场景名称
How to get scenario name from a scenario outline in cucumber using java
假设我有一个像这样的测试用例-
*Scenario: Facebook login test
GIVEN I am a Facebook user
WHEN I enter my user name & password
THEN login should be successful*
如何从"I am a Facebook user"或"I enter my user name & password"或"login should be successful"对应的步骤定义方法中获取场景名称?
步骤定义方法是-
@Given("^I am a Facebook user$")
public void method1() {
//some coding
//I want to get the scenario name here
}
@When("^I enter my user name & password$")
public void method2() {
//some coding
//I want to get the scenario name here
}
@Then("^login should be successful$")
public void method3() {
//some coding
//I want to get the scenario name here
}
您可以使用 @Before
挂钩获取当前正在执行的 Scenario
对象。
@Before
public void beforeHook(Scenario scenario) {
this.sce = scenario
System......(scenario.getName())
System......(scenario.getId())
}
您可以在步骤定义中访问存储的场景对象。
不@Bappa,这是可能的,虽然你的步骤定义class是单例的并且你的测试是并行的,但通过使用用于存储的线程安全静态哈希映射变量增强它来看到它受到以下方法的攻击:
public class StepDefinitions{
private static HashMap<Integer,String> scenarios;
public StepDefinitions(){ //or even inside of your singleton's getInstance();
if(scenarios == null)
scenarios = new HashMap<Integer,String();
}
@Before
public void beforeHook(Scenario scenario) {
addScenario(scenario.getName());
}
@When("your step definition")
public void stepDefinition1(){
String scenario = getScenario(); //problem-o-solved here...
}
private void addScenario(String scenario){
Thread currentThread = Thread.currentThread();
int threadID = currentThread.hashCode();
scenarios.put(threadID,scenario);
}
private synchronized String getScenario(){
Thread currentThread = Thread.currentThread();
int threadID = currentThread.hashCode();
return scenarios.get(threadID);
}
在步骤定义中,您可以使用CucumberHelper.scenario.getName()
。
基于此 API,您可以使用 getID
、getSourceTagNames
、getStatus
和 getClass
方法。
假设我有一个像这样的测试用例-
*Scenario: Facebook login test
GIVEN I am a Facebook user
WHEN I enter my user name & password
THEN login should be successful*
如何从"I am a Facebook user"或"I enter my user name & password"或"login should be successful"对应的步骤定义方法中获取场景名称?
步骤定义方法是-
@Given("^I am a Facebook user$")
public void method1() {
//some coding
//I want to get the scenario name here
}
@When("^I enter my user name & password$")
public void method2() {
//some coding
//I want to get the scenario name here
}
@Then("^login should be successful$")
public void method3() {
//some coding
//I want to get the scenario name here
}
您可以使用 @Before
挂钩获取当前正在执行的 Scenario
对象。
@Before
public void beforeHook(Scenario scenario) {
this.sce = scenario
System......(scenario.getName())
System......(scenario.getId())
}
您可以在步骤定义中访问存储的场景对象。
不@Bappa,这是可能的,虽然你的步骤定义class是单例的并且你的测试是并行的,但通过使用用于存储的线程安全静态哈希映射变量增强它来看到它受到以下方法的攻击:
public class StepDefinitions{
private static HashMap<Integer,String> scenarios;
public StepDefinitions(){ //or even inside of your singleton's getInstance();
if(scenarios == null)
scenarios = new HashMap<Integer,String();
}
@Before
public void beforeHook(Scenario scenario) {
addScenario(scenario.getName());
}
@When("your step definition")
public void stepDefinition1(){
String scenario = getScenario(); //problem-o-solved here...
}
private void addScenario(String scenario){
Thread currentThread = Thread.currentThread();
int threadID = currentThread.hashCode();
scenarios.put(threadID,scenario);
}
private synchronized String getScenario(){
Thread currentThread = Thread.currentThread();
int threadID = currentThread.hashCode();
return scenarios.get(threadID);
}
在步骤定义中,您可以使用CucumberHelper.scenario.getName()
。
基于此 API,您可以使用 getID
、getSourceTagNames
、getStatus
和 getClass
方法。