问题描述
如何在量角器中等待元素改变属性?当我进入主页时,连接按钮的类别为红色,要等到将类别更改为绿色
解决方法
这是您要解决的任务的通用方法
/**
* waitUntilElementHasAttribute
* @param {ElementFinder} $element Locator of element
* @param {String} attributeName Attribute of an element to evaluate
* @param {string} [attributeString=""] A target attribute value
* @param {number} [timeout=1000] Time in ms
* @return {Promise}
*/
waitUntilElementHasAttribute($element,attributeName,attributeString = '',timeout = 1000) {
return browser.wait(
() =>
$element.getAttribute(attributeName).then(attributeValue => {
if (attributeValue !== null) {
return attributeValue.includes(attributeString);
} else {
return false;
}
}),timeout,"Wait until element '" + $element.locator() + "' HAS " + attributeName + ': ' + attributeString
);
},
请注意,直到指定的属性部分匹配字符串
要使用,只需这样称呼
await waitUntilElementHasAttribute(
$element,'class','green',3000
)