WPF code sample (in Java)

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() throws GeneralLeanFtException, ParseException {
		
	flightGUIAapplicationWindow = Desktop.describe(Window.class, new WindowDescription.Builder().objectName("Micro Focus MyFlight Sample")
		.fullType("window").windowTitleRegExp("Micro Focus MyFlight Sample").build());
		
	performLogin();
	searchForFlight();
	openFlightsTable();
	selectFlight();
	finishOrder();
	verifyOrderCompleted();
	exitFlightApp();
}

private void performLogin() throws GeneralLeanFtException{
	// Sample showing how to perform the actions to login in to the Flight GUI sample application
		
	// Identify the user name field
	EditField userNameEdit = flightGUIAapplicationWindow.describe(EditField.class, new EditFieldDescription.Builder()
		.objectName("agentName").build());            
	userNameEdit.setText("john"); // Fill in the user name field
                
	// Identify the password field
	EditField passwordEdit = flightGUIAapplicationWindow.describe(EditField.class, new EditFieldDescription.Builder()
		.objectName("password").build());
        
	// Fill in the password field. You should use the encryption utility to find out the encrypted string (see the LeanFT documentation). 
	// Here, for sample and clarity, we use plain text.
	passwordEdit.setSecure("HP");
        
	// Identify the OK button
	Button okButton = flightGUIAapplicationWindow.describe(Button.class, new ButtonDescription.Builder()
		.text("OK").objectName("okButton").build());
	okButton.click(); // Click on the OK button
}
	
private void searchForFlight() throws GeneralLeanFtException, ParseException{
	chooseFlightSourceAndDestination();
	chooseFlightDate();
	chooseFlightClassAndNumberOfTickets();
}

private void chooseFlightSourceAndDestination() throws GeneralLeanFtException{
	// Identify the "from city" combobox
	ComboBox fromCityComboBox = flightGUIAapplicationWindow.describe(ComboBox.class, new ComboBoxDescription.Builder()
		.objectName("fromCity").build());
        
	// Select a combobox item using a numeric index. The index of "Los Angeles" in the combobox is 3 (The combobox indexes are 0 based).
	fromCityComboBox.select(3);

	// Identify the "to city" combobox
	ComboBox toCityComboBox = flightGUIAapplicationWindow.describe(ComboBox.class, new ComboBoxDescription.Builder()
		.objectName("toCity").build());
        
	// Select a combobox item using a string value. We will select the item with the string "London"
	toCityComboBox.select("London");
}


private void chooseFlightDate() throws GeneralLeanFtException, ParseException{
	// Identify the date picker control.
	Calendar flightDatePicker = flightGUIAapplicationWindow.describe(Calendar.class, new CalendarDescription.Builder()
		.objectName("datePicker").build());
	SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	Date date = sdf.parse("1/8/2025");
	flightDatePicker.setDate(date); // Choose flight date - choose August 1st, 2025
}						
	
private void chooseFlightClassAndNumberOfTickets() throws GeneralLeanFtException{
	// Identify the flight class combobox
	ComboBox flightClassCombobox = flightGUIAapplicationWindow.describe(ComboBox.class, new ComboBoxDescription.Builder()
		.objectName("Class").fullType("combo box").build());
	flightClassCombobox.select("Business");
		
	// Identify the number of tickets combobox
	ComboBox numOfTickets = flightGUIAapplicationWindow.describe(ComboBox.class, new ComboBoxDescription.Builder()
		.objectName("numOfTickets").build());
	numOfTickets.select("2"); // Choose 2 tickets to buy
}
	
private void openFlightsTable() throws GeneralLeanFtException{
	// Identify the "FIND FLIGHTS" button
	Button findFlightsButton = flightGUIAapplicationWindow.describe(Button.class, new ButtonDescription.Builder()
		.text("FIND FLIGHTS").objectName("FIND FLIGHTS").build());
	findFlightsButton.click(); // click on the button
}	
						
private void selectFlight() throws GeneralLeanFtException{
	// Identify the flights data grid
	Table flightsDataGrid = flightGUIAapplicationWindow.describe(Table.class, new TableDescription.Builder()
		.objectName("flightsDataGrid").build());
	flightsDataGrid.selectRow(2); // Select the third row in the data grid

	Button selectFlightButton = flightGUIAapplicationWindow.describe(Button.class, new ButtonDescription.Builder()
		.text("SELECT FLIGHT").objectName("selectFlightBtn").build());
	selectFlightButton.click(); // Click on the "Select Flight" button
}						
	
private void finishOrder() throws GeneralLeanFtException{
	// Identify the passenger name edit field
	EditField passengerNameField = flightGUIAapplicationWindow.describe(EditField.class, new EditFieldDescription.Builder()
		.objectName("passengerName").build());
	passengerNameField.setText("John Doe"); // Enter the passenger name

	// Identify the total price field
	UiObject totalPrice = flightGUIAapplicationWindow.describe(UiObject.class, new UiObjectDescription.Builder().objectName("totalPrice").build());
	String expectedPrice = "$652.00";
	String actualPrice = totalPrice.getText();
	Verify.areEqual(expectedPrice, actualPrice, "Verify Total Price", "Verify that the total price is correct ($652.00)."); // Verify the price is $652.00
        
	// Identify the "Order" button
	Button oderButton = flightGUIAapplicationWindow.describe(Button.class, new ButtonDescription.Builder().text("ORDER").objectName("orderBtn").build());
	oderButton.click(); // Click on the "Order" button to perform the flight reservation
}	
						
private void verifyOrderCompleted() throws GeneralLeanFtException{
	// Identify the order completed text
	UiObject orderCompletedControl = flightGUIAapplicationWindow.describe(UiObject.class, new UiObjectDescription.Builder().objectName("orderCompleted").build());
		
	WaitUntilEvaluator<UiObject> evaluator = new WaitUntilEvaluator<UiObject>() {
		public boolean evaluate(UiObject testObject) {
			try {
				return testObject.isVisible();
			} catch (GeneralLeanFtException e) {
			return false;
			}
		};	
	};
	boolean result = waitUntil(orderCompletedControl, evaluator, 5000);
	Verify.isTrue(result, "Verify Order Completed Control Exists", "Wait to verify the order completed control appears.");
	String orderCompletedControlText = orderCompletedControl.getText(); // Get the "order completed" control's text

	Verify.isTrue(orderCompletedControlText.endsWith("completed"), "Verify Order Completed Control Text", "Verify that the order completed control contains the text 'completed'.");
}						
	
private void exitFlightApp(){
	wpfFlightAppProcess.destroy();
}

Back to top

Change a toggle button state

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

@Test
public void testWpfToggleButton() throws GeneralLeanFtException {
	// Identify the application that has the toggle button.
	Window appMainWindow = Desktop.describe(Window.class, new WindowDescription.Builder().objectName("MainWindow").fullType("window").windowTitleRegExp("MainWindow").build());

	// Identify the button that supports the WPF toggle mechanism.
	Button buttonWithToggleButtonSupport = appMainWindow.describe(Button.class, new ButtonDescription.Builder().text("Toggle Button").objectName("Button4").build());
	ToggleButton toggleButton = buttonWithToggleButtonSupport.getToggleButton();
	toggleButton.set(CheckedState.CHECKED); // Example for setting the toggle button's state to checked

	// Change the toggle button state.
	if (toggleButton.isChecked()) {
		toggleButton.set(CheckedState.UNCHECKED);
	} else {
		toggleButton.set(CheckedState.CHECKED);
	}
}

Back to top

See also: