Print scenario name when running each scenario - api

I want to print the name of each scenario as the test run. What can i call or do to get the name so that i can execute * print <scenario_name> ?
The answer for this post is exactly what i want to do: Print scenario name Is there a way to access the Scenario object?

As of now this is not supported, but will be easy to add. But here's the question - is this just to help you make sense of the logs ? Because if you are not using the Cucumber HTML report yet, you should - and that's what most teams are using: https://twitter.com/KarateDSL/status/899671441221623809
Refer to this discussion for more: https://stackoverflow.com/a/47555173/143475
If you still really need this, kindly raise a feature request.
Edit: this will be available in the next version: https://github.com/intuit/karate/issues/257

Related

How to switch to an iframe whose id is stored in a variable in karate? [duplicate]

Api returns a confirmationNumber, which i'm storing in a variable then using that confirmationNumber to click and assert on the UI page.
def myFeature = call read('Feature1.feature')
def confirmationNumber = myFeature.confirmationNumber
//some ui login steps are here
Then input('#paymentNum',confirmationNumber) //--successfully entered confirmationNumber
And assert exists('{div}confirmationNumber'). //--not successful. not able to read the stored value
And click('{a}confirmationNumber') //--not successful. not able to read the stored value
Please help
Thank you
Easy. Think of anything within the ( and ) as pure JavaScript. Also please note the extra exists, hmm maybe I should re-design that part of the API - and thanks for asking this question ! https://github.com/intuit/karate/issues/1148
# this is how to use exists() right now, but I have decided to change this !
And assert exists('{div}' + confirmationNumber).exists
And click('{a}' + confirmationNumber)

Is there a way to assign a unique "ID Key" to each feature/scenario in Karate DSL

I would like to know if it is possible to assign "ID Keys" to each feature or scenarios in Karate DSL in order to keep all of them uniquelly referenced.
When you have hundreds of features/scenarios seems very difficult to keep all this list tidy . The name of the scenarios does not seem to be a valid unique reference as I am including variable data in its name.
It is probably best that you use a combination of the feature file name and the "scenario ref id" which looks like this: [1.4] or [2.1:10].
In the upcoming 1.0 / RC version, have a look at the values of karate.scenario and karate.feature when you are within a test execution: https://github.com/intuit/karate/wiki/1.0-upgrade-guide#karateinfo-deprecated
The other thing you can consider is add tags to every scenario: https://github.com/intuit/karate#tags
#id=001
Scenario: some name
* print karate.scenario.name
That's all we have right now, it may need you / the community to contribute if you have some ideas for the future.

Karate API Testing - Access variable value across scenario in a feature file

I am running into this situation (similar to this) when trying to do the following:
Scenario: Create User
Given path 'user'
And request read('user.json');
When method post
Then status 200
And def user_id = response.userId
Scenario: Test A
Given path 'user/' + user_id <- Received javascript error here
...
Scenario: Test B
Given path 'user/' + user_id <- Received javascript error here
...
Scenario: Test A
Given path 'user/' + user_id <- Received javascript error here
...
Basically what i am trying to do is create a user in my database first, then run it through a series of test and use a last scenario to delete this user. Therefore, i need to share the user_id value across multiple scenarios. Background doesn't work for me because that means i have to create a new user for each scenario. I have seen in the demo that a simple way would be to put all test under 1 scenario but i don't feel that it is right to put all tests on 1 scenario
I have reviewed the karate demo but i did not come across any sample code that will help my situation. May i know the right way to do this in Karate? Thanks.
I think you are missing the callonce keyword. Understand it and then look at the demos, for example, this one: callonce.feature.
You will need to move the 'common' code into a separate feature, but that is the right thing to do, because you will typically want it re-usable by multiple feature files as well.

Karate Automation: Is there any way we can set the Scenario name dynamically from a json file [duplicate]

This question already has answers here:
Can we parameterize the request file name to the Read method in Karate?
(2 answers)
Closed 1 year ago.
I am using a JSON file which act as a test case document for my API testing. The JSON contain Test Case ID, Test case Description, Header and Request body details, which should be the driving factor of Automation
Currently i am looping a feature over this json file to set different header and body validations. However it will be helpful if i can set the Scenario name from JSON file while its iterating
Something like
serverpost.feature
Feature:re-usable feature to publish data
Scenario: TC_NAME # TC_NAME is avaliable in the JSON data passed to this feature. However, CURRENTLY ITS NOT TAKING THIS DATA FROM JSON FILE.
Given path TC_ID # TC ID is taken from JSON
Given url 'http://myappurl.com:8080/mytestapp/Servers/Data/uploadServer/'
And request { some: '#(BODY)' } # Request Body Details is taken from JSON
Please suggest
In my honest opinion, you are asking for a very un-necessary feature. Please refer to the demo examples, look for it in the documentation.
Specifically, look at this one: dynamic-params.feature. There are multiple ways to create / use a data table. Instead of trying to maintain 2 files - think of Karate as being both - your data table AND the test execution. There is no need to complicate things further.
If you really really want to re-use some JSON lying around, it is up to you but you won't be able to update the scenario name, sorry. What I suggest is just use the print statement to dump the name to the log and it will appear in the HTML report (refer to the doc). Note that when calling a feature in a loop using a JSON array, the call argument is ALREADY included the report, so you may not need to do anything.
Just an observation - your questions seem to be very basic, do you mind reading the doc and the examples a bit more thoroughly, thanks.

using Selenium: how to use output from one test as input to another test

I'm a Selenium n00b... it's clear how easy it is to run a test and verify a particular response, but how can I take a value from one response and use it in the next test?
an example might be a contact creation form...
type in name/email and click submit
response arrives with new ContactID
grab the ContactID that was returned and put it into "get" textbox and click "submit"
response arrives with contact details
verfy the name/email match the first set
how might I go about doing this in Selenium?
And now something completely different:
Now I understand when you say "test", you mean a single assertion within one test case. So you want to use a value returned from a request as input for another request in the same test case.
Assuming you use selenium ide: To do this, use one of the "store..." commands in selenium ide and store the value into a variable. The contactID can be found using a matching selector with the storeText command. For example:
command: storeText
target: selector for element containing contactId
value: contactId
Then, use variable substitution and the type command to insert that text somewhere else.
command: type
target: selector for target input box
value: ${contactId}
Hope this helps :)
(This answer is still correct I think if you interpret "test" as "test case". For another, totally different answer see below.)
You don't do this. Each test should be independent from all other tests. For your second test, just repeat the steps in the first test. This way, you can reproduce test success and failures in a reliable way.
If you have many tests which all start from a certain application state which requires many steps to reach, just write a private helper method to reach that state.
The alternative: All steps you describe can be put into a single test. There is no reason not to have several asserts in one test.