Yoda
Yoda

Reputation: 18068

Protractor how to check if attribute exists?

How to check if attribute exists in protractor? I have element

<input class="x" type="checkbox" id="checkbox1" value="undefined" disabled name="checkboxname1" tabindex="0" aria-label="">

I want to check if disabled attribute is present.

I tried that:

const isDisabled: boolean = await field.getAttribute('disabled') === null ? true : false;

but this checks the value of this attribute, not if it's present.

Upvotes: 2

Views: 3704

Answers (3)

Sebastian Kristof
Sebastian Kristof

Reputation: 61

The CSS reference for element that has a particular attribute is [attribute]. So you can do something like this:

const disabledCheckbox1 = '#checkbox1[disabled]';    
const isCheckbox1Disabled = await $(disabledCheckbox).isPresent();

where $(...) is a shortcut for element.by(css(...))

Or if you want to make it more generic:

const element = '#checkbox1';
const attribute = 'disabled';
const isElementDisabled = await $(`${element}[${attribute}]`).isPresent();

Hope this helps!

Here's a great reference to various CSS selectors magic you can do: https://www.w3schools.com/cssref/css_selectors.asp

Upvotes: 0

Vladimir
Vladimir

Reputation: 101

In my test I wait until attribute is not present. I did it by code

await browser.wait(async () => await this.loginButton.getAttribute('disabled') === null);

when you call getAttribute() for nonexistent attribute, it returns null.

Upvotes: 2

Optimworks
Optimworks

Reputation: 2547

You can find whether an element is Disabled or Not using elementToBeClickable() method.

CODE:

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*

Upvotes: 0

Related Questions