Data-Driven Testing in Protractor

前端 未结 5 1110
南笙
南笙 2020-12-11 07:28

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 st         


        
相关标签:
5条回答
  • 2020-12-11 08:04

    A simpler approach using map function:

    var testParams = testConfig.testArray;
    testParams.map(function(testdata) {
            it('write your test here', function() {
              console.log('Username: ', testData.username);
             });
     });
    
    0 讨论(0)
  • 2020-12-11 08:11

    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
    
    0 讨论(0)
  • 2020-12-11 08:13

    I am assuming its an Array of objects, you can iterate each array element and directly access its contents and you don't need testdata.forEach(), you could try something like this -

     'use strict';
    
    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].passwordField); 
        element(by.buttonText("Authenticate")).click();
    
       });
      });
     });  
    

    I haven't tested the above code and you should use Page Objects rather than directly using them in your tests!

    0 讨论(0)
  • 2020-12-11 08:18

    I think your approach is quite reasonable. The reason you are getting undefined is because you put data in the 'done' parameter. It is setting data to the 'done' object that is passed when the 'it' function calls the function you are defining.

    testData.forEach(function (data) { it("data.description", function (data) {

    should be

    testData.forEach(function (data) { it("data.description", function () {

    0 讨论(0)
  • 2020-12-11 08:24

    The 2nd answer is more prominent. Just to add here that if you want to read data from Excel sheet and then perform Data Driven Testing then this video is very helpful: https://www.youtube.com/watch?v=vzvC4dYE84Q

    0 讨论(0)
提交回复
热议问题