如何使用 Protractor 检查元素是否不可点击?
How to check if an element is not clickable with Protractor?
用 Protractor 测试一个元素 是否 是可点击的是微不足道的,但我一直在摸不着头脑试图弄清楚如何检查一个元素是否是 不可点击。
我试图将点击功能包装在 try/catch 中,以便在尝试点击时抛出错误时应该捕获它并让测试通过;但是,这不起作用。
这是我执行检查的方法的代码:
return this.shouldSeeDisabledFunds()
.then(function() {
var clickable = true;
try {
fundsElem.first().click();
} catch (e) {
clickable = false;
console.log(clickable);
} finally {
console.log(clickable);
}
console.log(clickable);
// All the way through, clickable is still true, and the console log in the
// catch is not called. I believe this is because click is asynchronous.
})
;
我找到了适用于此的解决方案。作为 click()
returns 的承诺,您可以简单地 .then
关闭它并放入成功的点击处理程序并重写 catch 处理程序不执行任何操作,如果元素不可点击则测试通过.
return this.shouldSeeDisabledFunds()
.then(function() {
fundsElem.first().click()
.then(
function() {
throw "Can click Funds element that should be disabled";
},
function() {}
)
;
})
;
可能不适用于您的情况,但检查元素是否可点击的更好方法是检查它是否可见和启用:elem.isDisplayed()
和 elem.isEnabled()
。这样你就不会在不应该的时候不小心点击按钮。
Fyi,会有一个图书馆来帮助处理这样的情况:https://github.com/angular/protractor/pull/1703
验证可点击:element.isDisplayed().toBe(true)
不可点击:element.isDisplayed().toBe(false)
对我有用。
实际上有两种检查方法。
1) 使用 ExpectedConditions
var EC = protractor.ExpectedConditions;
// Waits for the element with id 'abc' to not be clickable.
browser.wait(EC.not(EC.elementToBeClickable($('#abc'))), 5000);
如果发现可以点击,会return报错。
2) 使用量角器的 isEnabled
、isDisplayed
和 isPresent
据我所知,您可以创建 isNotClickable
,只有当元素存在、显示或启用时才会 return 为假,否则为真:
function isNotClickable(element) {
return element.isPresent().then((isPresent) => {
if (isPresent) {
return element.isDisplayed().then((isDisplayed) => {
if (isDisplayed) {
return !element.isEnabled();
}
return true;
});
}
return true;
});
}
用 Protractor 测试一个元素 是否 是可点击的是微不足道的,但我一直在摸不着头脑试图弄清楚如何检查一个元素是否是 不可点击。
我试图将点击功能包装在 try/catch 中,以便在尝试点击时抛出错误时应该捕获它并让测试通过;但是,这不起作用。
这是我执行检查的方法的代码:
return this.shouldSeeDisabledFunds()
.then(function() {
var clickable = true;
try {
fundsElem.first().click();
} catch (e) {
clickable = false;
console.log(clickable);
} finally {
console.log(clickable);
}
console.log(clickable);
// All the way through, clickable is still true, and the console log in the
// catch is not called. I believe this is because click is asynchronous.
})
;
我找到了适用于此的解决方案。作为 click()
returns 的承诺,您可以简单地 .then
关闭它并放入成功的点击处理程序并重写 catch 处理程序不执行任何操作,如果元素不可点击则测试通过.
return this.shouldSeeDisabledFunds()
.then(function() {
fundsElem.first().click()
.then(
function() {
throw "Can click Funds element that should be disabled";
},
function() {}
)
;
})
;
可能不适用于您的情况,但检查元素是否可点击的更好方法是检查它是否可见和启用:elem.isDisplayed()
和 elem.isEnabled()
。这样你就不会在不应该的时候不小心点击按钮。
Fyi,会有一个图书馆来帮助处理这样的情况:https://github.com/angular/protractor/pull/1703
验证可点击:element.isDisplayed().toBe(true)
不可点击:element.isDisplayed().toBe(false)
对我有用。
实际上有两种检查方法。
1) 使用 ExpectedConditions
var EC = protractor.ExpectedConditions;
// Waits for the element with id 'abc' to not be clickable.
browser.wait(EC.not(EC.elementToBeClickable($('#abc'))), 5000);
如果发现可以点击,会return报错。
2) 使用量角器的 isEnabled
、isDisplayed
和 isPresent
据我所知,您可以创建 isNotClickable
,只有当元素存在、显示或启用时才会 return 为假,否则为真:
function isNotClickable(element) {
return element.isPresent().then((isPresent) => {
if (isPresent) {
return element.isDisplayed().then((isDisplayed) => {
if (isDisplayed) {
return !element.isEnabled();
}
return true;
});
}
return true;
});
}