Cleanup steps for Cucumber scenarios - cucumber

Is there a way to define the cleanup steps for all of the scenarios for a feature in Cucumber? I know that Background is used to define the setup steps for each scenario that follows it, but is there a way to define something like that to happen at the end of each scenario?

should also notice that 'Before' and 'After' is global hooks i.e those hooks are run for every scenario in your features file
If you want the setup and teardown to be run for just few testcases ( grouped by tags) then you need to use taggedHooks, where the syntax is
Before('#cucumis, #sativus') do
# This will only run before scenarios tagged
# with #cucumis OR #sativus.
end
AfterStep('#cucumis', '#sativus') do
# This will only run after steps within scenarios tagged
# with #cucumis AND #sativus.
end
For more info : https://github.com/cucumber/cucumber/wiki/Hooks

You can use an After hook that will run after each scenario:
After do
## teardown code
end
There's also a Before hook that will allow you to set up state and/or test data prior to the scenario:
Before do
## setup code
end
The Before and After hooks provide the functionality of setup and teardown from Test::Unit, and they are generally located in hooks.rb in the features/support directory.

Related

How to read labels in Gitlab CI script

I have a few use cases in my Gitlab setup I would like to be able to support:
If a certain label (let's call it “skip_build”) is set, the deployment steps should not be run when I merge an MR to a main branch. This would be useful when we have multiple MRs being merged right after another and only need the last one built.
If another label (we'll call it “skip_tests”) is set, I should be able to read it as an env var from within the script and alter the flow within the script accordingly (using normal bash syntax), e.g. to alter the package command parameters used a bit. This is useful for small changes where it might not make sense to run a lengthy test suite.
Is this possible with Gitlab, and if so, how?
I’ve tried experimenting with CI_MERGE_REQUEST_LABELS, but it doesn’t seem to be able to read that as an env var from within the script.
You have to use merge request pipelines for the CI_MERGE_REQUEST_LABELS variable (and other MR-related variables) to be present as documented in predefined variables.
You could use a rules: clause to skip jobs. Something like
build:
rules: # only run this job if the regex pattern does not match
- if: $CI_MERGE_REQUEST_LABELS !~ /skip_build/
You can also do this on any other kind of predefined (or user-defined) variable, like branch name, commit messages, MR titles, etc. Whatever works for you.
For example, a built in feature of GitLab is that if your commit message contains [ci skip] it will prevent the pipeline from running. You could implement similar functionality for your jobs and/or pipelines through rules: or workflow:rules:.

How can I execute the same lines of code after all scenarios in specific feature files in Karate?

How can I execute the same few lines of code after every specific scenario?
For example, we have 'Background' defined at the top of our feature files. Is there a 'Footer' or something similar?
Since I only want to execute this code on specific features, not our entire repository, I would need to add it to the specific feature file, but I don't want to add it to every single scenario in the feature file, I just want to add it once.
We have background, which executes at the beginning of each scenario:
Background:
*to start do this
to start do this as well*
I want something like this, to execute at the end of each scenario:
Footer:
*to finish do this
to finish do this as well*
Yes, there is an afterScenario hook: https://github.com/intuit/karate#hooks
Background:
* configure afterScenario = function(){ karate.log('hello') }

Best approach to Ignore Cucumber feature files?

I have a large suite of feature files, and every single scenario is tagged #regression.
After running full regression I realized that some features do not need to be run for the current environment.
What is the best approach to ignore specific scenarios keeping in mind that each scenario is tagged with #regression?
You can use Tags to run certain features/scenarios, or not run them.
To specifically ignore them, see Ignoring a subset of scenarios:
"You can tell Cucumber to ignore scenarios with a particular tag:
Using JUnit runner class:
#CucumberOptions(tags = "not #smoke")
public class RunCucumberTest {}
"

Load Steps into Console?

Is it possible to load the step definitions I have defined into the calabash-android console?
I would like to be able to use them when navigating the app within the console.
Thanks
No from the console you can not run a single step definition.
But you can start execution of a test at a specific line appending parameter to the call to start your test
:<linenumber>
This will start execution of your feature file from that specific line and it will run from there to the end of the file.
So while it is not what you are looking for at least it is something.
Did you try step('<step_name>') method?
To be honest I'm not sure if this will work. I know it's working insinde Ruby methods and step definitions - I wanted to post a comment but I can't with 28 points of reputation ;)
You can also try making ruby methods with code from within the step definition:
Then /^I do something$/ do
some code
goes here
end
def do_something
some code
goes here
# same code as in step definition
end
or just use step method:
def do_something
step('I do something')
end
and then call it in a calabash console (I prefer using binding.pry inside some script rather than calling "pure" calabash-console - it makes me sure that I will have all needed methods included).

Is it possible to incorporate an environment variable into a ruby script for Calabash?

I am testing a feature on an app that requires the user to be a certain age. The only time you see the prompt that asks for your age is once you open the app for the first time and once you log out of the app. I don't want my test to only go through my steps to log in and then log out to be able to see this prompt, but I also don't want to manually reset the data in between tests either. Isn't this why we write scripts? Anyways, before I launch the test, I use the environment variable RESET_BETWEEN_SCENARIOS=1 cucumber features/my_feature.feature. Is there a way that I can use this variable INSIDE of my step definition so that it resets the data on its own once I run the script?
I'm not familiar with Calabash, but it appears to be using cucumber. If that is the case, you could handle the action in a before or after hook which would run before or after each scenario.
Within the features/support folder, add a file hooks.rb
Before() do
if ENV['RESET_BETWEEN_SCENARIOS'] == '1'
#code to reset data
end
end
This could also be run after the scenario by using After() do. The same if/then could be used within a scenario step as well.

Resources