如何使用量角器检查元素是否无法点击?

See*_*eer 8 javascript functional-testing node.js protractor

测试一个元素是否可以使用量角器进行测试是微不足道的,但是我一直在试图弄清楚如何检查一个元素是否无法点击.

我试图将click函数包装在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.
    })
;
Run Code Online (Sandbox Code Playgroud)

See*_*eer 9

我找到了一个适用于此的解决方案.当click()返回一个promise时,你可以简单地.then离开它并抛出成功的click处理程序并覆盖catch处理程序,如果该元素不可点击,则不执行任何操作使测试通过.

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