Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
879 views
in Technique[技术] by (71.8m points)

json - Data-Driven Testing in Protractor

I am new to protractor. Can anyone please guide me for data driven testing using protractor. Below is the code, config file and testdata.json file.

'use strict';

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

describe('LoginPage', function() {

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

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

});
});
});  

Config file:

 // An example configuration file.
exports.config = {
directConnect: true,

//seleniumAddress: 'http://localhost:4444/wd/hub',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'

},

// Framework to use. Jasmine is recommended.
framework: 'jasmine',

// Spec patterns are relative to the current working directory when
// protractor is called.
specs: ['Testpage.js'],

// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
}
};

Json File:

[
{
 "username": "admin",
 "passwordField": "admin"
},

{
"username": "admin1",
"passwordField": "admin2"
}
]

Issue is that instead of taking data , it is writing undefined in all input boxes. Please Help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We have jasmine-data-provider package which will helps us in doing data driven testing with Protractor.

Code Snippet:

var using = require(‘jasmine-data-provider);
var loginData = require('../example/Test Data/Test.json');


 describe('Data driven test spec', function () { /*define sets of input data as array in method called arrayOfData*/
     function arrayOfData() {
       return [
              {
                "username": "admin",
                "passwordField": "admin"
              },

             {
              "username": "admin1",
              "passwordField": "admin2"
              }
          ]
         //or return loginData json object here
   } /*below one will loop the test case based on data size and pass single data set every time till complete the end of array*/

using(arrayofData, function (inputData) {
    it('test case logic to be executed for each set of data', function () {
        browser.get("http://127.0.0.1:8080/#/login");
        element(by.model("username")).sendKeys(inputData.username);
        element(by.model("password")).sendKeys(inputData.passwordField); 
        element(by.buttonText("Authenticate")).click();
    });
  });
 });

NOTE: If jasmine-data-provider package NOT yet installed in your machine, please install it by running below command before going to run test script.

 npm install jasmine-data-provider

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...