WPF code samples (in .NET)

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 main Flight GUI sample application window
		_flightGUIAapplicationWindow = Desktop.Describe<IWindow>(new WindowDescription
		{
			ObjectName = @"Micro Focus MyFlight Sample",
			FullType = @"window",
			WindowTitleRegExp = @"Micro Focus MyFlight Sample"
		});

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

private void PerformLogin()
{
	// Sample showing how to perform the actions to login in to the Flight GUI sample application

	// Identify the user name field
	var userNameEdit = _flightGUIAapplicationWindow.Describe<IEditField>(new EditFieldDescription
	{
		ObjectName = @"agentName"
	});            
	userNameEdit.SetText("john"); // Fill in the user name field
            
	#region Doc_Wpf_SetSecure
	// 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");
	#endregion

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

#region Doc_Wpf_Combobox_Samples
// 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");
}
#endregion

#region Doc_Wpf_SettingWpfDateControl
// 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 = @"Micro Focus MyFlight Sample",
		FullType = @"window",
		WindowTitleRegExp = @"Micro Focus MyFlight Sample"
	}).Describe<ICalendar>(new CalendarDescription
	{
		ObjectName = @"datePicker"
	});
	// Select the flight date.
	flightDatePicker.SetDate(new DateTime(year:2025, month:8, day:1));
}
#endregion

private void ChooseFlightClassAndNumberOfTickets()
{
	// Identify the flight class combobox
	var flightClassCombobox = _flightGUIAapplicationWindow.Describe<IComboBox>(new ComboBoxDescription
	{
		ObjectName = @"Class",
		FullType = @"combo box"
	});            
	flightClassCombobox.Select("Business"); // Choose the "Business" flight class

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

private void OpenFlightsTable()
{
	// Identify the "FIND FLIGHTS" button
	var findFlightsButton = _flightGUIAapplicationWindow.Describe<IButton>(new ButtonDescription
	{
		Text = @"FIND FLIGHTS",
		ObjectName = @"FIND FLIGHTS"
	});
	findFlightsButton.Click(); // click on the button
}

#region Doc_Wpf_SettingWpfDataGridControlRow
// 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(); 
}
#endregion

private void FinishOrder()
{
	// Identify the passenger name edit field
	var passengerNameField = _flightGUIAapplicationWindow.Describe<IEditField>(new EditFieldDescription
	{
		ObjectName = @"passengerName"
	});
	passengerNameField.SetText("John Doe"); // Enter the passenger name

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

	// Identify the "Order" button
	var oderButton = _flightGUIAapplicationWindow.Describe<IButton>(new ButtonDescription
	{
		Text = @"ORDER",
		ObjectName = @"orderBtn"
	});
	oderButton.Click(); // Click on the "Order" button to perform the flight reservation
}

#region Doc_Wpf_WaitUntilSample
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; });
	Verify.IsTrue(controlIsVisible, "Verify Control is Visible", "Verify the 'order completed' control is visible."); // Verify the control is visible
            
	string orderCompletedControlText = orderCompletedControl.Text; // Get the "order completed" control's text
	Verify.IsTrue(orderCompletedControlText.EndsWith("completed"), "Verify Order Completed", "Verify the 'order completed' control's text is ending with the string 'completed'."); // Verify the "order completed" control's text is ending with the string "completed".
}
#endregion

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

Back to top

Use WaitUntil to complete a flight reservation

Back to top

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; });
	Verify.IsTrue(controlIsVisible, "Verify Control is Visible", "Verify the 'order completed' control is visible."); // Verify the control is visible
            
	string orderCompletedControlText = orderCompletedControl.Text; // Get the "order completed" control's text
	Verify.IsTrue(orderCompletedControlText.EndsWith("completed"), "Verify Order Completed", "Verify the 'order completed' control's text is ending with the string 'completed'."); // Verify the "order completed" control's text is ending with the string "completed".
}

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 = @"Micro Focus MyFlight Sample",
		FullType = @"window",
		WindowTitleRegExp = @"Micro Focus MyFlight Sample"
	}).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

Change a toggle button state

This example locates a toggle button and changes the toggle button state.

 [Test]
public void TestWpfToggleButton()
{
	// Identify the application and assign it to an IWindow object. 
	var appMainWindow = Desktop.Describe<IWindow>(new WindowDescription
	{
		ObjectName = @"MainWindow",
		FullType = @"window",
		WindowTitleRegExp = @"MainWindow"
	});

	// Identify the WPF toggle button. 
	var buttonWithToggleButtonSupport = appMainWindow.Describe<IButton>(new ButtonDescription
	{
		Text = @"Toggle Button",
		ObjectName = @"Button4"
	});
	IToggleButton toggleButton = buttonWithToggleButtonSupport.ToggleButton;
            
	// Sets the toggle button's state to checked.
	toggleButton.Set(CheckedState.Checked); 

	// Changes the toggle button state.
	if (toggleButton.IsChecked)
	{
		toggleButton.Set(CheckedState.Unchecked);
	}
	else
	{
		toggleButton.Set(CheckedState.Checked);
	}
}

Back to top