Jef*_*eff 6 javascript angularjs protractor
我有一个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.
});
Run Code Online (Sandbox Code Playgroud)
如何选择可见.myDiv并单击它?
你能filter()说出来:
var myDiv = element.all(by.css('.myDiv')).filter(function (elm) {
return elm.isDisplayed().then(function (isDisplayed) {
return isDisplayed;
});
}).first();
Run Code Online (Sandbox Code Playgroud)
使用 Angular 时,隐藏许多层相同的 html 是很常见的,但在整个 html 页面中比您想要使用的可见元素更早。
对于一般调试,将站点打开到您期望量角器测试所在的位置,然后打开 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();
};
Run Code Online (Sandbox Code Playgroud)
传入您想要的任何元素,它将获取定位器并获取它的第一个可见版本。您还可以去掉 .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');
});
});
Run Code Online (Sandbox Code Playgroud)
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();
};
};
Run Code Online (Sandbox Code Playgroud)
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();
};
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4526 次 |
| 最近记录: |