Javascript 量角器如何检查属性是否存在?
如何检查量角器中是否存在属性?我有元素Javascript 量角器如何检查属性是否存在?,javascript,protractor,Javascript,Protractor,如何检查量角器中是否存在属性?我有元素 <input class="x" type="checkbox" id="checkbox1" value="undefined" disabled name="checkboxname1" tabindex="0" aria-label=""> 但这会检查该属性的值,而不是它是否存在。您可以使用elementToBeClickable()方法查找元素是否已禁用 代码: var EC=protractor.ExpectedCondiditon
<input class="x" type="checkbox" id="checkbox1" value="undefined" disabled name="checkboxname1" tabindex="0" aria-label="">
但这会检查该属性的值,而不是它是否存在。您可以使用elementToBeClickable()方法查找元素是否已禁用 代码:
var EC=protractor.ExpectedCondiditons;
var field=/*input filed locator*/
expect(EC.elementToBeClickable(field)).toBe(false)
/*EC.elementToBeClickable(field) - return true if element is enabled or
/*false*
在我的测试中,我一直等到属性不存在。 我是按密码做的
await browser.wait(async () => await this.loginButton.getAttribute('disabled') === null);
当为不存在的属性调用
getAttribute()
时,它返回null。具有特定属性的元素的CSS引用是[attribute]
。所以你可以这样做:
const disabledCheckbox1 = '#checkbox1[disabled]';
const isCheckbox1Disabled = await $(disabledCheckbox).isPresent();
其中,$(…)
是元素的快捷方式
或者,如果您想使其更通用:
const element = '#checkbox1';
const attribute = 'disabled';
const isElementDisabled = await $(`${element}[${attribute}]`).isPresent();
希望这有帮助
这里有一个关于各种CSS选择器的很好的参考,你可以这样做:对我来说不起作用。获取“预期函数为真/假”。失败
const element = '#checkbox1';
const attribute = 'disabled';
const isElementDisabled = await $(`${element}[${attribute}]`).isPresent();