Sneh
Sneh

Reputation: 53

How to check if the checkbox is checked or not in protractor using javascript

I am trying to write an end to end protractor test and I am stuck with checking if the checkbox is enabled or not since it does not have the checked property or anything. Is there any way in javascript we could check from a list by getting an element and check if it is checked and if it is checked get the text for that.

This is what i have for the HTML:

<li>

<div _ngcontent-c8="" class="ng-tns-c8-2" >

<input _ngcontent-c8="" class="ng-tns-c8-2" type="checkbox" id="Team TableAccepted">

<label _ngcontent-c8="" class="ng-tns-c8-2" for="Team TableAccepted">Accepted</label></div>

</li>

Upvotes: 1

Views: 839

Answers (2)

Ven Test
Ven Test

Reputation: 1

You can check whether the checkbox is selected/not in two ways

  1. Through the command isSelected()
  2. Through attribute aria-checked=true/false(For mat componenets)

Upvotes: 0

Hunter Gemmer
Hunter Gemmer

Reputation: 96

isSelected() should do the trick here:

Given this input: <input type="checkbox" id="team" />

To check if it's selected, we would write the following:

const checkbox = element(by.id('team'));

expect(checkbox.isSelected()).toBe(true);

If you do not want to use protractor and would prefer vanilla js, this is another option:

const isChecked = document.getElementById('team').checked;

Also, neither HTML4 nor HTML5 allows space characters in id attribute values. In the case of your input: <input _ngcontent-c8="" class="ng-tns-c8-2" type="checkbox" id="Team TableAccepted">, id="Team TableAccepted is not valid HTML. For id attributes, limit the value to one descriptor.

Hope this helps!

Upvotes: 2

Related Questions