Tuesday 10 March 2015

Selecting item from ListBox and check which value is selected

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());

}

}

11 comments:

  1. nice programming on Selenium. Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. For more details.

    Check this site tekslate for indepth tutorials/selenium-tutorials/

    Go here if you’re looking for information on tutorials/selenium-tutorials/

    ReplyDelete
  2. nice programming on Selenium. Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. For more details.

    Check this site tekslate for indepth tutorials/selenium-tutorials/

    Go here if you’re looking for information on tutorials/selenium-tutorials/

    ReplyDelete
  3. Great post!
    Thanks for sharing this information.post more articles like this...........
    We provide Quality Trainings for all courses by who is having professional experience & hands on real time experts.
    Selenium Online Training

    ReplyDelete
  4. Thanks you for the awesome information in your blog on  Selenium training in Chennai

    ReplyDelete
  5. Thanks you for the awesome information in your blog on  Selenium training in Chennai

    ReplyDelete
  6. Thank you! Learn Software Testing Tools Training by Best Training Institute QEdge Technologies.
    Selenium Training in Hyderabad

    ReplyDelete
  7. This is very informative blog and article thank you for sharing with us keep posting more information about salesforce trainingselenium training, selenium online training, selenium course,learn selenium course

    ReplyDelete

  8. Advanced Selenium Framework Training in Chennai by Vishwa

    Hi, Reach Mr.Vishwa for Best Selenium and Coded UI Training in Chennai with real time project assistance. This people will teach everything from the basics up to advanced level scenarios from frameworks.

    I learned from Mr.Vishwa and Raj they are very good automation people for Selenium Frameworks. After joined with them I learned many things and now I am writing automation scripts in own project. Vishwa 9003085882
    selenium training in chennai

    ReplyDelete
  9. Thanks for sharing great information in your blog.

    Selenium training institute which provides Real Time Hands on Training…THE CREATING EXPERTS

    http://thecreatingexperts.com/selenium-training-in-chennai/


    contact-8122241286

    ReplyDelete