Cannot invoke "org.openqa.selenium.WebElement.sendKeys(java.lang.CharSequence[])" because "this.username" is null - selenium

[[here is my LoginPage , stepDefinition and Test runner](https://i.stack.imgur.com/uH2Ft.jpg)](https://i.stack.imgur.com/sh7nu.jpg)learning cucumberPOM and getting this error.could not figure it out the resolution
tried to execute all the stepDefinition methods but only two gets passed and getting this.username is null for my third test i.e enter username and password

Related

Postman Testing Scripts: How to change env variable after one test case?

So my issue is that I want to have 2 tests for a single api call - one pass and one fail with missing params.
Here is what I have:
pm.test("Successful Login", function () {
pm.response.to.have.status(200);
});
pm.test("Missing Parameters", function () {
const currentUsername = pm.environment.get("username");
pm.environment.set("username", null);
pm.response.to.have.status(400);
//pm.environment.set("username", currentUsername);
});
So as you can see, I set username to null for the second test, only to set it back to is original value after the test. What I found was that instead of running the script sequentially, postman set my username to null before the first test could have been run, so I fail the first test. What should I do ?
Ok guys. Apparently you cannot set variables in the testing scripts because the testing script is run after the api call has been made. This needed to be set in the pre-request script. As for how to set all various tests in just on request I dont think this can be done. Therefore, I am just making a new request per test case.

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.

katalon test case parameterize with variable

i would like post different API body every time the test case run.
i have set the variable at POST object
e.g. testID default value test0001
then the HTTP body as below, test and verify passed.
{
“drugId”: “$testID”,
}
what syntax/command i can use in test case like parameterize test step, so first time test case run
drugId = test0001
second time test case run, it will be
drugId = test0002
Your HTTP body should be something like
{
“drugId”: “${testID}”
}
And your request in code should look something like this
response = WS.sendRequest(findTestObject('requestObject',[('testID'): 'test0001']))
where requestObject is your request saved in the Object Repository.
Implementation
Now, if you want to iterate this 10 times, you can do the following:
create a new test case called "callee" with the following content
response = WS.sendRequest(findTestObject('requestObject',[('testID'): testID]))
create another test case called "caller" with the following content
String test = "test000"
for(i=0;i<10;i++){
WebUI.callTestCase(findTestCase("callee"), ["testID":"${test+i.toString()}"], FailureHandling.OPTIONAL)
}
run the "caller" test

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