量角器自定义定位器无法定位元素
Protractor custom locator fails to locate the element
我已经为元素添加了一个自定义数据属性,我需要使用量角器识别和交互。属性为data-test-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。我给它添加了data-test-id
属性:
<h1 data-test-id="test-element">Home</h1>
这是我的量角器测试:
. 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);
});
});
conf.js :
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)导航到网站。标题应该是Home
信息:NoSuchElementError: using locator: by. testd ("test-element")没有找到元素
我做错了什么?我怎么能解决这个问题,并得到这个工作?
当您获得属性时,您应该要求data-test-id
而不是[data-test-id]
。似乎是复制粘贴错误。
替换:
return (node.getAttribute('[data-test-id]') === value);
:
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()需要将输入作为'tag',如果没有发送parentelelement,那么它将查询'body'标签下的所有标签,否则将查询来自parentelelement的所有元素。然后循环遍历所有元素并检查属性'lid'是否存在。如果存在,验证值,如果值是期望值,则返回该元素
相关文章:
- 在Jquery detachment()和appendTo()之后定位元素
- Selenium无法在浏览器DOM中定位元素
- 固定定位元素在滚动时停止在某个点
- 如何相对于父边框的外边缘定位元素
- 量角器无法定位元素/值
- jQuery动画和定位元素
- 使用ng-click在作用域内定位元素
- 不会转义视口的绝对定位元素
- Z 索引绝对定位元素低于父级,具有 z 索引解决方法
- 不能使用 jQuery 在循环中定位元素的 ID
- 桌面网络工具包(safari / chrome)中具有绝对定位元素的奇怪渲染错误
- 在聚合物重复中定位元素
- 视差.js移动我的 CSS 定位元素
- 怎么能这样定位元素
- 无法使用量角器的绑定定位器定位元素
- Iframe 和绝对定位元素
- 如何使用 jQuery 计算相对定位元素的绝对位置*没有*
- 使用Javascript调整绝对定位元素的大小
- 基于浏览器宽度定位元素时出现问题
- CSS:在IE中使用jquery定位元素