November 24, 2010

Print values in the dropdown

The below script explains you how to get the values in a dropdown and print the values to a notepad or excel sheet


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;


public class Dropdowngoogle {
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, "*firefox", "http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testDropdowngoogle()throws Exception {
selenium.open("http://www.google.com");
//click on Advanced Search Window
    selenium.click("link=Advanced Search");  
    selenium.waitForPageToLoad("5000");
     //Print all the available options from the results dropdown in the google advanced search page
    String[] option = selenium.getSelectOptions("name=lr");      
    //The above command returns a array of strings(options)
   //write results to excel and note pad
    File file = new File("C:/Dropdownvalues.xls");//excel
    File file1 = new File("C:/Dropdownvalues.txt");//notepad
    BufferedWriter out = new BufferedWriter(new FileWriter(file));//excel
    BufferedWriter out1 = new BufferedWriter(new FileWriter(file1));//notepad
    out.write("Options in drop down\n");//Excel
    out1.write("Options in drop down\n");//notepad
    for (int i = 0; i < option.length; i++) {
        System.out.println("Option: " + i + " is" + option[i]);
        out.write(""+option[i]);
        out.newLine();      
        out1.write(""+option[i]);
        out1.newLine();      
    }
    out.close();//excel
    out1.close();//notepad
  }
 @AfterClass
 public void tearDown()throws Exception {
selenium.stop();
seleniumserver.stop();

 }
 }

9 comments:

  1. This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing...
    Selenium Training in Chennai|Selenium Training

    ReplyDelete
  2. And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
    selenium training in chennai

    ReplyDelete
  3. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.

    Best Java Training Institute Chennai

    Amazon Web Services Training in Chennai


    ReplyDelete
  4. Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some recommendations for your blog you might be interested in hearing.

    Hadoop Training in Chennai

    ReplyDelete