Wednesday, January 19, 2011

How to get the values from the Drop Down menu.


package smoke;

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class dropdownvalue {
     public Selenium selenium;
     public SeleniumServer seleniumserver;

       @BeforeClass
       public void setUp() throws Exception {
     RemoteControlConfiguration rc = new RemoteControlConfiguration();
     seleniumserver = new SeleniumServer(rc);
     selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com");
     seleniumserver.start();
     selenium.start();
     }

     @Test
     public void testDefaultTNG()throws Exception {
     selenium.open("http://www.google.com");


     selenium.click("link=Advanced Search");
     selenium.waitForPageToLoad("50000");
     // Print all the available options from the results dropdown in the
     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]);
         }
         //select value from dropdown
             selenium.select("num", "label=30 results");
             //Print the selected value from dropdown
         String s=selenium.getSelectedValue("name=num");
         System.out.println("selected value is "+s);
         
         
     }
     @AfterClass
      public void tearDown() throws InterruptedException{
     selenium.stop();
     seleniumserver.stop();
     
      }
      }

No comments:

Post a Comment