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

主宰稳场 提交于 2019-11-30 10:49:52

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();
Leo Gallucci

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();

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);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!