Skip to main content
All docs
V24.2

How To Test Controls With Appium

  • 3 minutes to read

DevExpress WPF Controls support the Appium framework with WinAppDriver UI test automation. This topic shows how to prepare the environment and create a test.

Prepare the Environment

Follow the steps below to use Appium with DevExpress WPF Controls:

  1. Enable Windows Developer Mode.
  2. Install WinAppDriver.
  3. Reference the Appium.WebDriver nuget package in your project.
  4. Switch the DevExpress WPF controls to UI Testing mode. To do this, set the DX.UITESTINGENABLED environment variable to 1 or the ClearAutomationEventsHelper.UITestingEnabled property to true in the tested application on the application startup. This mode results in the following changes:

    • Animations are disabled.
    • Context menus are opened only on mouse click and do not open when the mouse pointer is above a menu.
    • The UI Automation tree is adjusted to make UI tests more stable and reliable.

Create a Test

To use the Appium API, create a WindowsDriver instance. The following code sample runs the tested application and creates a WindowsDriver session:

var options = new AppiumOptions();
options.AddAdditionalCapability(capabilityName: "app", capabilityValue: PathToTheApp);
options.AddAdditionalCapability(capabilityName: "deviceName", capabilityValue: "WindowsPC");
options.AddAdditionalCapability(capabilityName: "platformName", capabilityValue: "Windows");
options.AddAdditionalCapability(capabilityName: "ms:experimental-webdriver", capabilityValue: true);
var driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), options);

To test a running application, change the line with the “app” capabilityName from the code sample above to the following:

options.AddAdditionalCapability(capabilityName: "appTopLevelWindow", capabilityValue: $"0x{WindowHandle.ToInt64():X8}");

Note

If you host tests in the same process as the tested application, tests should be in a separate thread. In this case, the UI thread can process windows messages synchronously.

Use WinAppDriver UI Recorder

You can use the WinAppDriver UI Recorder tool to generate tests. In this case, your test application requires the DesktopSession class. This approach has the following cons:

  • These tests use the FindElementByXPath method to find elements. This method is slow because it parses the entire visual tree.
  • These tests are difficult to maintain since they use absolute XPaths to find elements. Application layout changes may break tests.
  • These tests are difficult to read. Refer to the commit on our GitHub repository to compare the recorded test and the Appium API-based test readability.

Use Appium API

Use the WinAppDriver’s FindElementByName, FindElementByClassName, and FindElementByAccessibilityId methods to find application elements. These methods work faster than the FindElementByXPath method. Tests based on these methods do not fail when you modify the application layout.

You can use the Inspect tool to find element names, class names, and automation ids.

Tip

You can specify the AutomationProperties.AutomationId attached property of the application elements to enhance test readability. Refer to the Use the AutomationID Property topic for more information.

Example

The following code opens the “New Employee” window, finds the “First Name” element with the TextEdit class in this window, inputs “John”, and clicks the “Save & Close” element:

[Test]
public void CreateEmployee()
{        
    var bNewEmployee = driver.FindElementByName("New Employee");
    bNewEmployee.Click();

    WindowsElement newEmployeeWindow = null;
    while (newEmployeeWindow == null)
        newEmployeeWindow = driver.FindElementByName("Employee (New)");

    newEmployeeWindow.FindElementByName("First Name").FindElementByClassName("TextEdit").SendKeys("John");
    newEmployeeWindow.FindElementByName("Save & Close").Click();
}
See Also