0

I need to automate a form where there is a checkbox for all days, and a checkbox for each day. The test cases are:

  1. At least one of the Days checkboxes must be checked.
  2. If All is checked, no other checkbox can be checked.
  3. If any individual day (Sunday, Monday, Tuesday etc.) is checked, All must be unchecked.

How do I get values from the check boxes, and how do I set them?

Here is my code, I wrote for testing has priority numbers are three. First default testng for open login page. After login go to category module then enter name for each field and then submit and here I want to test the check boxes .I wrote the next testng priority 3 for testing the check boxes and I run my code .Here not testing the priority no. 3 .till testing priority 1 and 2 and then finish.What should I do ?

public class subcategoryTest extends DriverManager {
    subcategorysteps log;
    int inputDataFlag = 0;

    public subcategoryTest() throws IOException {
        log = new subcategorysteps();
    }

    @BeforeSuite
    public void initDriver() throws IOException {


        HomePage page = new LoginSteps().Login(prop.getProperty("nam"), prop.getProperty("pas"));
    }

    @Test(dataProvider = "setData")
    public void init(String nam, String nam_loc, String cat, String cli, String pre, String ts, String te, String tavg) {

        log.Login(nam, nam_loc, cat, cli, pre, ts, te, tavg);
        Assert.assertFalse(driver.getPageSource().contains("List Subcategory"));

    }

   @Test(priority = 1, dataProvider = "setData")
    public void ini(String nam, String nam_loc, String cat, String cli, String pre, String ts, String te, String tavg) {
        log.Login(nam, nam_loc, cat, cli, pre, ts, te, tavg);
        Assert.assertTrue(driver.getPageSource().contains("List Subcategory"));
    }

    @DataProvider
    Object[][] setData() {

        Object[][] dataValues = getValues();
        return dataValues;
    }

    @Test(priority = 2, dataProvider = "getData")
    public void days() {

        Assert.assertTrue(driver.getPageSource().contains("List Subcategory"));
        }


    @DataProvider
    Object[][] getData() throws InterruptedException {

        Object[][] dataValues = setValues();
        return dataValues;
    }

private Object[][] setValues() throws InterruptedException {
    Thread.sleep(100000);
//    if (!driver.findElement(By.id("days")).isSelected()) {
//        driver.findElement(By.id("days")).click();
//    }
//    return new Object[0][];

    // Find the checkbox or radio button element by its name.
    List<WebElement> list = driver.findElements(By.name("days"));

    // Get the number of checkboxes available.
    int count = list.size();
    System.out.println("no .of boxes  :" +count);
    // Now, iterate the loop from first checkbox to last checkbox.
    for (int i = 0; i < count; i++) {

        // Store the checkbox name to the string variable, using 'Value'
        // attribute
        String sValue = list.get(i).getAttribute("value");

        // Select the checkbox if its value is the same that you want.
        if (sValue.equalsIgnoreCase("Sunday")) {

            list.get(i).click();

            // This statement will get you out of the for loop.
            break;
        }

    }

    return new Object[0][];
}
  • Can you articulate what problem you are having. The current question reads more like "can you carry out my task". – ECiurleo Mar 10 '17 at 11:27
  • @comrade - I've edited your question to make it clearer. – Kate Paulk Mar 10 '17 at 12:30
  • @KatePaulk Please check my code and I edited my question – user24314 Mar 13 '17 at 6:41
  • Does this code work? If not, what error do you get? – Kate Paulk Mar 13 '17 at 11:16
  • @KatePaulk I got the answer and thanks for the suuport – user24314 Mar 13 '17 at 16:34
2

This is a pretty basic Selenium selector question.

Going by the information in the screenshot, you can get the onload values of the checkboxes by retrieving the value of the hidden field with ID of "days": var daylist = driver.findElement(By.ID, "days");

It looks like the day checkboxes are identified this way:

  • Checkbox with value = 1 => Sunday
  • Checkbox with value = 2 => Monday
  • Checkbox with value = 3 => Tuesday
  • Checkbox with value = 4 => Wednesday
  • Checkbox with value = 5 => Thursday
  • Checkbox with value = 6 => Friday
  • Checkbox with value = 7 => Saturday
  • Checkbox with value = 8 => All

Whether or not the checkbox is checked is determined by whether the attribute checked is present.

To interact, you can find by class to gather a list of the checkboxes, and iterate through as in Niels' answer, using getAttribute("value") to find the one you need if they don't have a unique id.

0

I would create a combinatorial test matrix for the combinations I would like to test. Test it manually and write down the steps. Then use these scripts to write an automated test for the cases.

The Selenium API for checkboxes is the following.

if ( !driver.findElement(By.id("idOfTheElement")).isSelected() )
{
     driver.findElement(By.id("idOfTheElement")).click();
}

You click to select one, you can use isSelected() to see if it is checked. True is checked, false is unchecked.

  • Excuse me sir, please check my question I edited my question – user24314 Mar 13 '17 at 6:56
0

you can simply call the isSelected() method on the WebElement (checkbox) and it will return true if it is selected. You can then also just call .click() when you want to select it (after checking it is not already selected). you can also make a List of your checkboxes using the driver.findelements (multiple, not element) and loop through checking them as you desire.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy