Pages

Wednesday, October 20, 2010

Selection of Dymanic Combo Values

There are 2 cases where the Combo Box goes dynamic
1.ComboBox it self is an ajax element
2.The Combovalues gets dynamically populated on some event trigger

Point 1 can be handled using selenium.waitForCondition()
How about point 2 ? Below is the code

public class DynamicComboElementHandling {
    static Selenium selenium;
    static SeleniumServer server;

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        selenium = new DefaultSelenium("localhost", 4444, "*firefox",
                "http://www.google.com");
        server = new SeleniumServer();
        server.start();
        selenium.start();
        //Click on Google Advanced Search Link
        selenium.open("/advanced_search?hl=en");
        //Wait Until the Drop appears on the screen
        selenium.waitForCondition("selenium.isElementPresent(\"name=num\");",
                "15000");
        boolean flag = true;
        while (flag) {
            //Get the count of the drop down values
            int optionCount = selenium.getSelectOptions("name=num").length;
            // if the dropdown is populated with values, come out of the while
            // loop
            // else continue looping until the count is > 0
            if (optionCount > 0) {
                break;
            }
            continue;
        }
        //This code will be executed only if dropdown has some values
        //else the execution control will be inside the while loop until the
        //drop down values gets populated
        String[] options = selenium.getSelectOptions("name=num");
        for (int i = 0; i < options.length; i++) {
            if (options[i].equals("50 results")) {
                selenium.select("name=num", options[i]);
                break;
            }
        }

        Thread.sleep(3000);
        selenium.close();
        selenium.stop();
        server.stop();

    }

}

No comments:

Post a Comment