Showing posts with label Dropdown. Show all posts
Showing posts with label Dropdown. Show all posts

December 13, 2013

Handling Cascading Dropdowns (Example for AJAX)

December 13, 2013 3
Handling Cascading Dropdowns (Example for AJAX)
Below post explains you how to handle Cascading Dropdowns. It a good example for Ajax.

Each time the selection of one the DropDownList controls changes, the CascadingDropDown makes a call to a specified web service to retrieve the list of values for the next DropDownList in the set.

CascadingDropDown enables a common scenario in which the contents of one list depends on the selection of another list and does so without having to embed the entire data set in the page or transfer it to the client at all.

Below code explains how to handle Cascading drop downs:


August 4, 2013

Get Values from Dropdown (Birthday field in Gmail registration )

August 04, 2013 0
Below is the sample script to get  values or options from a dropdown.

Below is the image of Birth Month dropdown





The Birth Month field is not a Drop down. If you want to get all the values from dropdown first we need to click on the arrow mark and then we can get all the values of dropdown.

Here in the below example..first click on the drop down arrow then all the elements (Month options) will be visible and then you can get all the values of dropdown.
===============================================================
package one;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Gmail_Reg {
public WebDriver driver;

@Before
public void setUp() throws Exception {
//Specify the browser
driver=new FirefoxDriver();
//declare globally wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//maximize the window
driver.manage().window().maximize();
}

@After
public void tearDown() throws Exception {
//close the browser
driver.quit();
}

@Test
public void testGmail_Reg() throws Exception {


driver.get("https://accounts.google.com/SignUp");
//click on the arrow mark
driver.findElement(By.xpath("//label[@id='month-label']/span/div/div")).click();
//get all the vlaues of dropdown
List x=driver.findElements(By.xpath("//div[@class='goog-menu goog-menu-vertical']/div"));
System.out.println("Size of the dropdown : "+x.size());
//print dropdown options
for (int i = 0; i < x.size(); i++) {
System.out.println(x.get(i).getText());
}
Thread.sleep(5000);

}

}
=============================================================

Output:
Size of the dropdown : 12
January
February
March
April
May
June
July
August
September
October
November
December

===================================================================

December 8, 2010

Verify value in drop down list.

December 08, 2010 0
Verify value in drop down list.
How to verify value in drop down list.


Its difficult to find element with some specific attributes.
Take an example How to find some value is in drop down list or not? 
To find element drop down is easy but to find a drop down having some specific value in their list is difficult.


Go to www.google.com  and click Advanced Search, you will see there are some drop down boxes.

How to verify a specific value "50 results" is in drop down list.




Here is the script for finding the value is in drop down or not





package test;


import com.thoughtworks.selenium.*;


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




public class Dropdownall {
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://");
seleniumserver.start();
selenium.start();
}


@Test
public void testDropdownall()throws Exception {

selenium.open("http://www.google.com");
selenium.windowMaximize();
selenium.click("link=Advanced Search");
selenium.waitForPageToLoad("50000");

//Verifying Element is Present or not here i used xpath to find that

if(selenium.isElementPresent("//select[@name='num']/option[contains(text(),'50 results')]"))
{
System.out.println("ys");
}


}
   
 @AfterClass
 public void tearDown()throws Exception {
selenium.stop();
seleniumserver.stop();
 
 }
 }



if the value 50results are in drop down it will display yes or else u will not get any message..



//select[@name='num']/option[contains(text(),'50 results')]





Here name=num is the name of the dropdown u can see it in HTML code...
We used contains (text(),50 results') .....

Select,identify selected and print values in dropdown

December 08, 2010 55
Select,identify selected and print values in dropdown
Here is the post which will explain you 

How to select a value from drop down
Identify the selected value in drop down.
Display the values in drop down
Verifying Element is Present or not here i used xpath to find that


package test;


import com.thoughtworks.selenium.*;


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




public class Dropdownselectvalue {
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://");
seleniumserver.start();
selenium.start();
}


@Test
public void testDropdownselectvalue()throws Exception {

selenium.open("http://www.google.com");
selenium.windowMaximize();
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);

//Verifying Element is Present or not here i used xpath to find that if(selenium.isElementPresent("//select[@name='num']/option[contains(text(),'50 results')]")) { System.out.println("ys"); }


}
   
 @AfterClass
 public void tearDown()throws Exception {
selenium.stop();
seleniumserver.stop();
 
 }
 }


.

November 25, 2010

Print values in the dropdown using Xpath

November 25, 2010 2
Print values in the dropdown using Xpath
The below script explains you how to get the values in a dropdown and print the values to a notepad or excel sheet using the xpath...




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 Dropdowngooglexpath {
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
//here i am using xpath to identify the element
    String[] option = selenium.getSelectOptions("xpath=/html/body/table[2]/tbody/tr/td/table/tbody/tr/td/div/form/div/table[2]/tbody/tr[1]/td[2]/select");      
    //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();

 }
 }