Pass POJOs in Cucumber Example table - selenium

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.

Related

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.

Run Feature file with different login

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

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" |

Selenium test redirect

I'm using the Selenium IDE to create some test scenario's. I want to create a scenario where a user tries to visit a certain url without logging in. But the webpage should redirect the user to the login screen instead, since the user needs to login first But I can't find any information about redirecting with the Selenium IDE.
Check this out. That will prevent javascript redirect:
getEval | window.location.href = 'http://your.page';
storeLocation | url
while | storedVars['url'] != 'http://your.page'
storeLocation | url
endWhile
getEval | window.stop();
You need Selenium IDE flow control for that.
if you just need to check that redirection happens:
open | http://your.page
pause | 5000
storeLocation | url
verifyEval | storedVars['url'] == 'http://crocodile.page' | true
If you need something else try to make your question a little bit more detailed. It is hard to understand what are you trying to do.