我有一个SPA,其中多个div具有相同的类。我希望量角器选择可见的div并单击它。我一直在获取Failed: element not visible
其中的信息,可以相信它获得了不在此特定页面上的某些元素(也许?)。我也得到WARNING - more than one element found for locator By.cssSelector('.myDiv') - the first result will be used
了让我觉得不是单击可见的按钮,而是单击不可见的按钮。
这是我的规格:
describe('User actions', function () {
it("should be able to click a my div.", function () {
var myDiv = element(by.css('.myDiv'));
myDiv.click();
// some expect... haven't gotten this far yet.
});
如何选择可见的.myDiv
并单击它?
使用angular时,隐藏同一层html的许多层是很普遍的,但是在整个html页面中,要早于您要使用的visible元素。
对于常规调试,将站点打开到期望量角器测试所处的位置,然后打开html并在html中搜索量角器测试正在搜索的元素。请注意它是否可见以及其整体位置。
考虑是否要为页面的不同区域添加标签,该元素可以在其中显示,然后使用父选择器和子选择器来获取所需的标记。
您还可以使用它来选择第一个可见元素:
// Takes a protractor webelement and returns the first displayed version
// Ex:
// var coolBox = $('.coolBox');
// var visibleCoolBox = getFirstVisibleProtractorElement(coolBox);
this.getFirstVisibleProtractorElement = function(selector){
var allElementsOfSelector = element.all(by.css(selector.locator().value));
return allElementsOfSelector.filter(function(elem) {
return elem.isDisplayed().then(function(displayedElement){
return displayedElement;
});
}).first();
};
传入您想要的任何元素,它将使用定位器并获得它的第一个可见版本。您也可以删除.first()部分以返回要使用的可见元素数组。
编辑:
为了使用它,我将举一个量角器+茉莉花的例子。考虑到页面上有任意数量的所需元素,并且至少有一个可见元素,我认为这应该可行。但是,这太过头了,所以我可能在某个地方犯了一个错误。
example_spec.js
var examplePage = require('./example_page.js');
describe('Extracting visible elements', function(){
it('A visible element can be extracted', function(){
expect(examplePage.isACoolBoxVisible()).toBeTruthy('Error: No visible CoolBoxes');
});
});
example_page.js
var protractorUtils = require('./protractor_utils.js');
module.exports = new function(){
var elements = {
coolBox: $('.coolBox')
};
this.getVisibleCoolBox = function(){
return protractorUtils.getFirstVisibleProtractorElement(elements.coolBox);
};
this.isACoolBoxVisible = function(){
return getVisibleCoolBox.isDisplayed();
};
};
protractor_utils.js
module.exports = new function(){
this.getFirstVisibleProtractorElement = function(selector){
var allElementsOfSelector = element.all(by.css(selector.locator().value));
return allElementsOfSelector.filter(function(elem) {
return elem.isDisplayed().then(function(displayedElement){
return displayedElement;
});
}).first();
};
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句