How to validate when a checkbox is checked in AngularJS e2e tests?
Asked Answered
D

3

30

I've been trying out the AngularJS e2e tests and am getting stuck determining whether or not a checkbox is checked.

I used the end to end test for the checkbox input as a sample (see the End to end test tab in the Example).

Html snippet:

Value1: <input type="checkbox" ng-model="value1"> <br/>

Controller snippet:

function Ctrl($scope) {
  $scope.value1 = true;
}

Here is what I tried:

1) expect(binding('value1')).toEqual('true');

This works in the sample end to end test as long as value1 is displayed on screen with {{value1}}. If you test this locally and remove `{{value1}} the binding test fails. In most of my real-world examples I am not displaying the checkbox value on the screen anywhere.

2) expect(input('value1').val()).toEqual('true');

The value will always default to on and is not related to whether or not the checkbox is in a checked state (taken from this post).


Note: It looks like the Angular E2E testing will be replaced with Protractor in the future (see the docs)

Dysphoria answered 18/9, 2012 at 21:20 Comment(0)
R
16

I upvoted this question as I had the same issue. I used following workaround in my test, but I'm hoping to see the better way.

expect( element('input[ng-model="value1"]').attr('checked') ).toBeTruthy();
Reclaim answered 18/9, 2012 at 22:9 Comment(3)
Thanks! I like this better than what I'm currently doing. I'll keep this question open a bit just in case there is a better way :)Dysphoria
In karma, it's not attr but prop that should be used. For fellow googlers.Monroy
This answer doesn't work for me. The Leo Gallucci answer works.Russel
O
55

For anyone using Protractor, there is webdriver isSelected() for exactly this.

Instead of asking for checked attribute you can do:

expect(element(by.model('value1')).isSelected()).toBeTruthy();
Oxytocin answered 7/9, 2014 at 22:27 Comment(2)
When i try this code getting Exception : "invalid element state: Element is not currently interactable and may not be manipulated". Any idea how to solve thisFlick
Back in my Protractor days, I solved the Stale element problem with the waitReady.js script, see: #35533639Oxytocin
R
16

I upvoted this question as I had the same issue. I used following workaround in my test, but I'm hoping to see the better way.

expect( element('input[ng-model="value1"]').attr('checked') ).toBeTruthy();
Reclaim answered 18/9, 2012 at 22:9 Comment(3)
Thanks! I like this better than what I'm currently doing. I'll keep this question open a bit just in case there is a better way :)Dysphoria
In karma, it's not attr but prop that should be used. For fellow googlers.Monroy
This answer doesn't work for me. The Leo Gallucci answer works.Russel
D
1

I'm hoping there is a better way but I got around this by validating the count of the checked input elements matching that model binding:

expect(element('input[ng-model="value1"]:checked').count()).toBe(1);

At least one downside to this when checking if something is not checked is if the element doesn't exist or if there was a typo the value would still be 0 like in this example:

expect(element('input[ng-model="valueDoesNotExist"]:checked').count()).toBe(0);

Dysphoria answered 18/9, 2012 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.