Check all checkboxes

Hello team,

I searched the forum but I could not find something to solve my issue.

I am trying to fill out a form, with a list of checkboxes that is dynamic. I mean they have the same type and class but different ID.
I need to automatically fill out every checkbox that is visible.
The class is: form-checkbox agreement
The type is: checkbox

I tried the below:

def checkboxes = WebUI.findWebElements(findTestObject("Object Repository/ma/ma_signup_form/input_agreement_2"), By.cssSelector(".form-checkbox agreement"))
checkboxes.each{ WebUI.click(it) }
def checkboxes = findTestObject("Object Repository/ma/ma_signup_form/input_agreement_1").findChild(By.className("form-checkbox agreement"))
checkboxes.each{ WebUI.click(it) }
def checkboxes = findTestObject("Object Repository/ma/ma_signup_form/input_agreement_1").findElements(by.type("checkbox"))
checkboxes.each{ WebUI.click(it) }

def checkboxes = findTestObject("Object Repository/ma/ma_signup_form/input_agreement_1")

for (def checkbox : checkboxes) {
	WebUI.click(checkbox)
}

Ok, what exceptions are you facing at run time?

What does the HTML look like around these checkboxes?

Thanks for your reply. The html is similar to the below

<div class="form-group">
	<div class="row">
		<div class="checkbox">
			

			<div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 hidden">
				<span>
					
				</span>
			</div>
			<div class="col-xs-10 col-sm-10 col-md-10 col-lg-10 agreements_container">
				<label style="">
					
					<input class="form-checkbox agreement" type="checkbox" data-agreement_id="14" name="" id="" value="1">
					TEXT 
					
				</label>
				<label style="">
					
					<input class="form-checkbox agreement" type="checkbox" data-agreement_id="16" name="" id="" value="1">
					TEXT 
					
				</label>
			</div>
		</div>
		
	</div>
</div>

Thank you for getting back to me so quickly!

This looks like Bootstrap, straight from a codebase.

What does the rendered HTML look like in the DOM?

UPDATE: look at your attempts here. You’re using a TestObject to find a List<WebElement>.

Your first attempt was on the right track, but you should read the documentation.

You don’t pass in a By as that second argument. You pass in a timeOut.

Also you need to make sure the first argument is a TestObject for your checkboxes, assuming you can click the checkboxes. This is why I asked you for the rendered DOM HTML for this.

Once we have that, I can advise you on the best selection strategy for the clickable checkbox widgets.

Your question has a similar solution to the below, but on an input tag, not a div tag.

Use your class attribute, and maybe type attribute, to see if you get just the elements you want.

I also might add a verification to your checkbox being checked.

for (def checkbox : checkboxes) {
	WebUI.click(checkbox)
    WebUI.verifyElementChecked(checkbox, 10)
}

Thank you, that helped :slight_smile: