UIA Pro code sample (Java 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
package com.company;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.hp.lft.sdk.*;
import com.hp.lft.sdk.uiapro.*;
import com.hp.lft.verifications.*;

import unittesting.*;

public class FTDTest extends UnitTestClassBase {

    public FTDTest() {
        //Change this constructor to private if you supply your own public constructor
    }

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        instance = new FTDTest();
        globalSetup(FTDTest.class);
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        globalTearDown();
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() throws GeneralLeanFtException {
        // Make sure that the Windows calculator application is launched.

        // Describe the two button.
        Button twoButton = Desktop.describe(Window.class, new WindowDescription.Builder()
                        .processName("ApplicationFrameHost")
                        .name("Calculator")
                        .path("Window")
                        .frameworkId("Win32")
                        .controlType("Window").build())
                .describe(UiObject.class, new UiObjectDescription.Builder()
                        .processName("CalculatorApp")
                        .path("Window;Window;Custom")
                        .frameworkId("XAML")
                        .controlType("Custom").build())
                .describe(Button.class, new ButtonDescription.Builder()
                        .processName("CalculatorApp")
                        .name("Two")
                        .path("Window;Window;Custom;Group;Group;Button")
                        .frameworkId("XAML")
                        .controlType("Button").build());

        // Describe the square button.
        Button squareButton = Desktop.describe(Window.class, new WindowDescription.Builder()
                        .processName("ApplicationFrameHost")
                        .name("Calculator")
                        .path("Window")
                        .frameworkId("Win32")
                        .controlType("Window").build())
                .describe(UiObject.class, new UiObjectDescription.Builder()
                        .processName("CalculatorApp")
                        .path("Window;Window;Custom")
                        .frameworkId("XAML")
                        .controlType("Custom").build())
                .describe(Button.class, new ButtonDescription.Builder()
                        .processName("CalculatorApp")
                        .name("Square")
                        .path("Window;Window;Custom;Group;Group;Button")
                        .frameworkId("XAML")
                        .controlType("Button").build());

        // Describe the result text.
        Pane resultText = Desktop.describe(Window.class, new WindowDescription.Builder()
                        .processName("ApplicationFrameHost")
                        .name("Calculator")
                        .path("Window")
                        .frameworkId("Win32")
                        .controlType("Window").build())
                .describe(UiObject.class, new UiObjectDescription.Builder()
                        .processName("CalculatorApp")
                        .path("Window;Window;Custom")
                        .frameworkId("XAML")
                        .controlType("Custom").build())
                .describe(Pane.class, new PaneDescription.Builder()
                        .processName("CalculatorApp")
                        .path("Window;Window;Custom;Group;Text;Pane")
                        .frameworkId("XAML")
                        .controlType("Pane").build());

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

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

        // Get the result value from the text.
        String result = resultText.getLegacyIAccessiblePattern().getName();

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

}