UIA Pro code sample (.NET SDK)

Calculate 2 squared using a calculator application

This example shows how to test the "squared" operation on the Windows calculator, by calculating 2 squared and checking the result:

Copy code
using HP.LFT.SDK;
using HP.LFT.SDK.UIAPro;
using HP.LFT.Verifications;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace FTDTestProject1
{
    [TestClass]
    public class FTDTest : UnitTestClassBase<FTDTest>
    {
        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            GlobalSetup(context);
        }

        [TestInitialize]
        public void TestInitialize()
        {

        }

        [TestMethod]
        public void TestMethodCalculatorUIAPro()
        {
            // Make sure that the Windows calculator application is launched.

            // Describe the two button.
            var twoButton = Desktop.Describe<IWindow>(new WindowDescription
            {
                ProcessName = @"ApplicationFrameHost",
                Name = @"Calculator",
                Path = @"Window",
                FrameworkId = @"Win32",
                ControlType = @"Window"
            })
            .Describe<IUiObject>(new UiObjectDescription
            {
                ProcessName = @"CalculatorApp",
                Path = @"Window;Window;Custom",
                FrameworkId = @"XAML",
                ControlType = @"Custom"
            })
            .Describe<IButton>(new ButtonDescription
            {
                ProcessName = @"CalculatorApp",
                Name = @"Two",
                Path = @"Window;Window;Custom;Group;Group;Button",
                FrameworkId = @"XAML",
                ControlType = @"Button"
            });

            // Describe the square button.
            var squareButton = Desktop.Describe<IWindow>(new WindowDescription
            {
                ProcessName = @"ApplicationFrameHost",
                Name = @"Calculator",
                Path = @"Window",
                FrameworkId = @"Win32",
                ControlType = @"Window"
            })
            .Describe<IUiObject>(new UiObjectDescription
            {
                ProcessName = @"CalculatorApp",
                Path = @"Window;Window;Custom",
                FrameworkId = @"XAML",
                ControlType = @"Custom"
            })
            .Describe<IButton>(new ButtonDescription
            {
                ProcessName = @"CalculatorApp",
                Name = @"Square",
                Path = @"Window;Window;Custom;Group;Group;Button",
                FrameworkId = @"XAML",
                ControlType = @"Button"
            });

            // Describe the result text.
            var resultText = Desktop.Describe<IWindow>(new WindowDescription
            {
                ProcessName = @"ApplicationFrameHost",
                Name = @"Calculator",
                Path = @"Window",
                FrameworkId = @"Win32",
                ControlType = @"Window"
            })
            .Describe<IUiObject>(new UiObjectDescription
            {
                ProcessName = @"CalculatorApp",
                Path = @"Window;Window;Custom",
                FrameworkId = @"XAML",
                ControlType = @"Custom"
            })
            .Describe<IPane>(new PaneDescription
            {
                ProcessName = @"CalculatorApp",
                Path = @"Window;Window;Custom;Group;Text;Pane",
                FrameworkId = @"XAML",
                ControlType = @"Pane"
            });

            // Invoke the default action on the 2 button (clicking using the invoke pattern).
            twoButton.InvokePattern.Invoke();

            // Click the square button.
            squareButton.Click();

            // Get the result value from the text field.
            string result = resultText.LegacyIAccessiblePattern.Name;

            // Verify the expected result (as string) matches with the actual result.
            Verify.AreEqual(4 + "", result, "Verification for the result.");
        }

        [TestCleanup]
        public void TestCleanup()
        {
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            GlobalTearDown();
        }
    }
}