[英]Need to execute a step (each feature may have diff step) only once before a Cucumber feature file
I want to execute a specific step only once before each cucumber feature files. 我只想在每个黄瓜特征文件之前执行一次特定步骤。 A cucumber feature files can have multiple scenarios.
黄瓜功能文件可以有多种情况。 I don't want Background steps here which executes before each scenario.
我不希望这里的背景步骤在每种情况下都执行。 Every feature file can have a step (which is different in each feature) which will execute only once.
每个功能文件都可以有一个步骤(每个功能都不同),该步骤仅执行一次。 So i can't use that step into before hook as i have a specific step for every 20 features.
所以我不能在上钩之前使用该步骤,因为我每20个功能都有一个特定步骤。 Sample Gherkin shows below:
小黄瓜示例如下所示:
Scenario: This will execute only once before all scenario in this current feature
When Navigate to the Page URL
Scenario: scenario 1
When Some Action
Then Some Verification
Scenario: scenario 2
When Some Action
Then Some Verification
Scenario: scenario 3
When Some Action
Then Some Verification
I hope you guys understand my Question. 我希望你们能理解我的问题。 I am using Ruby Capybara Cucumber in my framework.
我在我的框架中使用Ruby Capybara Cucumber。
Cucumber doesn't really support what you are asking about. 黄瓜并不真正支持您的要求。 A way to implement this with cucumber hooks would be to use these two pieces of doc:
用黄瓜钩子实现此目的的一种方法是使用以下两个文档:
https://github.com/cucumber/cucumber/wiki/Hooks#tagged-hooks https://github.com/cucumber/cucumber/wiki/Hooks#tagged-hooks
https://github.com/cucumber/cucumber/wiki/Hooks#running-a-before-hook-only-once https://github.com/cucumber/cucumber/wiki/Hooks#running-a-before-hook-only-once
You would tag all your feature files appropriately and you can implement tagged Before hooks that execute once on a per feature tag basis. 您将适当地标记所有功能文件,并且可以实现在每个功能标签的基础上执行一次的,带标签的Before挂钩。
It's not beautiful but it accomplishes what you want without waiting on a feature request (or using a different tool). 它虽然不漂亮,但是可以完成您想要的功能,而无需等待功能请求(或使用其他工具)。
This can be achieved by associating a Before, After, Around or AfterStep hook with one or more tags. 这可以通过将Before,After,Around或AfterStep挂钩与一个或多个标签相关联来实现。 Examples:
例子:
Before('@cucumis, @sativus') do
# This will only run before scenarios tagged
# with @cucumis OR @sativus.
end
This must be in the top 5 most frequent questions on the Cucumber mailing list. 这必须是黄瓜邮件列表上最常见的5个问题。 You can do what you want with hooks.
你可以用钩子做你想做的事。 However you almost certainly should not do what you want.
但是,您几乎可以肯定不应该做您想做的事。 The execution time you save by taking this approach is totally outweighed by the amount of time and effort it will take to debug the intermittent failures that such an approach generally leads to.
通过这种方法节省的执行时间,将远远超过调试这种方法通常会导致的间歇性故障所需的时间和精力。
One of the foundations of creating automated tests is to start from a consistent place. 创建自动化测试的基础之一是从一个一致的地方开始。 When you have code that setups key things in scenarios, but that is not run for every scenario you have to do the following:
当您具有用于设置方案中关键内容的代码,但未在每种方案中都运行该代码时,您必须执行以下操作:
In your example you'd have to ensure that every action in every scenario ends up on your original page URL. 在您的示例中,您必须确保每种情况下的每项操作都以原始页面URL结尾。 If just one scenario fails to do that, then you will end up with intermittent failures, and you will have to go through every scenario to find your culprit.
如果只有一种方案无法做到这一点,那么您将遭受间歇性失败,并且您将不得不仔细检查每种方案来找出罪魁祸首。
In general it is much easier and more effective to put your effort into making your setup code FAST enough so that you are not worried about running it before each scenario. 通常,将设置代码变得足够快变得更加容易和有效,这样您就不必担心在每种情况之前都运行它。
Yes, This can be done by passing the actual value in you feature file and using " (\\\\d+)
" in you java file. 是的,这可以通过在要素文件中传递实际值并在Java文件中使用“
(\\\\d+)
”来完成。 Look at below shown code for better understanding. 请看下面显示的代码,以更好地理解。
Scenario: some test scenario
Given whenever a value is 50
In myFile.java
, write the step definition as shown below 在
myFile.java
,编写步骤定义,如下所示
@Given("whenever a value is (\\d+)$")
public void testValueInVariable(int value) throws Throwable {
assertEqual(value, 50);
}
you can also have a look at below link to get more clear picture: https://thomassundberg.wordpress.com/2014/05/29/cucumber-jvm-hello-world/ 您也可以在下面的链接中查看以获得更清晰的图片: https : //thomassundberg.wordpress.com/2014/05/29/cucumber-jvm-hello-world/
Some suggestions have been given, especially the one quoting the official documentation which uses a global variable to store whether or not initial setup has been run. 给出了一些建议,特别是引用官方文档的建议,该文档使用全局变量来存储是否已运行初始设置。
For my case, where multiple features were executed one after another, I had to reset the variable again by checking whether scenario.feature.name
has changed: 对于我的情况,在多个功能被执行了一个又一个,我不得不通过检查是否再次重置变量
scenario.feature.name
发生了变化:
$feature_name ||= ''
$is_setup ||= false
Before do |scenario|
current_feature_name = scenario.feature.name rescue nil
if current_feature_name != $feature_name
$feature_name = current_feature_name
$is_setup = false
end
end
$is_setup
can then be used in steps to determine whether any initial setup needs to be done. 然后,可以在步骤中使用
$is_setup
来确定是否需要进行任何初始设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.