javascript 如何使用量角器检查元素是否不可点击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27859751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:05:48  来源:igfitidea点击:

How to check if an element is not clickable with Protractor?

javascriptnode.jsprotractorfunctional-testing

提问by Seer

It's trivial to test if an element isclickable with Protractor, but I'm stuck scratching my head trying to figure out how to check if an element is notclickable.

使用量角器测试一个元素是否可点击微不足道的,但我一直在挠头,试图弄清楚如何检查一个元素是否不可点击。

I've attempted to wrap the click function in a try/catch so that when an error is thrown when trying to click it should catch it and let the test pass; however, this does not work.

我试图将 click 函数包装在 try/catch 中,以便在尝试单击时抛出错误时应该捕获它并让测试通过;但是,这不起作用。

Here is my code for the method that does the check:

这是我进行检查的方法的代码:

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

回答by Seer

I have found a solution that works for this. As click()returns a promise you can simply .thenoff of it and throw in the successful click handler and override the catch handler to do nothing which makes the test pass if the element is not clickable.

我找到了一个适用于此的解决方案。当click()返回一个承诺时,您可以简单地.then关闭它并投入成功的点击处理程序并覆盖捕获处理程序,如果元素不可点击,则不执行任何使测试通过的操作。

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

回答by hankduan

Maybe not applicable in your case, but a better way to check if an element is clickable is checking if it is both visible and enabled: elem.isDisplayed()and elem.isEnabled(). This way you don't accidentally click on buttons when you're not supposed to.

也许不适用于您的情况,但检查元素是否可点击的更好方法是检查它是否可见且已启用:elem.isDisplayed()elem.isEnabled(). 这样您就不会在不应该点击的时候意外点击按钮。

Fyi, there will be a library coming to help with cases like this: https://github.com/angular/protractor/pull/1703

仅供参考,将有一个库来帮助处理这样的情况:https: //github.com/angular/protractor/pull/1703

回答by Jacek Góraj

There are actually two methods to check it.

实际上有两种方法可以检查它。

1) Using ExpectedConditions

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

If found to be clickable, it will return error.

如果发现可点击,则会返回错误。

2) Using protractor's isEnabled, isDisplayedand isPresent

2) 使用量角器的isEnabled,isDisplayedisPresent

So as far as my understanding goes, you can create isNotClickable, which will return false only if element is present, displayed or enabled and true otherwise:

因此,据我所知,您可以 create isNotClickable,它仅在元素存在、显示或启用时返回 false ,否则返回 true :

function isNotClickable(element) {
    return element.isPresent().then((isPresent) => {
        if (isPresent) {
            return element.isDisplayed().then((isDisplayed) => {
                if (isDisplayed) {
                    return !element.isEnabled();
                }
                return true;
            });
         }
         return true;
     });
}

回答by kesav ragi

To verify Clickable : element.isDisplayed().toBe(true)

验证可点击: element.isDisplayed().toBe(true)

Not Clickable : element.isDisplayed().toBe(false)

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

Worked for me.

对我来说有效。