7

I am looking to see if this is possible... I am writing UI tests for web and mobile in Cucumber (using Protractor for web and Appium for mobile) and would like to store login details (email and password) in a text file (or another type of file). Is there anyway to run a Feature/Steps file to include a text file so the information stored within the file will be used when logging in to our web/mobile app? Not stored directly inside the Steps file for example.

This was a question that came up during a recent meeting and I couldn't find an answer, so maybe someone else has come across this before?

If it's possible an explanation would be great!

2

One way is to store these User/Pass in application property files. I am using Spring though you can check this link to see how you can read the application property.

In this way, you can have and save multiple username and password for different staging environments in an external file.

For example your property file (release.properties) contains:

    mainpage.loginEmail1 = foo@bar.de
    mainpage.pass1 = 1234
    mainpage.loginEmail2 = foo2@bar.de
    mainpage.pass2 = 6666

Then in your Cucumber scenario (.feature) you have:

Given I am logged in with user no "2"

And in your step implementation:

@Value("${mainpage.loginEmail1}")
private String loginEmail1;
@Value("${mainpage.loginEmail2}")
private String loginEmail2;

@When("^I am logged in with user no no \"([^\"]*)\"$")
    public void logInWithUser(final String userNo) {
            switch (userNo) {

            case "1" :
                userName = loginEmail1;
                break;

            case "2" :
                userName = loginEmail2;
                break;
}
pageUIAuthPage.loginAs(userName, yourPass);
}
1

Sure, you can store credentials or other useful data in a text file. Here's an example from a StackOverflow question where someone is using a YAML file to store credentials, which they then load in env.rb:

https://stackoverflow.com/questions/23682817/how-to-store-and-call-data-from-yaml-file-using-ruby

1

You can store the credentials in yaml files, and then load the yaml files in the test steps or add the credentials to your global context. (ie. Given a user)

I would recommend keeping the files separate, one for mobile and one for web app.

For the web app I am guessing you are using javascript since you mentioned protractor. You can look at something like jsyaml to load yaml into json.

For appium you can look at the yaml gem for ruby. I have not done yaml loading on java so I do not have recommendation there.

0

For Protractor/JavaScript , use JSON for data driven testing.

JSON example:

{
[
    {"username":"User1", "password":"Password1"}, 
    {"username":"User2", "password":"Password2"}, 
    {"username":"User3", "password":"Password3"}
]
}

You may store data in a json file and simply read into a variable and use it as required:

var testData = require('../example/Test Data/Test.json');

    describe('LoginPage', function() {

    it("data.description", function () {
        browser.get("http://127.0.0.1:8080/#/login");
        element(by.model("username")).sendKeys(testData[0].username);
        element(by.model("password")).sendKeys(testData[0].password); 
        element(by.buttonText("Authenticate")).click();

       });
      });
     }); 

By using JSON , you may define complex data hierarchy upto Nth level deeply nested if required.

Complex data definition example using Json:

{
  "squadName": "Super hero squad",
  "homeTown": "Metro City",
  "formed": 2016,
  "secretBase": "Super tower",
  "active": true,
  "members": [
    {
      "name": "Molecule Man",
      "age": 29,
      "secretIdentity": "Dan Jukes",
      "powers": [
        "Radiation resistance",
        "Turning tiny",
        "Radiation blast"
      ]
    },
    {
      "name": "Madame Uppercut",
      "age": 39,
      "secretIdentity": "Jane Wilson",
      "powers": [
        "Million tonne punch",
        "Damage resistance",
        "Superhuman reflexes"
      ]
    },
    {
      "name": "Eternal Flame",
      "age": 1000000,
      "secretIdentity": "Unknown",
      "powers": [
        "Immortality",
        "Heat Immunity",
        "Inferno",
        "Teleportation",
        "Interdimensional travel"
      ]
    }
  ]
} 

If we loaded this object into a JavaScript program, parsed in a variable called superHeroes for example, we could then access the data inside it using the dot/bracket notation as below:

superHeroes.homeTown
superHeroes['active'] 

To access data further down the hierarchy, you simply have to chain the required property names and array indexes together. For example, to access the third superpower of the second hero listed in the member's list, you'd do this:

superHeroes['members'][1]['powers'][2]

Alternatively for more flexible & complex data definitions, one may store data as JS objects in which is a bit more flexible compared to JSON files.

JS Object Example:

var person = {
    firstName:"John",
    lastName:"Doe",
    age:50,
    eyeColor:"blue"
};

Source: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON https://www.w3schools.com/js/js_json.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.