Learning by Doing: Sample Code to Illustrate Use of Selenium WebDriver 2
To see how Selenium WebDriver 2 works, copy the code below into a java file into your IDE and run it. A Firefox browser opens, it navigates to a page for searching for American Airlines flights, and the reservation information is input and submitted.
This test is constructed to run as a local Selenium WebDriver script with Java language bindings. Prerequisites for running this particular test are as follows, and assistance for setting up the prerequisites are provided in the Installation page of this website.
- An IDE (Integrated Development Environment) such as Eclipse is installed on your machine.
- A project, package (named "airlineReservationTests"), and Java class have been created to use for working through a Selenium WebDriver example.
- The Selenium WebDriver & Client version for Java language binding has been downloaded, and the Selenium Library has been included or referenced in your project.
- The highest version of Firefox compatible with the version of Selenium downloaded is installed (you may find this in the release notes for Selenium WebDriver. If when you run the code and get the following error, install an older version of Firefox: "Failed to start up socket within 45000 ms. Attempted to connect to the following addresses: 127.0.0.1:7055")
Sample Code: copy and paste
Copy the following code (in teal color font) and paste it into the FirstSampleTest.java file in Eclipse, replacing any existing code in that file.
package airlineReservationTests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.Select;
// Navigate to Advanced Flight Search Page and fill in search criteria and submit
public class FirstSampleTest {
public static void main(String[] args) {
String Expected_SearchInputPageTitle = "Book A Flight | Search Flight Schedules | Round-Trip Airfare | AA.com";
String Expected_SearchResultsPageTitle = "Airline Reservations | Choose Flights | Results | Book Flights | AA.com";
String Error_Navigation = "Navigation ERROR: ";
// create and set browser profile for Firefox to insure language is english-us
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", "en-us");
WebDriver driver = new FirefoxDriver(profile); // create firefox driver
driver.get("http://www.aa.com/homePage.do"); // go to the AA main website
driver.manage().window().maximize(); // resize window to max size
// wait for "Refine Your Search" link element to be present before defining/accessing it, so that we don't end with an exception
WebElement Lnk_AdvSearch = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("aaMoreDetails")));
Lnk_AdvSearch.click();
// Check that the link for advanced search gets us to the right page. make this a function, since it will be used every time we change pages in a flow
String Txt_SearchInputPageTitle = driver.getTitle();
if (Txt_SearchInputPageTitle.compareTo(Expected_SearchInputPageTitle) !=0 ) //actual page title not equal to expected page title
{
// log an error with the given error message Error_Navigation. use a common function to log validation errors
// stop processing
System.out.println(Error_Navigation + "Title of Current Page is " + Txt_SearchInputPageTitle + "but was expecting " + Expected_SearchInputPageTitle);
return;
}
else
System.out.println("Title of Current Page is " + Txt_SearchInputPageTitle + " which equals expected title " + Expected_SearchInputPageTitle);
// fill in all fields in the advanced search page
WebElement Rad_FlightOnly = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("air_room_car_0")));
Rad_FlightOnly.click();
WebElement Txt_OriginAirport = driver.findElement(By.id("flightSearchForm.originAirport"));
Txt_OriginAirport.clear();
Txt_OriginAirport.sendKeys("RDU");
WebElement Txt_DestinationAirport = driver.findElement(By.id("flightSearchForm.destinationAirport"));
Txt_DestinationAirport.clear();
Txt_DestinationAirport.sendKeys("MIA");
Select Lst_AltOriginAirport = new Select(driver.findElement(By.id("flightSearchForm.originAlternateAirportDistance")));
Lst_AltOriginAirport.selectByValue("30");
Select Lst_AltDestAirport = new Select(driver.findElement(By.id("flightSearchForm.destinationAlternateAirportDistance")));
Lst_AltDestAirport.selectByValue("60");
WebElement Rad_ResultBySchedule = driver.findElement(By.id("flightSearchForm.searchType.schedule"));
Rad_ResultBySchedule.click();
Select Lst_NumberStops = new Select(driver.findElement(By.id("flightSearchForm.numberOfStopsSchedule")));
Lst_NumberStops.selectByVisibleText("None");
Select Lst_NumberResultsShown = new Select(driver.findElement(By.id("flightSearchForm.numberOfFlightsToDisplay")));
Lst_NumberResultsShown.selectByValue("40");
Select Lst_DepartureMonth = new Select(driver.findElement(By.id("flightSearchForm.flightParams.flightDateParams.travelMonth")));
Lst_DepartureMonth.selectByValue("12");
Select Lst_DepartureDay = new Select(driver.findElement(By.id("flightSearchForm.flightParams.flightDateParams.travelDay")));
Lst_DepartureDay.selectByValue("1");
Select Lst_TimeOfDay = new Select(driver.findElement(By.id("flightSearchForm.flightParams.flightDateParams.searchTime")));
Lst_TimeOfDay.selectByVisibleText("Morning");
Select Lst_ReturnMonth = new Select(driver.findElement(By.id("flightSearchForm.returnDate.travelMonth")));
Lst_ReturnMonth.selectByValue("12");
Select Lst_ReturnDay = new Select(driver.findElement(By.id("flightSearchForm.returnDate.travelDay")));
Lst_ReturnDay.selectByValue("21");
Select Lst_ReturnTimeOfDay = new Select(driver.findElement(By.id("flightSearchForm.returnDate.searchTime")));
Lst_ReturnTimeOfDay.selectByVisibleText("Evening");
Select Lst_NumberAdults = new Select(driver.findElement(By.id("flightSearchForm.adultPassengerCount")));
Lst_NumberAdults.selectByValue("2");
Select Lst_NumberSeniors = new Select(driver.findElement(By.id("flightSearchForm.seniorPassengerCount")));
Lst_NumberSeniors.selectByValue("1");
Select Lst_NumberYoungAdults = new Select(driver.findElement(By.id("flightSearchForm.youngAdultPassengerCount")));
Lst_NumberYoungAdults.selectByValue("1");
Select Lst_NumberChildren = new Select(driver.findElement(By.id("flightSearchForm.childPassengerCount")));
Lst_NumberChildren.selectByValue("1");
Select Lst_NumberInfant = new Select(driver.findElement(By.id("flightSearchForm.infantPassengerCount")));
Lst_NumberInfant.selectByValue("1");
//use default fare pref until solve issues
//Select Lst_FarePreference = new Select(driver.findElement(By.id("flightSearchForm.coachRestrictedCabinClass")));
//Lst_FarePreference.selectByValue("Refundable");
WebElement Rad_Carrier = driver.findElement(By.id("flightSearchForm.carrierAll"));
Rad_Carrier.click();
Select Lst_CountryResidence = new Select(driver.findElement(By.id("flightSearchForm.countryPointOfSale")));
Lst_CountryResidence.selectByValue("US");
WebElement Btn_ContinueSearch = driver.findElement(By.id("flightSearchForm.button.go"));
Btn_ContinueSearch.click();
// check that we get to the expected page with search results
String Txt_SearchResultsPageTitle = driver.getTitle();
if (Txt_SearchResultsPageTitle.compareTo(Expected_SearchResultsPageTitle) !=0 ) //actual page title not equal to expected page title
{
// log an error with the given error message Error_Navigation. use a common function to log validation errors
// stop processing
System.out.println(Error_Navigation + "Title of Current Page is " + Txt_SearchResultsPageTitle + "but was expecting " + Expected_SearchResultsPageTitle);
return;
}
}
}