如何检查一个元素是否与量angular器不可点击?

testing一个元素是否可以用量angular器点击微不足道的,但我被困在我的脑海里试图弄清楚如何检查一个元素是不是可点击的。

我试图把try / catch中的click函数包装起来,这样当试图点击它的时候就会抛出一个错误,应该抓住它并让testing通过; 但是,这是行不通的。

这是我的代码检查方法:

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. }) ; 

我find了一个适用于此的解决scheme。 由于click()返回一个promise,所以你可以简单地closures它,然后抛出成功的click处理程序,并重写catch处理程序,如果元素不可点击,则不做任何事情使testing通过。

 return this.shouldSeeDisabledFunds() .then(function() { fundsElem.first().click() .then( function() { throw "Can click Funds element that should be disabled"; }, function() {} ) ; }) ; 

也许不适用于你的情况,但是检查一个元素是否可点击的更好的方法是检查它是否可见和启用: elem.isDisplayed()elem.isEnabled() 。 这样,你不会无意中点击button。

Fyi,会有一个图书馆来帮助解决这个问题: https : //github.com/angular/protractor/pull/1703

validationClickable: element.isDisplayed()。toBe(true)

不可点击: element.isDisplayed()。toBe(false)

为我工作。