Я добавил атрибут пользовательских данных к элементам, которые мне нужно идентифицировать и взаимодействовать с помощью транспортира. Атрибут: data-test-id
. Я создал пользовательский локатор в обратном вызове onPrepare
в conf.js
для обнаружения элемента. Это ниже:Неправильный локатор неработающего ловушки не находит элемент
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);
});
});
}
Мое угловое приложение имеет h1
с текстом Home внутри него. Я добавил атрибут data-test-id
к нему:
<h1 data-test-id="test-element">Home</h1>
Вот мой транспортир тест:
test.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);
});
});
}
};
Когда я бегу t он испытывает образец, я получаю следующую ошибку:
1) Navigate to the website. Should have the Heading as Home
Message: NoSuchElementError: No element found using locator: by.testId("test-element")
Что я делаю неправильно? Как я могу исправить эту проблему и заставить ее работать?
Спасибо большое .. Это была глупая ошибка с моей стороны ..: | –