如何在量角器中全局添加自定义定位器?

4 protractor

我为量角器编写了一个自定义定位器,该定位器根据anchor元素的ui-sref值查找元素。在我的规范中,我只是by.addLocator添加了自定义定位器,但我认为发布并让其他人使用它可能是一件很酷的事情。

目标是将此自定义定位器添加到全局Protractor对象,以便可以在您的任何规格中使用。

我最初的方法是onPrepare在量角器配置块中添加此功能。类似于下面的伪代码:

onPrepare: function () {
  require('ui-sref-locator')(protractor); // The protractor object is available here.
}
Run Code Online (Sandbox Code Playgroud)

该require语句将执行以下功能:

function (ptorInstance) {
  ptorInstance.by.addLocator('uiSref', function (toState, opt_parentElement) {
    var using = opt_parentElement || document;
    var possibleAnchors = using.querySelectorAll('a[ui-sref="' + toState +'"]');
    var result = undefined;

    if (possibleAnchors.length === 0) {
      result = null;
    } else if (possibleAnchors.length === 1) {
      result = possibleAnchors[0];
    } else {
      result = possibleAnchors;
    }

    return result;
  });
};
Run Code Online (Sandbox Code Playgroud)

问题是by没有protractoronPrepare块中可用的对象上定义。这意味着我不能使用该.addLocator方法。

And*_*s D 5

请尝试以下操作:

function () {
  by.addLocator('uiSref', function (toState, opt_parentElement) {
  ...
Run Code Online (Sandbox Code Playgroud)

通过应在全球范围内。