Pages

Wednesday, September 1, 2010

How to print all the available options in a dropdown

    static SeleniumServer server;
    static Selenium selenium;
    public void testPrintDropdownValues() throws InterruptedException
    {
        //Start the server
        try {
            server.start();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("Failed to start the server");
            e.printStackTrace();
        }
        //Stop the server
        server.stop();
      
        //Start the selenium session
        selenium = new DefaultSelenium
                               ("localhost",4444,"*firefox","http://www.google.com");
        selenium.start();
        selenium.open("");
   
        //click on Advanced Search Window
        selenium.click("link=Advanced Search");    
       
        //Print all the available options from the results dropdown in the google advanced search page
        String[] options = selenium.getSelectOptions("name=num");       
        //The above command returns a array of strings(options)
        for (int i = 0; i < options.length; i++) {
            System.out.println("Option: " + i + " is" + options[i]);
        }
       
       
        //Stop & close the selenium browsers
        selenium.close();
        selenium.stop();
    }

No comments:

Post a Comment