使用量角器获取按钮上的禁用属性不起作用

using protractor to get the disabled attribute on button not working

本文关键字:属性 不起作用 按钮 量角器 获取      更新时间:2023-09-26

我正在尝试在按钮上获取禁用的attr,它应该被"禁用",但我似乎没有得到值。角度和量角器新手!

当我检查页面时,这是我为显示禁用的按钮获得的 HTML 被禁用,就像它在页面上一样:

 <button type="submit" class="button primary inverse" ng-disabled="!comment.$dirty && comment.$valid" disabled="disabled">Save</button>

下面的量角器测试返回"预期空值等于禁用"

    var btnSave = element(by.css('.primary'));
    expect(btnSave.isPresent()).toBeTruthy();
    var attr = element(by.css('.primary')).getAttribute('disabled');
    expect(attr).toEqual("disabled");

当我尝试时,我被期望"等于禁用。

expect(attr).toEqual("disabled");

有什么想法我哪里出错了吗?

谢谢

角器中的getAttribute()函数以promise的形式返回值。因此,要么等到它返回,然后执行验证,要么可以将函数传递给expectation,从而解析承诺。 disabled html 属性是一个布尔属性,因此它返回的值要么是 true 要么是 false 。方法如下 -

element(by.css('.primary')).getAttribute('disabled').then(function(attr){
    expect(attr).toBe(true);
});

expect(element(by.css('.primary')).getAttribute('disabled')).toBe(true);

希望对您有所帮助。