UIA Pro code sample (.NET SDK)
Supported in UFT Developer version 2023 and later
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:
using HP.LFT.SDK;
using HP.LFT.SDK.UIAPro;
using HP.LFT.Verifications;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace UftDeveloperTestProject1
{
[TestClass]
public class UftDeveloperTest : UnitTestClassBase<UftDeveloperTest>
{
[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 2 button.
var twoButton = Desktop.Describe<IWindow>(new WindowDescription
{
ProcessName = @"ApplicationFrameHost",
Name = @"Calculator",
Path = @"Window",
SupportedPatterns = new string[] { @"LegacyIAccessible", @"Transform", @"Window" },
FrameworkId = @"Win32",
ControlType = @"Window",
AutomationId = string.Empty
})
.Describe<IUiObject>(new UiObjectDescription
{
ProcessName = @"CalculatorApp",
Name = string.Empty,
Path = @"Window;Window;Custom",
SupportedPatterns = new string[] { @"LegacyIAccessible", @"ScrollItem", @"Selection" },
FrameworkId = @"XAML",
ControlType = @"Custom",
AutomationId = @"NavView"
})
.Describe<IGroup>(new GroupDescription
{
ProcessName = @"CalculatorApp",
Name = string.Empty,
Path = @"Window;Window;Custom;Group",
SupportedPatterns = new string[] { @"LegacyIAccessible", @"ScrollItem" },
FrameworkId = @"XAML",
ControlType = @"Group",
AutomationId = string.Empty
})
.Describe<IGroup>(new GroupDescription
{
ProcessName = @"CalculatorApp",
Name = @"Number pad",
Path = @"Window;Window;Custom;Group;Group",
SupportedPatterns = new string[] { @"LegacyIAccessible", @"ScrollItem" },
FrameworkId = @"XAML",
ControlType = @"Group",
AutomationId = @"NumberPad"
})
.Describe<IButton>(new ButtonDescription
{
ProcessName = @"CalculatorApp",
Name = @"Two",
Path = @"Window;Window;Custom;Group;Group;Button",
SupportedPatterns = new string[] { @"Invoke", @"LegacyIAccessible", @"ScrollItem" },
FrameworkId = @"XAML",
ControlType = @"Button",
AutomationId = @"num2Button"
});
// Describe the square button.
var squareButton = Desktop.Describe<IWindow>(new WindowDescription
{
ProcessName = @"ApplicationFrameHost",
Name = @"Calculator",
Path = @"Window",
SupportedPatterns = new string[] { @"LegacyIAccessible", @"Transform", @"Window" },
FrameworkId = @"Win32",
ControlType = @"Window",
AutomationId = string.Empty
})
.Describe<IUiObject>(new UiObjectDescription
{
ProcessName = @"CalculatorApp",
Name = string.Empty,
Path = @"Window;Window;Custom",
SupportedPatterns = new string[] { @"LegacyIAccessible", @"ScrollItem", @"Selection" },
FrameworkId = @"XAML",
ControlType = @"Custom",
AutomationId = @"NavView"
})
.Describe<IGroup>(new GroupDescription
{
ProcessName = @"CalculatorApp",
Name = string.Empty,
Path = @"Window;Window;Custom;Group",
SupportedPatterns = new string[] { @"LegacyIAccessible", @"ScrollItem" },
FrameworkId = @"XAML",
ControlType = @"Group",
AutomationId = string.Empty
})
.Describe<IGroup>(new GroupDescription
{
ProcessName = @"CalculatorApp",
Name = @"Standard functions",
Path = @"Window;Window;Custom;Group;Group",
SupportedPatterns = new string[] { @"LegacyIAccessible", @"ScrollItem" },
FrameworkId = @"XAML",
ControlType = @"Group",
AutomationId = @"StandardFunctions"
})
.Describe<IButton>(new ButtonDescription
{
ProcessName = @"CalculatorApp",
Name = @"Square",
Path = @"Window;Window;Custom;Group;Group;Button",
SupportedPatterns = new string[] { @"Invoke", @"LegacyIAccessible", @"ScrollItem" },
FrameworkId = @"XAML",
ControlType = @"Button",
AutomationId = @"xpower2Button"
});
// Describe the result text field.
var resultTextField = Desktop.Describe<IWindow>(new WindowDescription
{
ProcessName = @"ApplicationFrameHost",
Name = @"Calculator",
Path = @"Window",
SupportedPatterns = new string[] { @"LegacyIAccessible", @"Transform", @"Window" },
FrameworkId = @"Win32",
ControlType = @"Window",
AutomationId = string.Empty
})
.Describe<IUiObject>(new UiObjectDescription
{
ProcessName = @"CalculatorApp",
Name = string.Empty,
Path = @"Window;Window;Custom",
SupportedPatterns = new string[] { @"LegacyIAccessible", @"ScrollItem", @"Selection" },
FrameworkId = @"XAML",
ControlType = @"Custom",
AutomationId = @"NavView"
})
.Describe<IGroup>(new GroupDescription
{
ProcessName = @"CalculatorApp",
Name = string.Empty,
Path = @"Window;Window;Custom;Group",
SupportedPatterns = new string[] { @"LegacyIAccessible", @"ScrollItem" },
FrameworkId = @"XAML",
ControlType = @"Group",
AutomationId = string.Empty
})
.Describe<IText>(new TextDescription
{
ProcessName = @"CalculatorApp",
Path = @"Window;Window;Custom;Group;Text",
SupportedPatterns = new string[] { @"Invoke", @"LegacyIAccessible", @"ScrollItem" },
FrameworkId = @"XAML",
ControlType = @"Text",
AutomationId = @"CalculatorResults"
});
// 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 = resultTextField.LegacyIAccessiblePattern.Value;
// 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();
}
}
}
See also: