import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.support.ui.Select;
// - Selecting Existing list of values from ListBox
// and check which value is selected
public class WebDriver19 {
/**
* @param args
*/
public static WebDriver oBrowser;
public static String sUrl = "http://www.goibibo.com";
public static void main(String[] args)
{
boolean bIsBrowserOpened;
bIsBrowserOpened = OpenBrowser();
if (bIsBrowserOpened)
{
Set_ListBox_Item_Method3();
//Set_ListBox_Item_Method2_Valid();
//Set_ListBox_Item_Method2_InValid();
CloseBrowser();
}
}
public static boolean OpenBrowser()
{
try
{
oBrowser = new FirefoxDriver();
oBrowser.get(sUrl);
try
{
Thread.sleep(5000L);
}
catch (Exception e)
{
e.printStackTrace();
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
return false;
}
return true;
}
public static void CloseBrowser()
{
oBrowser.close();
}
public static void Set_ListBox_Item_Method1()
{
WebElement oListBox;
oListBox = oBrowser.findElement(By.id("gi_source"));
oListBox.sendKeys("Hyderabad");
}
//For throwing not found exception
public static void Set_ListBox_Item_Method2_Valid()
{
WebElement oListBox;
oListBox = oBrowser.findElement(By.id("gi_source"));
//oListBox.sendKeys("Hyderabad");
try
{
oListBox.findElement(By.xpath("//option[@value='HYD']")).click();
}
catch(NoSuchElementException e)
{
System.out.println("Specified Item Not Found");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void Set_ListBox_Item_Method2_InValid()
{
WebElement oListBox;
oListBox = oBrowser.findElement(By.id("gi_source"));
//oListBox.sendKeys("Hyderabad");
try
{
oListBox.findElement(By.xpath("//option[@value='HYD123']")).click();
}
catch(NoSuchElementException e)
{
System.out.println("Specified Item Not Found");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void Set_ListBox_Item_Method3()
{ //using select class
WebElement oListBox;
Select ListSelect;
oListBox = oBrowser.findElement(By.id("gi_source"));
ListSelect=new Select(oListBox);
ListSelect.selectByValue("HYD");
System.out.println("Selected:= "+ListSelect.getAllSelectedOptions().get(0).getText());
ListSelect.selectByVisibleText("Pune");
System.out.println("Selected:= "+ListSelect.getAllSelectedOptions().get(0).getText());
}
}
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.support.ui.Select;
// - Selecting Existing list of values from ListBox
// and check which value is selected
public class WebDriver19 {
/**
* @param args
*/
public static WebDriver oBrowser;
public static String sUrl = "http://www.goibibo.com";
public static void main(String[] args)
{
boolean bIsBrowserOpened;
bIsBrowserOpened = OpenBrowser();
if (bIsBrowserOpened)
{
Set_ListBox_Item_Method3();
//Set_ListBox_Item_Method2_Valid();
//Set_ListBox_Item_Method2_InValid();
CloseBrowser();
}
}
public static boolean OpenBrowser()
{
try
{
oBrowser = new FirefoxDriver();
oBrowser.get(sUrl);
try
{
Thread.sleep(5000L);
}
catch (Exception e)
{
e.printStackTrace();
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
return false;
}
return true;
}
public static void CloseBrowser()
{
oBrowser.close();
}
public static void Set_ListBox_Item_Method1()
{
WebElement oListBox;
oListBox = oBrowser.findElement(By.id("gi_source"));
oListBox.sendKeys("Hyderabad");
}
//For throwing not found exception
public static void Set_ListBox_Item_Method2_Valid()
{
WebElement oListBox;
oListBox = oBrowser.findElement(By.id("gi_source"));
//oListBox.sendKeys("Hyderabad");
try
{
oListBox.findElement(By.xpath("//option[@value='HYD']")).click();
}
catch(NoSuchElementException e)
{
System.out.println("Specified Item Not Found");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void Set_ListBox_Item_Method2_InValid()
{
WebElement oListBox;
oListBox = oBrowser.findElement(By.id("gi_source"));
//oListBox.sendKeys("Hyderabad");
try
{
oListBox.findElement(By.xpath("//option[@value='HYD123']")).click();
}
catch(NoSuchElementException e)
{
System.out.println("Specified Item Not Found");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void Set_ListBox_Item_Method3()
{ //using select class
WebElement oListBox;
Select ListSelect;
oListBox = oBrowser.findElement(By.id("gi_source"));
ListSelect=new Select(oListBox);
ListSelect.selectByValue("HYD");
System.out.println("Selected:= "+ListSelect.getAllSelectedOptions().get(0).getText());
ListSelect.selectByVisibleText("Pune");
System.out.println("Selected:= "+ListSelect.getAllSelectedOptions().get(0).getText());
}
}