Reputation: 15
My Page has single checkBox for two fields each and I need to pass the option to check or not as a generic method. Tried with if else loop and switch case but not working.
My Code:
public void checkBoxClick(String checkBox, String propName){
switch (checkBox) {
case "Checked":
WebElement element=driver.findElement(By.name(propName));
element.click();
break;
case "UnChecked":
break;
}
}
Step-define file:
@Then("^Enter (.*) also check (.*) and (.*)$")
public void enter_rucNo_check_transit(String checkBox1, String checkBox2) throws InterruptedException {
driver_interactions.checkBoxClick("mrclBulkHeader.transit", checkBox1);
driver_interactions.checkBoxClick("mrclBulkHeader.airTransit", checkBox2);
}
I don't know why the checkBox doesn't get click. Can anyone correct me.
Upvotes: 0
Views: 209
Reputation: 896
try this:
public void checkBoxClick(String propName,String checkBox ){
switch (checkBox) {
case "Checked":
WebElement element=driver.findElement(By.name(propName));
element.click();
break;
case "UnChecked":
break;
}
}
Upvotes: 1
Reputation: 12255
Here method that will check or uncheck your checkbox:
public void checkBoxSelect(String propName, boolean select){
WebElement element=driver.findElement(By.name(propName));
if (element.isSelected() != select) {
element.click();
}
If you want to check and checkbox not checked, it will do it. If checkbox already checked will do nothing:
checkBoxSelect("checkboxname", true);
If you want to ucheck and checkbox not unchecked already, it will do it. If checkbox already unchecked will do nothing:
checkBoxSelect("checkboxname", false);
Your step will be:
@Then("^Enter (.*) also check (.*) and (.*)$")
public void enter_rucNo_check_transit(String checkBox1, String checkBox2) throws InterruptedException {
driver_interactions.checkBoxSelect("mrclBulkHeader.transit", true);
driver_interactions.checkBoxSelect("mrclBulkHeader.airTransit", true);
}
In my opinion separate them will be better:
@Then("^Set value of \"Transit\" checkbox to (.*)$")
public void enter_rucNo_check_transit(String check) throws InterruptedException {
driver_interactions.checkBoxSelect("mrclBulkHeader.transit", string.equalsIgnoreCase("true"));
}
@Then("^Set value of \"Air Transit\" checkbox to (.*)$")
public void enter_rucNo_check_transit(String check) throws InterruptedException {
driver_interactions.checkBoxSelect("mrclBulkHeader.airTransit", string.equalsIgnoreCase("true"));
}
Are you locating checkboxes by name attribute "mrclBulkHeader.transit" and "mrclBulkHeader.airTransit"? Please share your html?
Upvotes: 0