Run Feature file with different login - cucumber-jvm

Here we need to run the same feature file with different login and provide login user name from tags.
Run the feature file with user1 and user2
#user1 #user2
Feature: Feature-1
Background :
Given I am login with user
Scenario:
Scenario:
Run the feature file with user1
#user1
Feature: Feature-2
Background :
Given I am login with user
Scenario:
Scenario:

Use Scenario Outline concept of gherkin language where you can pass user name and password as a Example. For ex:
-- Feature file
Scenario Outline: Verify Login Functionality
Given There is a user "<username>" and <password>"
When I login to the application
Examples:
|username|password|
|user1|pass1|
|user2|pass2|
-- Spec file
Given(/^There is a user (.*) and (.*)$/, async function(username, password) {
driver.findElement(Locator to identify the username element).sendKeys(username);
driver.findElement(Locator to identify the password element).sendKeys(password);
});
Here, same test scenario written above will be repeated for two different users.
Take these two regular expressions as a parameter in spec file and send it to username and password text fields using selenium

Related

Store all tokens for different users and run tests in parallel

Bothering again but really need a piece of advice from the community
Imagine I have 3 user types:
read
write
admin
with different permissions and I need to request a token for all 3. Then store each token and then be able to run some tests for read user, other tests for admin and some tests for all 3 profiles.
Is it possible to handle with Karate?
I know there is ScenarioOutline but that would require me to add this ScenarioOutline to every single scenario.
Also thought about having a json.file and run my CreateToken.feature with every single user that is in this users.json but then I don't know how to tell my tests which token to use.
My Create Token feature file for the basic profile is:
Feature: Create Token
Background: URL definition
* url authUrl
Scenario: Create Token for Read user
When path '/v2/u/login'
And request
"""
{
"username": "#(readUser)",
"password": "#(password)"
}
"""
And method Post
Then status 200
* def readAccessToken = response.token
in karate-config.js I have defined the 3 users:
var config = {
readUser: 'readuser#example.com',
writeUser: 'writeuser#example.com',
adminUser: 'adminuser#example.com',
password: karate.properties['password'],
}
const readAccessToken = karate.callSingle('classpath:helpers/CreateToken.feature', config).readAuthToken
karate.configure('headers', {Authorization: 'Bearer ' + readAccessToken})
In the Feature files:
Scenario: Get explore page
#This should run for all 3 types of users
When path '/a/v2/explore'
And method Get
Then status 200
And match response contains exploreResponse
Scenario: Get user list
#This should run only for admin user
When path '/users'
And method Get
Then status 200
Sorry but I can't the best way to setup this
Thanks!
I would just create the 3 tokens using call-single, keep them as global variables in config and then use them the way you see fit.

Parameterisation for login feature in Karate

I want to parameterise the login feature file so that is can be used for multiple user credentials. Can anyone please help me here? Below is the code that I am using:
Given url baseUrl
And form field username = 'admin'
And form field password = 'admin'
When method GET
Then status 200
I want to parameterise the value for username and password so that it can be passed from another feature file and can be reused again.

Pass POJOs in Cucumber Example table

Description:
As a test developer, I would like to use a single scenario to test 3 different environments.
Simplified Scenario example:
#smoke
Scenario: Login to the login page and assert that the user is logged in
Given User navigates to the page
And User enters valid login credentials
When User clicks on the login button
Then Landing page can be seen
Data ( These are grabbed from a property file - converted to POJO ) :
Env1.class
url = www.environment1.com
username = john
password = doe1
Env2.class
url = www.environment2.com
username = john2
password = doe2
Env2.class
url = www.environment3.com
username = john3
password = doe3
Test Setup
Each environment has its own test runner ( failsafe )
Each environment runs in parallel.
Test runs and is built via ~mvn clean verify
Tests are property file dependant as environments may change.
Potential solution:
Is there a way to pass POJOs in the Example Table? or Cucumber's data table?
I am new to BDD and Cucumber - any help would be great. Thank you.
TLDR: is there a way to pass the Prop File variable in the Examples Table in Cucumber?
| URL | Username | Password |
| env1.getUrl | env1.getUsername | env1.getPassword |
So it'll be
#smoke
Scenario: Login to the login page and assert that the user is logged in
Given User navigates to the page <URL>
And User enters valid login credentials <Username> and <Password>
When User clicks on the login button
Then Landing page can be seen
You can use the scenario outline to run the same scenario with different data for each run. But it will be not parallel. It is sequential. The feature file is,
#smoke
Scenario Outline: Login to the login page and assert that the user is logged in
Given User navigates to the page <URL>
And User enters valid login credentials <Username> and <Password>
When User clicks on the login button
Then Landing page can be seen
Example:
|URL |UserName|Passowrd|
|www.environment1.com|john1 |doe1 |
|www.environment2.com|john2 |doe2 |
|www.environment2.com|john3 |doe3 |
You can use a single runner class. No need to use either property file nor pojo class.
You can achieve that using cucumber extension for BDD2. By using it you can have external examples or you can use properties in example as below:
| URL | Username | Password |
| ${env1.getUrl} | {env1.getUsername} | ${env1.getPassword} |
Alternate is you can use CSV or XML data provider.
In order to use pojo, you need to modify your step definition to accept either DataTable or POJO as argument. When accepting POJO as argument you need to write transformer.
When you use cucumber extension you can also use QAFTestStep which accepts POJO without addition effort. Here you can find step examples and feature file.

Overriding variable in called feature

I'm using Karate to write integration tests for a REST API. In all test scenarios I have to login users to get an authentification token for all calls related to the REST API. So I want to put the login logic into a separate Karate feature, so I don't have to copy and paste the login API call to all other Scenarios.
From the doku I was able to figure out how to call my login.feature in another feature (logout, with needs a logged in user). I'm also able to pass the username and password to the called login.feature from my calling logout.feature. But I also want my login.feature to be executable on it's own, so I have to define username and password in the login.feature. But if I do so, I'm not able to override this variables from the calling feature.
If I run the logout.feature like follows, the login.feature is not using the email parameter I'm providing in the logout.feature If I delete the email variable from login.feature, it is using the parameter from logout.feature, but then I can't run the login.feature on its own.
Calling logout.feature:
Feature: Login/Logout Test
Background:
* url urlBase http://localhost:5000
* def login = call read('classpath:ires/session/login.feature') {email: "user1#test.com", password: "test"}
* def authToken = login.authToken
Scenario: Testing logout via PUT
Given path '/sessions/logout'
Given param TOKEN = authToken
And request {}
When method PUT
Then status 202
Called login.feature:
Feature: Logs in the given user
Background:
* url urlBase http://localhost:5000
* def email = "user2#test.com"
* def password = "test"
Scenario: Test login via POST
Given path '/sessions/login'
And request {email: '#(email)', password: '#(password)'}
And print email
When method post
Then status 200
And def authToken = response
My suggestion is you can create a login-caller.feature that will call the login.feature with the proper argument passed. Another option is to have email and password defined globally in karate-config.js.
I don't recommend depending on undefined variables as it leads to maintainability problems in the long run, but you can do this kind of conditional check:
* def email = typeof email == 'undefined' ? 'user2#test.com' : email

cucumber - wanted to execute same scenario multiple time with multiple set of data ( data will be different ) without scenario outline

wanted to execute below scenario multiple time with different set of user name and password without using scenario outline. As I am new to this world so need some guidance
Data source - excel file
Language - JAVA
#login
Scenario: Login with correct credential
Given open browser & enter user name and password
When validate user credential
Then application should open
And Logout to application & close browser
Scenario: Login with correct credential
Given open browser & enter user name and password
And enter<superuser> in ....
And enter<superUserPassword> in ....
Then click "dadada" button
And verify application should open
And Logout to application & close browser
Examples:
|superuser | superUserPassword |
|"superUser1" |"superPassword1" |
|"superUser2" |"superPassword2" |
|"superUser3" |"superPassword3" |