禁用的锚点返回使用WebElement.isEnabled()启用的

Disabled anchor returns enabled using WebElement.isEnabled()

本文关键字:isEnabled 启用 WebElement 返回      更新时间:2023-09-26

由于某些原因,即使在禁用的html锚标记上,elmFinder.isEnabled()也会解析为true

我已经建立了一个测试站点来证明这一点。

当IMHO不应该时,以下Protractor测试失败

describe('isEnabled() should resolve to true on any html element', function() {
  var checkElm = element(by.model('checked'));
  var btnElm = element(by.model('button'));
  var linkElm = element(by.model('link'));
  it('open test page', function() {
    browser.get('http://run.plnkr.co/plunks/oExtzK/');
  });
  it('should be enabled by default: button & link', function() {
    expect(btnElm.isEnabled()).toBeTruthy();
    expect(linkElm.isEnabled()).toBeTruthy();
  });
  it('clicks the checkbox to switch enabled/disabled status', function() {
    checkElm.click();
  });
  it('button should now be disabled', function() {
    expect(btnElm.isEnabled()).toBeFalsy();
  });
  // This fails
  it('link should now be disabled', function() {
    expect(linkElm.isEnabled()).toBeFalsy();
  });
});

输出:

Describe: isEnabled() should resolve to true on any html element
 001 - open test page ✔
 002 - should be enabled by default: button & link ✔
 003 - clicks the checkbox to switch enabled/disabled status ✔
 004 - button should now be disabled ✔
 005 - link should now be disabled  FAILED!
   Message:    Expected true to be falsy.
   Stacktrace: Error: Failed expectation

在java绑定文档中找到

对于除禁用的输入元素之外的所有元素,通常都将返回true。

所以我想,解决方法是使用+否定elm.getAttribute('disabled')而不是elm.isEnabled()

it('link should now be disabled', function() {
  expect(linkElm.getAttribute('disabled')).toBeTruthy();
});