0

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.

2 Answers 2

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

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.