How to select the Checkboxes when there are set of both Enable and Disabled checkboxes

Hi
My application has set of records in which some are enabled and some are disabled. Here i want to check from the list if record is disable then search for a enabled record from the list and select it.

Any Help?
Regards!!

Hi,

    verify is it clickable or not

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import com.kms.katalon.core.webui.driver.DriverFactory

WebDriver driver = DriverFactory.getWebDriver()

//List<WebElement> elements = driver.findElements(By.xpath("//input[@type='checkbox']"));
List<WebElement> elements = driver.findElements(By.xpath("//div[@class='col-sm-offset-5 col-sm-4']/label"));
List <String> chxClickable = new Arraylist<>()
List <String> chxNotClickable = new Arraylist<>()
int val = 1
String txtval = val
for (WebElement el: elements){
	'Then you have to change xpath property value to get all checkbox values'
	'Change xpath property to new value'
	checkboxToModify = WebUI.modifyObjectProperty(findTestObject('Object Repository/DatePicker/Page_/day'), 'text','equals',txtval, true)
	clickable = WebUI.verifyElementClickable(checkboxToModify, FailureHandling.OPTIONAL)
	if(clickable){
		println ("DEBUG**** clickable "+clickable)
		chxClickable.add("${el.getText()}")
	
	}else{
		chxNotClickable.add("${el.getText()}")
		println ("DEBUG**** not clickable "+clickable)
	}
	txtval++
}
for (String s : chxClickable){
	
	println ("here are all enabled checkboxes "+s)
}

for (String k : chxNotClickable){
	
	println ("here are all Not enabled checkboxes "+k)
}

Hi @Timo_Kuisma
i tried the given code but getting following error

Test Cases/Checkbox FAILED because (of) (Stack trace: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
file:/C:/Users/Creta/Katalon%20Studio/Dummy%20201/Scripts/Checkbox/Script1552878904002.groovy: 25: unable to resolve class Arraylist
@ line 25, column 33.
List chxClickable = new Arraylist()
^

file:/C:/Users/Creta/Katalon%20Studio/Dummy%20201/Scripts/Checkbox/Script1552878904002.groovy: 27: unable to resolve class Arraylist
@ line 27, column 36.
List chxNotClickable = new Arraylist()
^

2 errors

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:946)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:542)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at com.kms.katalon.core.main.ScriptEngine.getScript(ScriptEngine.java:188)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:183)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:108)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:294)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:285)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:264)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:256)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:200)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:99)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:90)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)
at TempTestCase1552879907717.run(TempTestCase1552879907717.groovy:22)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:518)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
at groovy.ui.GroovyMain.run(GroovyMain.java:384)
at groovy.ui.GroovyMain.process(GroovyMain.java:370)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
at groovy.ui.GroovyMain.main(GroovyMain.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)

)

sorry,

my typo

should be ArrayList not the Arraylist :slight_smile:

Hi

Yes error is fixed now. And when i run the test it showed above script is passed but it didnt select any checkbox from the list. here is the console

03-17-2019 10:41:09 PM - [START] - Start Test Case : Test Cases/Checkbox
03-17-2019 10:41:09 PM - [INFO] - Evaluating variables for test case
03-17-2019 10:41:10 PM - [START] - Start action : Statement - driver = com.kms.katalon.core.webui.driver.DriverFactory.getWebDriver()
03-17-2019 10:41:11 PM - [RUN_DATA] - Logging run data ‘sessionId’ with value ‘fa24bab53803f7a3d76569848a4993b4’
03-17-2019 10:41:11 PM - [RUN_DATA] - Logging run data ‘browser’ with value ‘Chrome 72.0.3626.121’
03-17-2019 10:41:11 PM - [RUN_DATA] - Logging run data ‘platform’ with value ‘Windows 10’
03-17-2019 10:41:11 PM - [RUN_DATA] - Logging run data ‘seleniumVersion’ with value ‘3.7.1’
03-17-2019 10:41:11 PM - [RUN_DATA] - Logging run data ‘proxyInformation’ with value ‘ProxyInformation{proxyOption=NO_PROXY, proxyServerType=HTTP, password=, proxyServerAddress=, proxyServerPort=0}’
03-17-2019 10:41:11 PM - [END] - End action : Statement - driver = com.kms.katalon.core.webui.driver.DriverFactory.getWebDriver()
03-17-2019 10:41:11 PM - [START] - Start action : Statement - elements = driver.findElements(org.openqa.selenium.By.xpath("//div[@class=‘col-sm-offset-5 col-sm-4’]/label"))
03-17-2019 10:41:11 PM - [END] - End action : Statement - elements = driver.findElements(org.openqa.selenium.By.xpath("//div[@class=‘col-sm-offset-5 col-sm-4’]/label"))
03-17-2019 10:41:11 PM - [START] - Start action : Statement - chxClickable = new java.util.ArrayList()
03-17-2019 10:41:11 PM - [END] - End action : Statement - chxClickable = new java.util.ArrayList()
03-17-2019 10:41:11 PM - [START] - Start action : Statement - chxNotClickable = new java.util.ArrayList()
03-17-2019 10:41:11 PM - [END] - End action : Statement - chxNotClickable = new java.util.ArrayList()
03-17-2019 10:41:11 PM - [START] - Start action : Statement - val = 1
03-17-2019 10:41:11 PM - [END] - End action : Statement - val = 1
03-17-2019 10:41:11 PM - [START] - Start action : Statement - txtval = val
03-17-2019 10:41:11 PM - [END] - End action : Statement - txtval = val
03-17-2019 10:41:11 PM - [START] - Start action : Statement - For (org.openqa.selenium.WebElement el : elements)
03-17-2019 10:41:11 PM - [END] - End action : Statement - For (org.openqa.selenium.WebElement el : elements)
03-17-2019 10:41:11 PM - [START] - Start action : Statement - For (String s : chxClickable)
03-17-2019 10:41:11 PM - [END] - End action : Statement - For (String s : chxClickable)
03-17-2019 10:41:11 PM - [START] - Start action : Statement - For (String k : chxNotClickable)
03-17-2019 10:41:11 PM - [END] - End action : Statement - For (String k : chxNotClickable)
03-17-2019 10:41:11 PM - [PASSED] - Test Cases/Checkbox
03-17-2019 10:41:11 PM - [END] - End Test Case : Test Cases/Checkbox

Below is the Script

import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory

WebDriver driver = DriverFactory.getWebDriver()

//List elements = driver.findElements(By.xpath("//input[@type=‘checkbox’]"))
List elements = driver.findElements(By.xpath(’//div[@class=‘col-sm-offset-5 col-sm-4’]/label’))

List chxClickable = new ArrayList()

List chxNotClickable = new ArrayList()

int val = 1

String txtval = val

for (WebElement el : elements) {
‘Then you have to change xpath property value to get all checkbox values’
‘Change xpath property to new value’
checkboxToModify = WebUI.modifyObjectProperty(findTestObject(‘New changes/Customer/Asset zone/1.Check box’), ‘text’, ‘equals’,
txtval, true)

clickable = WebUI.verifyElementClickable(checkboxToModify, FailureHandling.OPTIONAL)

if (clickable) {
println('DEBUG**** clickable ’ + clickable)

    chxClickable.add("$el.getText()")
} else {
    chxNotClickable.add("$el.getText()")

    println('DEBUG**** not clickable ' + clickable)
}

txtval++

}

for (String s : chxClickable) {
println('here are all enabled checkboxes ’ + s)
}

for (String k : chxNotClickable) {
println('here are all Not enabled checkboxes ’ + k)

WebUI.click(findTestObject('New changes/Customer/Asset zone/1.Check  box'))

}

hi,
i guess that this class name is not in your application
col-sm-offset-5 col-sm-4’
“List elements = driver.findElements(By.xpath(’//div[@class=‘col-sm-offset-5 col-sm-4’]/label’))”

NOTE NOTE
my code examples here are not the full solution :slight_smile:

Hey

I am very new to this and learning about the coding. So if u can expain me bit more like what exactly i need to put there. Like which element i need to capture for the same. Is it check box? for the below one

List elements = driver.findElements(By.xpath(’//div[@class=‘col-sm-offset-5 col-sm-4’]/label’))”

ping here checkbox xpaths

"List elements = driver.findElements(By.xpath(’//div[@class=‘col-sm-offset-5 col-sm-4’]/label’))”
class name where tag checkbox is in a Html
could you ping some piece of your application html here

Yeah.

This is the Inspect element for disabled checkbox

This is the inspect element fo enabled checkbox

try with this class name
List elements = driver.findElements(By.xpath(’//div[@class=‘pure-g row-s-box’]/label’))

but then i will need xpath values too

hey
here is the xpath value of check box

what is the xpath value if Attributes radio button is selected in Katalon?

hey

I see the same above mentioned xpath works for both selected and unselected checkboxes.

change xpath properties value in xpath string as here is done with index

def index = 1
 List elements = driver.findElements(By.xpath(’//div[@class=‘pure-g row-s-box’]/label’))
for (WebElement el: elements){
xPath = ".//*[normalize-space(text()) and normalize-space(.)='Facility'])[1]/following::input["+index+"]" //note this is not from your html, only example, you have to pick up correct xpath from your html
checkboxToModify = WebUI.modifyObjectProperty(findTestObject('<object path>'), 'xpath','equals',xPath, true)
clickable = WebUI.verifyElementClickable(checkboxToModify, FailureHandling.OPTIONAL)
index++
}

hey

Please see the following error showing in scripts

check my latest comments, there was my typo, fixed it

 List elements = driver.findElements(By.xpath(’//div[@class=‘pure-g row-s-box’]/label’))
for (WebElement el: elements){

hey

Sorry for the trouble, I added the scripts given by you and it showed me this error

what is note when red x pressed?

unexpected token: for @ line 26, column 1

correct these  '
```
List elements = driver.findElements(By.xpath(’//div[@class=‘pure-g row-s-box’]/label’))

do not copy all :slight_smile: 
```
or if you want to copy all then copy through notepad