0

我是量角器端到端测试工具的新手。我想知道,

  1. Protractor 中自定义定位器的用途是什么?

  2. 我们什么时候应该使用它们?

  3. 任何示例将不胜感激?

我在谷歌上搜索过,但没有找到更有用的东西,这可以对它们有所了解。

4

1 回答 1

3

I use custom locators when I can't use any other locator: id, name, repeater, binding, css etc.

For example I have ngClick attribute and I want to select element using that, this is my code:

var customlocators = function() {


by.addLocator('ngClick', function(toState,parentelement) {


     var using = parentelement || document ;
     var prefixes = ['ng-click'];
      for (var p = 0; p < prefixes.length; ++p) {
          var selector = '*[' + prefixes[p] + '="' + toState + '"]';
          var inputs = using.querySelectorAll(selector);
          if (inputs.length) {
              return inputs;
          }
      }     

});
}

module.exports = new customlocators();

Then I can use in tests like any other locator:

element(by.ngClick('addAuthentication()')).click();
于 2017-09-23T17:54:20.520 回答