我的挑战是我有两种不同类型的测试,使用Cucumber BDD与Java,Maven和JUnit一起运行。
在与UI相关的几个功能中,我需要在每个场景之前执行一些操作,例如启动VM,如下所示:
public class StepDefinitions {
@Before
protected void setUp(Scenario scenario) throws MalformedURLException {
//Create browser resources here for all of my UI related scenarios
}
但是,在非UI测试中,例如API测试,我不需要旋转这些浏览器。因此,我真的需要一个名为setUp的@Before方法的不同行为。
我面临的挑战是,似乎@Before钩子适用于每一个测试方法,即使这些方法在不同的类中。因此,无论我尝试什么,都会始终创建浏览器资源,即使对于不需要浏览器的API测试也是如此。
这是我尝试过没有成功的:
有没有办法以自动方式更改setUp的行为,以便它执行/不执行基于测试类型(API / UI)的适当逻辑?
您可以使用标记挂钩执行此操作:“可以根据场景的标记有条件地选择挂钩执行。要仅针对某些场景运行特定挂钩,您可以将挂钩与标记表达关联。”来自docs。
Feature File :- Hainvg 2 Scenarios, one for UI and other one for API
@UI
Scenario: This is First UI Scenario running on chrome browser
Given this is the first step
When this is the second step
Then this is the third step
@Non-UI
Scenario: This is First Non-UI Scenario running on chrome browser
Given this is the first step
When this is the second step
Then this is the third step
------------------------------------------ Hook Implementation ------------------------------------------
@Before("@UI")
public void beforeUISetup(){
Do here :- In several features, related to the UI, I need to perform some actions before every single scenario such as spinning up VMs
}
@Before("@Non-UI")
public void beforeNon-UIScenario(){
Do here :- in non-UI tests, such as API tests, I don't need those browsers to be spun up
}
如果您需要首先运行非UI @Before方法,那么我们也可以设置这些@Before的顺序。