1

I need some advice with issue below. When checkbox = checked, there will be attribute value checked=checked and for the unchecks, attribute will not be there and I can't uniquely identify. On a page, there will be checked and unchecked checkboxes. Dev refuses to add additional attribute for me and saying that there must be a way to identify (exist or not exist).

So on my my test page, I have 10 checkboxes with 3 checked and 7 unchecked.

HTML for uncheck checkboxes

<input type="checkbox" disabled="disabled" ng-checked="item.unread">

HTML for checked checkboxes

<input type="checkbox" disabled="disabled" ng-checked="item.unread" checked="checked">

My attempt at counting - it will returned exact number and will fail eventually as number may change.

expect(element.all(by.css('input[checked="checked"]')).count()).toBe(2);

No issue with identifying checked items with attribute - checked="checked"

expect(element.all(by.css('input[checked="checked"]')).isSelected()).toBeTruthy();

but fails when I check for unchecked item.

expect(element.all(by.css('input[type="checkbox"]')).isSelected()).toBeFalsy();

Failed result for unchecked item - 7 unchecked and 3 checked

[ false false false false true true false false true false]

Thanks

1 Answer 1

1

As for unhecked inputs, you can check that there is no checked atrribute set:

expect(element.all(by.xpath('//input[@type="checkbox" and not(@checked)]')).isSelected()).toEqual([false, false, false, false, false, false, false, false, false, false]);

If it is always 3 checked and 7 unchecked, you can assert the count also:

expect(element.all(by.xpath('//input[@type="checkbox" and not(@checked)]')).count()).toEqual(7);
expect(element.all(by.xpath('//input[@type="checkbox" and @checked="checked"]')).count()).toEqual(3);
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for quick reply @alecxe. For uncheck inputs, it's returning me failure eventhough it is expected result. Expected [ false, false, false, false, false, false, false, false, false, false] to be falsy.
@user2388556 ah, sure, give me a sec.
@user2388556 please check the update - I think it's okay to just have an aray of 10 false items in this case.
Thanks @alecxe. Array of 10 false items works for me. For checked and unchecked items, is it possible to check like greater than 0 or less than 10?
@user2388556 sure, you can use toBeGreaterThan and toBeLessThan.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.