4

How can I check if checkbox is checked with Protractor, CucumberJS and Chai ?

var el = 'myCheckbox';

this.expect(element(by.model(el)).to.have.prop("checked", true));
Jérémie Chazelle
  • 1,721
  • 4
  • 32
  • 70

4 Answers4

13

isSelected() would do that:

this.expect(element(by.model(el)).isSelected()).to.eventually.be.true;
Jérémie Chazelle
  • 1,721
  • 4
  • 32
  • 70
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I tried with this.expect(element(by.model(el)).isSelected()).to.be.true; but it's doesn't work – Jérémie Chazelle Dec 03 '15 at 16:07
  • I have the same error when I have a syntax error "TypeError: callback.fail is not a function" – Jérémie Chazelle Dec 03 '15 at 16:16
  • @JérémieChazelle are you using [Chai as Promised](http://chaijs.com/plugins/chai-as-promised)? – alecxe Dec 03 '15 at 16:17
  • I use element(by.model(el)), yes chai and chai-as-promised – Jérémie Chazelle Dec 03 '15 at 16:33
  • 1
    @JérémieChazelle okay, I think you need to use `eventually` since this is a promise returned by `isSelected`. Updated, check it out. Thanks. – alecxe Dec 03 '15 at 17:11
  • 1
    I had a problem with isSelected(), and it turned out my element selector was NOT actually grabbing the input checkbox element, so thats something you need to be 100% certain of. (i was using checkboxes underneath a checkbox-list directive) – BrettJ Dec 03 '15 at 18:37
1

You should use this.expect(element(by.model(el)).isSelected()).to.eventually.be(true);

nnn
  • 328
  • 3
  • 9
-1
expect(element(by.id("")).isSelected()).toBeFalsy();
JustBaron
  • 2,319
  • 7
  • 25
  • 37
  • Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Jul 31 '18 at 07:26
  • why should be it falsy? Shouldn't it be truthy ? – harsh hundiwala Aug 23 '18 at 14:29
-1

I have tried with the following code and it passed.

expect(element(by.css("")).get(i).isSelected()).toBeFalsy();

where, i corresponds to which checkbox needs to be tested