karate api testing - How to read tag names from command line to feature file - karate

karate api testing - How to read tag names from command line to feature file
My feature file
Feature: validating tag name reading from maven command line
Background:
Given url baseURL
When param validation = I want to read tagname here
Then method get
Then status 200
#com_status #all #I want to read tagname here
Scenario Outline: Testing tag input scenarios
print I want to read tagname here
Command - mvn clean test -Dtest=Runner -DargLine="-Dkarate.env=dev" -Dcucumber.options="--tags #com_status"

Below is the command to execute the tag from the command line in Karate Api automation testing.
mvn test -DargLine="-Dkarate.env=e2e" "-Dkarate.options=--tags #user_management_get_vender_types"

You cannot. Tags are designed to be passed on the command line to filter scenarios to run - and cannot be retrieved within a test. You can retrieve the tag of a Scenario though: https://github.com/intuit/karate#karate-tags
You can try using karate.properties or something similar to retrieve what was passed on the command-line: https://github.com/intuit/karate#dynamic-port-numbers
Command:
mvn clean test -DcustomName=foo
Feature:
* def customName = karate.properties['customName']
Feel free to contribute this feature if you think it is important.
EDIT - update in 1.1.0 onwards, there is a new feature "Exvironment Tags" that may solve what is being asked for here: https://github.com/intuit/karate#environment-tags
Also see: https://stackoverflow.com/a/50693388/143475

Related

Karate testcases for fetch and update service [duplicate]

Consider there are 100 scenarios and I want to run 99 scenarios in prod environment and 100 on stage environment .
Is there a way to achieve this in karate ?
Things I tried
1. Create a feature file with 1 scenario and another feature file with remaining 99
2. Add tag to the 1 scenario file
3. Ignore that while running
But then when I use it in jenkins job I have to run one command to run on both machines so would not work
The best solution for this case is the karate.abort() API:
So in the "special" scenario #100 - you can do this:
Scenario:
* eval if (karate.env == 'prod') karate.abort()
# normal scenario steps
Please note that there are advanced options for tag selectors in Karate 0.9.0 onwards - but just stick to the solution above please :)
Tag the 100th scenario with #hundred and just run the following command when you want to run 99 scenarios
mvn test -Dkarate.options="--tags ~#hundred"
And simply run mvn test when you want to run all tests.
You can tag a scenario
#hundred
Scenario: the scenario only played in one case
Given <...>
But you can also tag a Feature
#hundred
Feature: The feature containing the scenario only played in one case
Background:
* <..>
Scenario: <...>
Edit after your answer :
You could use a second runtime variable :
mvn test -Dkarate.options="--tags ~#{variable2}" -Dkarate.env={variable}
Or even use the same runtime variable :
mvn test -Dkarate.options="--tags ~#{variable}" -Dkarate.env={variable}
And that maybe wouldn't be the best solution, but you can add #Prod to the 99 scenarios, and #Stage to all of them, and switch the command to this :
mvn test -Dkarate.options="--tags #{variable}" -Dkarate.env={variable}
It's a bit longer to do, but at least the tag(s) on each feature/scenario will correspond to the case(s) in which they are launched

Choosing spec feature file in wdio at runtime

I want to run different feature files based and want to decide it at runtime, i.e via command line arguments.
Everytime I uncomment the file and then run the test.
Tried working with cucumber tags and did not get around it.
specs: [
// 'features/subscription/create.feature'
// './features/payment/create.feature'
],
Is there any simple way to do this?
There are two ways as far as i know:
Defining suites with required feature files and pass the suite name as parameter to WDIO test.
Detailed explanation on suites: https://webdriver.io/docs/organizingsuites.html
NOTE: In case if you are using npm test for starting the test, then use npm test -- --suite login to pick a suite.(this is not mentioned in the file).
You can directly pass in the features through command line as like below:
In you wdio.conf.js file write the below lines above the exports.config and parameter the spec value.
var features = process.env.FEATURE || './features/**/*.feature';
var featureArray = features.split(',');
exports.config = { .... spec: featureArray, ....} //skipped others
now while triggering the test use the command like below:
FEATURE='./features/test.feature,./features/test1.feature' npm test
So when the execution begins, features will receive the string and we converted that to array and passing as parameter to spec.
Hope this helps.

How to ignore a particular scenario in a feature file in karate at runtime?

#tag is per feature file , But Is there a way we can ignore a particular scenario in a feature file while running tests ?
You can provide a tag above your scenario and set it to ignore that tag while running
#ignore
Scenario:
* print "ignored test"
#regression
Scenario:
* print "regression test"
now if you want to ignore #ignore tag then you can use a '~' symbol before tags which you don't want to run in your cucumber options
eg:
i) passing as arguments
mvn clean test -Dcucumber.options="--tags ~#ignore" -Dtest=YourTestClass
or
ii) Defining in your test class itself
#CucumberOptions( tags = {"~#ignore"})
or
iii) for karate standalone pass additional argument with -t flag
-t ~#ignore

How to run single cucumber scenario by name

I'm asking for help on how to run a feature file scenario just by name. I've been trying for a while and it does not come out. I know that can be done by tags or by line number, but I wonder if we can run a cucumber test by name, more or less with this nomenclature.
Given a file named "features/test.feature" with:
Feature:
Scenario: My first scenario
Given this step is blah blah blah
Scenario: My second scenario
Given this step too blah blah
I want to run a scenario by name from the console or with gradle, maybe similar this way
cucumber features/test.feuture::My second scenario
Or maybe with gradle
./gradlew cucumber::My second scenario
You didn't describe how you start cucumber so I can't help you with that.
When used from the CLI accepts --name REGEXP. This will only run scenarios whose names match REGEXP.
The #CucumberOptions annotation accepts name="REGEXP".
Cucumber < v6.0.0 looks at the environment. For maven you can add -Dcucumber.options=--name REGEXP. I don't know the equivalent for gradle. Take note that the escape characters maybe shell/build system dependent.
Cucumber v6.0.0 and above looks at the environment. For maven you can add -Dcucumber.filter.name="REGEXP".
See:
https://cucumber.io/docs/reference/jvm#running
https://github.com/cucumber/cucumber-jvm/tree/main/core
From cucumber 6.x, you can run a scenario with below CLI commands:
// Specify a scenario by its line number
$ cucumber-js features/my_feature.feature:3
// Specify a scenario by its name matching a regular expression
$ cucumber-js --name "topic 1"
But, these are time-consuming and repetitive. You can save a lot of time by using a dedicated VSCode Extension called Cucumber-Quick. This extension will allow you to run a scenario/feature just by right-clicking on them. It can save you from all the hustle.
You would call the scenario by its line number.
So assuming that the second scenario starts on line 5 in your feature file you could run:
cucumber features/test.feature:5

IntelliJ Idea + cucumber run configuration: how to exclude #tags

Good news everyone.
I don't know how to exclude several tags within my run configuration for Cucumber project.
For instance, i have three tests.
#debug
Scenario outline: foo 1
When step
Then step
#obsolete
Scenario outline: foo 2
When step
Then step
#stable
Scenario outline: foo 3
When step
Then step
I put ~#debug,~#obsolete to the textbox called «Tags Filter» of the «Run configuration» dialog window.
But there's something wrong because when I start my tests — foo 1, foo 2 and foo 3 are starting anyways.
I have found the answer to this question within a comment to another stackoverflow question: https://stackoverflow.com/a/23743258.
Basically to add several tags to IntelliJ you need to add -Dcucumber.options="--tags ~#debug --tags ~#obsolete" to the VM options in the default cucumber settings.
For IntelliJ: Add below to the VM options by editing configuration
-Dcucumber.options="--tags #debug"
I don't know about IntelliJ, but on a terminal you would run:
cucumber --tags ~#debug --tags ~#obsolete
Specyfing --tags <your_tag_name> as Program arguments in the Cucumber run cofiguration resolved the issue in my case.
Add tags in cucumber options
#CucumberOptions(plugin = {"pretty"}, tags = {"#Runme"}, features = "src/test/resources/urpath", glue = "your.path")
Hence #CucumberOptions is obsolete with JUnit5 you have to use
#ExcludeTags({ "debug", "obsolete" })
instead.