以Angular形式循环遍历字段并使用Protractor测试输入验证?

时间:2015-10-30 06:55:17

标签: angularjs testing protractor

我是初学者&我试图以Angular形式遍历所有字段,并使用Protractor测试输入验证是否正常。到目前为止,我惨遭失败。我的伪代码如下:

//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

我写的代码如下。它不是很好,我甚至不确定是否可以实现我的目标而不为每个字段的每个验证测试指定单独的测试。我做错了什么/正确的做法是什么?

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://sometestlink.html');      

      //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));
      }
  }
});

1 个答案:

答案 0 :(得分:3)

There are a few issues in your code:

  1. Your it block will never be executed because you are not calling the testRequired function
  2. sendkeys() should be sendKeys(), and you are not sending any text, it's empty

You could try achieve what you described with this:

describe('Sample form', function() {

    // 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;
        });
    };

    var fields = {
        'userName': element(by.model('user.name')),
        'userSurname': element(by.model('user.surname')),
        'userId': element(by.model('user.id'))
    };

    for(var field in fields) {
        (function(field) {                
            it('should fail validation when ' + field + ' is missing', function () {
                browser.get('http://sometestlink.html');
                fields[field].sendKeys('dummy_data');
                expect(hasClass(fields[field], 'ng-valid')).toEqual(false);
            });
        })(field);
    };
});