vendredi 30 octobre 2015

Looping through fields in an Angular form and testing input validations using Protractor?

I'm a beginner & am trying to loop through all the fields in an Angular form, and test that the input validation is working, using Protractor. So far I'm failing miserably. My pseudo code is as follows:

//PSEUDO CODE FOR TEST PROCESS:
//------------------------------
// 1.For each field requiring validation
// 2.Reset test environment
// 3.Populate field with dummy data
// 4.Get result
// 5.Evaluate result versus expectation for test type
// 6.Pass test description & test result (true/false) to Protractor to print to command line

The code I've written is below. It's not great, and I'm not even sure if it's possible to achieve my objective without specifying individual tests for each validation test for each field. What am I doing wrong / what is the correct approach?

describe('Sample form', function() {

// Fields subject to input validation
  var userName          = element(by.model('user.name'));           // required field
  var userSurname       = element(by.model('user.surname'));        // required field
  var userId            = element(by.model('user.id'));             // required field

// Test population
  var fieldsRequired    = [userName, userSurname, userId];          // fields to be tested

// helper function to check class of a specified element --> ng-valid / ng-invalid etc.
  var hasClass = function (element, cls) {
    return element.getAttribute('class').then(function (classes) {
      return classes.split(' ').indexOf(cls) !== -1;
    });
  };

// The testing function
  function testRequired(fieldsRequired) {
    //1. loop through each field
      for (var i = 0; i < fieldsRequired.length; i++) {

      //2. Reset page  prior to each test
        browser.get('http://ift.tt/1P9cRd2');      

      //3. Populate field with dummy data
        fieldsRequired[i].sendkeys();  

      //4,5 & 6. Protractor test
        it('should fail validation when ' + fieldsRequired[i] + ' is missing', expect(hasClass(fieldsRequired[i],'ng-valid')).toEqual(false));
      }
  }
});

Aucun commentaire:

Enregistrer un commentaire