在实习生中有没有办法可以轮询,直到元素可见?我网站上的很多元素都在dom中,但却被隐藏了,所以每次我都会发现"找到"它应该出现之后的元素X失败,因为该元素明显破坏了selenium检查的一个可见属性。
我已经尝试了辅助功能" pollUntil"但我似乎无法让它发挥作用。 Dojo似乎不喜欢document.getElement *()
传递给pollUntil的Helper函数
//this is a helper function for pollUntil
//first we want to find an element by class name and text
var elementVisibleAndText = function(elems, innerText){
elems = document.getElementsByClassName(elems);
//if we can't even find it, we return null
//but if we do find it, we want to return a
//not null element
if (!elems || elems.length == 0){
return null;
}
//now let's look at all of the elements found by
//in elems, and see if the innerHTML matches. If it
//does then we want to return that it was found
var each;
for(each in elems){
if(elems[each].innerHTML == innerText)
return (elems[each].offsetWidth > 0 && elems[each].offsetHeight > 0) ? elems[each] : null;
}
//else return null if nothing is found in the elements
return null;
};
答案 0 :(得分:1)
结帐https://theintern.github.io/leadfoot/pollUntil.html。实习生使用铅笔 - 因此您应该可以访问此功能。
var Command = require('leadfoot/Command');
var pollUntil = require('leadfoot/helpers/pollUntil');
new Command(session)
.get('http://example.com')
.then(pollUntil('return document.getElementById("a");', 1000))
.then(function (elementA) {
// element was found
}, function (error) {
// element was not found
});
要在您的某个测试中使用该功能,您可以使用以下路径导入它:
'intern/dojo/node!leadfoot/helpers/pollUntil'
答案 1 :(得分:1)
我一直遇到这个问题,我们使用了实习生的pollUntil功能来编写一些辅助工具。在我们的测试中,我们使用类似.then(pollUntil(util.element_visible_by_class(), ['toast_notification'], 22000))
在单独的util.js
文件中,我们有
/**
* Our shared utility for unit testing
*/
define([
'intern!object',
'intern/chai!assert',
'require',
'intern/dojo/node!leadfoot/helpers/pollUntil'
], function (registerSuite, assert, require, util, pollUntil) {
return {
element_visible_by_class: function(elem) {
return function(elem) {
elem = document.getElementsByClassName(elem);
if (!elem || elem.length == 0) { return null; }
elem = elem[0];
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? elem : null;
}
},
element_visible_by_id: function(elem) {
return function(elem) {
elem = document.getElementById(elem);
if (!elem) { return null; }
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? elem : null;
}
},
element_hidden_by_id: function(elem) {
return function(elem) {
elem = document.getElementById(elem);
if (!elem) { return null; }
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? null : elem;
}
},
element_hidden_by_class: function(elem) {
return function(elem) {
elem = document.getElementsByClassName(elem);
if (!elem || elem.length == 0) { return null; }
elem = elem[0];
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? null : elem;
}
},
}
})