WPF code samples

End-to-End: Reserve a flight using WPF objects

The example below demonstrates how to create a program which simulates a flight booking on the Flight GUI sample application.

The example includes a SetSecure step for logging in.

[Test]
public void TestFlightSampleApplication()
{
	using (new FlightGuiSampleWpfApp())
	{
		// Identify the Flight GUI main application window. Assign the Flight GUI window to an IWindow object. 
		_flightGUIAapplicationWindow = Desktop.Describe<IWindow>(new WindowDescription
		{
			ObjectName = @"HP MyFlight Sample Application",
			FullType = @"window",
			WindowTitleRegExp = @"HP MyFlight Sample Application"
		});

		PerformLogin();
		SearchForFlight();
		OpenFlightsTable();
		SelectFlight();
		FinishOrder();
		VerifyOrderCompleted();
		ExitFlightApp();
	}
}

// This method shows how to login in to the Flight GUI sample application.
private void PerformLogin()
{
	// Identify the User Name field.
	var userNameEdit = _flightGUIAapplicationWindow.Describe<IEditField>(new EditFieldDescription
	{
		ObjectName = @"agentName"
	});

	// Enter a value for the User Name.
	userNameEdit.SetText("john"); 

	// Identify the password field
	var passwordEdit = _flightGUIAapplicationWindow.Describe<IEditField>(new EditFieldDescription
	{
		ObjectName = @"password"
	});

	// Enter a value for the password. You use the encryption utility to extract the encrypted string (see the LeanFT documentation). 
	// Plain text is used for the example code.
	passwordEdit.SetSecure("HP"); 
    
	// Identify the OK button.
	var okButton = _flightGUIAapplicationWindow.Describe<IButton>(new ButtonDescription
	{
		Text = @"OK",
		ObjectName = @"okButton"
	});
    
	// Click the OK button.
	okButton.Click(); 
}

private void SearchForFlight()
{
	ChooseFlightSourceAndDestination();
	ChooseFlightDate();
	ChooseFlightClassAndNumberOfTickets();
}

// This method shows how to select the flight destination details from a combo box. 
private void ChooseFlightSourceAndDestination()
{
	// Identify the "from city" combo box
	var fromCityComboBox = _flightGUIAapplicationWindow.Describe<IComboBox>(new ComboBoxDescription
	{
		ObjectName = @"fromCity"
	});
    
	// Select an item in the combo box using a (0-based) numeric index. 
	fromCityComboBox.Select(3);

	// Identify the "to city" combo box.
	var toCityComboBox = _flightGUIAapplicationWindow.Describe<IComboBox>(new ComboBoxDescription
	{
		ObjectName = @"toCity"
	});
    
	// Select an item in the combo box using a string value. 
	toCityComboBox.Select("London");
}

/* This method shows how to select the flight dates. The date picker is identified through 
the main application window and not through the Flight GUI application window. */
private void ChooseFlightDate()
{
	// Identify the date picker control. The date picker is identified through the main window and not directly from the Flight GUI applications. 
	var flightDatePicker = Desktop.Describe<IWindow>(new WindowDescription
	{
		ObjectName = @"HP MyFlight Sample Application",
		FullType = @"window",
		WindowTitleRegExp = @"HP MyFlight Sample Application"
	}).Describe<ICalendar>(new CalendarDescription
	{
		ObjectName = @"datePicker"
	});

	// Select the flight date.
	flightDatePicker.SetDate(new DateTime(year:2025, month:8, day:1)); 
}

// This method shows how to additional flight details: number of tickets, flight class.
private void ChooseFlightClassAndNumberOfTickets()
{
	// Identify the flight class combo box.
	var flightClassCombobox = _flightGUIAapplicationWindow.Describe<IComboBox>(new ComboBoxDescription
	{
		ObjectName = @"Class",
		FullType = @"combo box"
	});

	// Fly "Business!"
	flightClassCombobox.Select("Business"); 

	// Identify the number of tickets combo box.
	var numOfTickets = _flightGUIAapplicationWindow.Describe<IComboBox>(new ComboBoxDescription
	{
		ObjectName = @"numOfTickets"
	});
    
	// Select the number of tickets. 
	numOfTickets.Select("2"); 
}

// This method invokes the submit button on the page to view the available flights. 
private void OpenFlightsTable()
{
	// Identify the "FIND FLIGHTS" button
	var findFlightsButton = _flightGUIAapplicationWindow.Describe<IButton>(new ButtonDescription
	{
		Text = @"FIND FLIGHTS",
		ObjectName = @"FIND FLIGHTS"
	});

	// Click the submit button.
	findFlightsButton.Click();
}

/* This method shows how to select a row in a data grid. 
   The grid contains the list of flights available. */
private void SelectFlight()
{
	// Identify the flights data grid (ITable).
	var flightsDataGrid = _flightGUIAapplicationWindow.Describe<ITable>(new TableDescription
	{
		ObjectName = @"flightsDataGrid"
	});

	// Select the third row in the data grid. 
	flightsDataGrid.SelectRow(2); 

	// Identify the button to submit the page.
	var selectFlightButton = _flightGUIAapplicationWindow.Describe<IButton>(new ButtonDescription
	{
		Text = @"SELECT FLIGHT",
		ObjectName = @"selectFlightBtn"
	});

	// Click the "Select Flight" button.
	selectFlightButton.Click(); 
}

// This method shows how to complete the order by entering the final details for the flight.
private void FinishOrder()
{
	// Identify the passenger name edit field.
	var passengerNameField = _flightGUIAapplicationWindow.Describe<IEditField>(new EditFieldDescription
	{
		ObjectName = @"passengerName"
	});
    
	// Enter the passenger name.
	passengerNameField.SetText("John Doe"); 

	// Identify the total price field.
	var totalPrice = _flightGUIAapplicationWindow.Describe<IUiObject>(new UiObjectDescription
	{
		ObjectName = @"totalPrice"
	});
	// Ensure the total price is correct ($652.00).
	Assert.AreEqual("$652.00", totalPrice.Text); 

	// Identify the "Order" button.
	var orderButton = _flightGUIAapplicationWindow.Describe<IButton>(new ButtonDescription
	{
		Text = @"ORDER",
		ObjectName = @"orderBtn"
	});

	// Clicks the "Order" button to perform the flight reservation.
	orderButton.Click(); 
}

// This method shows how to ensure the order was completed successfully. Use a delegate to be notified when the order is complete. 
private void VerifyOrderCompleted()
{
	// Identify the order completed text object.
	var orderCompletedControl = _flightGUIAapplicationWindow.Describe<IUiObject>(new UiObjectDescription
	{
		ObjectName = @"orderCompleted"
	});
   
	// The next line shows how to use the WaitUntil method with C# function delegate to wait for the control to become visible.
	// The WaitUntil method passes the control's interface instance, in this case, an IUiObject, to the C# function delegate as a parameter. 
	// The C# function delegate expects a single parameter and returns a boolean value.
	// In this sample, the C# function delegate returns the IsVisible value of the control passed to it as a parameter.
	// The WaitUntil method then waits for the C# function delegate to return true, for a default amount of time.
	// The C# function delegate is called every few milliseconds, until it returns true, or until the default timeout expires.
	bool controlIsVisible = orderCompletedControl.WaitUntil((control) => { return control.IsVisible; });
    
	// Verify the control is visible.
	Assert.True(controlIsVisible);

	// Get the "order completed" control's text.
	string orderCompletedControlText = orderCompletedControl.Text;
    
	// Verify the "order completed" control's text ends with the string "completed".
	Assert.True(orderCompletedControlText.EndsWith("completed")); 
}

private void ExitFlightApp()
{
	_flightGUIAapplicationWindow.Close();
}

Back to top

Use WaitUntil to complete a flight reservation

private void VerifyOrderCompleted()
{
	// Identify the order completed text
	var orderCompletedControl = _flightGUIAapplicationWindow.Describe<IUiObject>(new UiObjectDescription
	{
		ObjectName = @"orderCompleted"
	});
    
	// The next line shows how to use the WaitUntil method with C# Func delegate to wait for the control to become visible.
	// The WaitUntil method passes the control's interface instance - in this case, an IUiObject - to the C# Func delegate as a parameter. 
	// The C# Func delegate expects a single parameter and returns a boolean value.
	// In this sample, the C# Func delegate returns the IsVisible value of the control passed to it as a parameter.
	// The WaitUntil method then waits for the C# Func delegate to return true, for a default amount of time.
	// The C# Func delegate is called every few milliseconds, until it returns true, or until the default timeout expires.
	bool controlIsVisible = orderCompletedControl.WaitUntil((control) => { return control.IsVisible; });
	Assert.True(controlIsVisible); // Verify the control is visible
    
	string orderCompletedControlText = orderCompletedControl.Text; // Get the "order completed" control's text
	Assert.True(orderCompletedControlText.EndsWith("completed")); // Verify the "order completed" control's text is ending with the string "completed".
}

Back to top

Select items from a combo box control

This example selects items from a combo box using index and string values.

private void ChooseFlightSourceAndDestination()
{
	// Identify the "from city" combobox
	var fromCityComboBox = _flightGUIAapplicationWindow.Describe<IComboBox>(new ComboBoxDescription
	{
		ObjectName = @"fromCity"
	});

	// Select an item in the combo box using a (0-based) numeric index. 
	fromCityComboBox.Select(3);

	// Identify the "to city" combo box
	var toCityComboBox = _flightGUIAapplicationWindow.Describe<IComboBox>(new ComboBoxDescription
	{
		ObjectName = @"toCity"
	});

	// Select an item in the combo box using a string value. 
	toCityComboBox.Select("London");
}

Back to top

Set the date in a DatePicker control

This example locates and sets the date of the date picker control.

private void ChooseFlightDate()
{
	// Identify the date picker control. The date picker is identified through the main window and not directly from the Flight GUI applications.
	var flightDatePicker = Desktop.Describe<IWindow>(new WindowDescription
	{
		ObjectName = @"HP MyFlight Sample Application",
		FullType = @"window",
		WindowTitleRegExp = @"HP MyFlight Sample Application"
	}).Describe<ICalendar>(new CalendarDescription
	{
		ObjectName = @"datePicker"
	});
	// Select the flight date.
	flightDatePicker.SetDate(new DateTime(year:2025, month:8, day:1));
}

Back to top

Select a row in a DataGrid control

This example selects a row in the data grid.

private void SelectFlight()
{
	// Identify the flights data grid (ITable).
	var flightsDataGrid = _flightGUIAapplicationWindow.Describe<ITable>(new TableDescription
	{
		ObjectName = @"flightsDataGrid"
	});

	// Select the third row in the data grid.
	flightsDataGrid.SelectRow(2); 

	var selectFlightButton = _flightGUIAapplicationWindow.Describe<IButton>(new ButtonDescription
	{
		Text = @"SELECT FLIGHT",
		ObjectName = @"selectFlightBtn"
	});

	// Click the "Select Flight" button.
	selectFlightButton.Click(); 
}

Back to top