Saturday, June 15, 2013

Selecting Multiple Option at a time from a List

One of the Scenario that is less frequent but comes in picture sometime is when we have to select
a Multiple options from a list Box  e.g. you are applying for a job and you have to select Domain in which you are working such as Finance , Retail etc . The below screen will give you an idea



So lets start the Coding for the same . This can be done very easily using the AdvancedUserInteractions
You can read a lot about them Here
We will be using some KeyBoard Actions  ( We have to hold CONTROL key and then Click on options which we want to select ) , so the same can  be done using ActionBuilders in webdriver.Lets take a look at
the sample code for the same scenario

        Actions builder = new Actions(driver);
        WebElement option1 = driver.findElement(By.id("m_opt_0"));

        WebElement option2 =driver.findElement(By.id("m_opt_3"));
        builder.keyDown(Keys.CONTROL).click(option1).click(option2).keyUp(Keys.CONTROL);
        Action select_multiple = builder.build();
        select_multiple.perform();

what the Hell above code is doing ??

1) Its saving both the Options you want to select in a webElement Object .
2) Using Actionbuilder its Pressing CONTROL Key , and then Clicking option1 object and subsequently Option2 element .

Hope that Helped.






No comments:

Post a Comment