Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 量角器自定义定位器无法定位元素_Javascript_Angularjs_Selenium_Protractor_End To End - Fatal编程技术网

Javascript 量角器自定义定位器无法定位元素

Javascript 量角器自定义定位器无法定位元素,javascript,angularjs,selenium,protractor,end-to-end,Javascript,Angularjs,Selenium,Protractor,End To End,我已经向需要使用量角器识别和交互的元素添加了自定义数据属性。该属性是数据测试id。我在conf.js中的onPrepare回调中创建了一个自定义定位器来检测元素。详情如下: onPrepare: function () { by.addLocator('testId', function(value, parentElement) { parentElement = parentElement || document; var nodes = parentElem

我已经向需要使用量角器识别和交互的元素添加了自定义数据属性。该属性是
数据测试id
。我在
conf.js
中的
onPrepare
回调中创建了一个自定义定位器来检测元素。详情如下:

onPrepare: function () {
    by.addLocator('testId', function(value, parentElement) {
      parentElement = parentElement || document;
      var nodes = parentElement.querySelectorAll('[data-test-id]');
      return Array.prototype.filter.call(nodes, function(node) {
        return (node.getAttribute('[data-test-id]') === value);
      });
    });
  }
我的angular应用程序有一个
h1
,里面有文本Home。我向它添加了
数据测试id
属性:

<h1 data-test-id="test-element">Home</h1>
conf.js

describe('Navigate to the website.', function() {
    it('Should have the Heading as Home', function() {
        browser.get('http://localhost:8888/#/');
        browser.waitForAngular();

        var textValue = 'Home';
        var heading = element(by.testId('test-element'));

        expect(heading.getText()).toEqual(textValue);
    });
});
exports.config = {
  //directConnect: true,
  seleniumAddress: 'http://localhost:4444/wd/hub',
  // Capabilities to be passed to the webdriver instance.
  capabilities: {
    'browserName': 'chrome'
  },

  // Spec patterns are relative to the current working directly when
  // protractor is called.
  specs: ['test.js'],

  // Options to be passed to Jasmine-node.
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000
  },

  onPrepare: function () {
    by.addLocator('testId', function(value, parentElement) {
      parentElement = parentElement || document;
      var nodes = parentElement.querySelectorAll('[data-test-id]');
      return Array.prototype.filter.call(nodes, function(node) {
        return (node.getAttribute('[data-test-id]') === value);
      });
    });
  }
};
当我运行示例测试时,我得到以下错误:

1) 导航到该网站。标题应为主页
信息: NOSCHELEMENTERROR:使用定位器by.testId(“测试元素”)未找到任何元素


我做错了什么?如何解决此问题并使其正常工作?

当您获取属性时,应该请求
数据测试id
,而不是
[data test id]
。看起来像是复制粘贴错误

替换:

return (node.getAttribute('[data-test-id]') === value);
与:


我也面临同样的问题,以下解决方案解决了我的问题:

by.addLocator('lid', function(value, parentElement) {
        var nodes;

        if(parentElement)
            nodes =  parentElement.querySelectorAll();
        else
            nodes = document.querySelectorAll('body *');


        return Array.prototype.filter.call(nodes, function(node) {
            if( node.hasAttribute("lid") && node.getAttribute('lid') === value)
                return node;
        });
    });

querySelectorAll()需要以“标记”的形式提供输入,如果未发送parentElement,则它将查询“body”标记下的所有标记,否则将查询parentElement中的所有元素。然后循环所有元素,检查属性“lid”是否存在。如果存在,请验证该值,如果该值是所需值,则返回该元素。

非常感谢。。我犯了一个愚蠢的错误|您好,我尝试了上面的解决方案,但仍然出现了“无此类元素”错误,我的代码是:conf.js
by.addLocator('customLocator',function(attribute,value,parentElement){parentElement=parentElement | | document;var nodes=parentElement.querySelectorAll(attribute);return Array.prototype.filter.call(节点,函数(节点){return(节点.getAttribute(属性)==value);});})
和testfile.js
元素(by.customLocator(“ng if”,“$select.open”))。单击()。请帮我解决这个问题。
by.addLocator('lid', function(value, parentElement) {
        var nodes;

        if(parentElement)
            nodes =  parentElement.querySelectorAll();
        else
            nodes = document.querySelectorAll('body *');


        return Array.prototype.filter.call(nodes, function(node) {
            if( node.hasAttribute("lid") && node.getAttribute('lid') === value)
                return node;
        });
    });