简体   繁体   English

如何使用Behat Mink扩展检查禁用属性?

[英]How to check for disabled attribute using behat mink extention?

I have one scenario in my functionality, where i wanted to write script if checkbox has disabled property then return true. 我的功能中有一种情况,如果复选框已禁用属性,那么我想编写脚本,然后返回true。

<input type="checkbox" class="checkbox-click" id="#checkbox-2" name="testcheck" value="where to go" disabled="disabled">

Following is the custom one which i was trying but not succeed: 以下是我尝试但未成功的自定义方法:

    /**
     * check for is disabled or not
     *
     * @Then /^I check for is disabled "([^"]*)"$/
     */
    public function isDisabled($selector)
    {
        $session = $this->getSession();
        $element = $session->getPage()->find(
            'xpath',
            $session->getSelectorsHandler()->selectorToXpath('css', $selector) // just changed xpath to css
        );

        $return = $element->getAttribute('disabled');    
    }

I was getting Fatal error : getAttribute calling on nonobject. 我遇到致命错误:getAttribute调用非对象。 This is not perfect code but i just given here for reference. 这不是完美的代码,但我在这里仅供参考。

This happens because you don't handle the error. 发生这种情况是因为您不处理该错误。 Find method returns the object if the element is found, else it will return null and using getAttribute on null results in fatal exception since is not an object. 如果找到了元素,Find方法将返回对象,否则将返回null,并且对null使用getAttribute会导致致命异常,因为它不是对象。

You should check if null and if yes then throw an exception. 您应该检查是否为null,如果是,则抛出异常。

You don't need to use selector to xpath. 您无需对xpath使用选择器。 Also you don't throw any error if the attribute is not found. 如果找不到该属性,也不会引发任何错误。

As a validation you could use the method hasAttribute() on the element and throw exception by case. 作为验证,您可以在元素上使用方法hasAttribute()并按情况抛出异常。 You don't Another thing you could do is to use a conditional wait. 您不需要做的另一件事是使用条件等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM