UIA Pro code sample (JavaScript 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
var LFT = require("leanft");
var Desktop = LFT.Desktop;
var UIAPro = LFT.UIAPro;
var expect = require("leanft/expect");
var whenDone = LFT.whenDone;


describe("UIAPro test example", function () {
    // set the default Jasmine time out (if not using Jasmine: remove this line)
    // jasmine: jasmine.DEFAULT_TIMEOUT_INTERVAL = 30 * 1000;

    // set the default Mocha time out (if not using Mocha: remove this line)
    mocha: this.timeout(30 * 1000);

    // For Jasmine, use beforeAll(); for Mocha, use before()
    before(function (done) {
        LFT.init();
        whenDone(done);
    });

    beforeEach(function (done) {
        LFT.beforeTest();
        whenDone(done);
    });

    it("Should describe the purpose of your test case", function (done) {
        // Make sure that the Windows calculator application is launched.

        // Describe the 2 button.
        var twoButton = Desktop.$(UIAPro.Window({
            processName: "ApplicationFrameHost",
            name: "Calculator",
            path: "Window",
            supportedPatterns: ["LegacyIAccessible", "Transform", "Window"],
            frameworkId: "Win32",
            controlType: "Window",
            automationId: ""
        }))
            .$(UIAPro.UiObject({
                processName: "CalculatorApp",
                name: "",
                path: "Window;Window;Custom",
                supportedPatterns: ["LegacyIAccessible", "ScrollItem", "Selection"],
                frameworkId: "XAML",
                controlType: "Custom",
                automationId: "NavView"
            }))
            .$(UIAPro.Group({
                processName: "CalculatorApp",
                name: "",
                path: "Window;Window;Custom;Group",
                supportedPatterns: ["LegacyIAccessible", "ScrollItem"],
                frameworkId: "XAML",
                controlType: "Group",
                automationId: ""
            }))
            .$(UIAPro.Group({
                processName: "CalculatorApp",
                name: "Number pad",
                path: "Window;Window;Custom;Group;Group",
                supportedPatterns: ["LegacyIAccessible", "ScrollItem"],
                frameworkId: "XAML",
                controlType: "Group",
                automationId: "NumberPad"
            }))
            .$(UIAPro.Button({
                processName: "CalculatorApp",
                name: "Two",
                path: "Window;Window;Custom;Group;Group;Button",
                supportedPatterns: ["Invoke", "LegacyIAccessible", "ScrollItem"],
                frameworkId: "XAML",
                controlType: "Button",
                automationId: "num2Button"
            }));

        // Describe the square button.
        var squareButton = Desktop.$(UIAPro.Window({
            processName: "ApplicationFrameHost",
            name: "Calculator",
            path: "Window",
            supportedPatterns: ["LegacyIAccessible", "Transform", "Window"],
            frameworkId: "Win32",
            controlType: "Window",
            automationId: ""
        }))
            .$(UIAPro.UiObject({
                processName: "CalculatorApp",
                name: "",
                path: "Window;Window;Custom",
                supportedPatterns: ["LegacyIAccessible", "ScrollItem", "Selection"],
                frameworkId: "XAML",
                controlType: "Custom",
                automationId: "NavView"
            }))
            .$(UIAPro.Group({
                processName: "CalculatorApp",
                name: "",
                path: "Window;Window;Custom;Group",
                supportedPatterns: ["LegacyIAccessible", "ScrollItem"],
                frameworkId: "XAML",
                controlType: "Group",
                automationId: ""
            }))
            .$(UIAPro.Group({
                processName: "CalculatorApp",
                name: "Standard functions",
                path: "Window;Window;Custom;Group;Group",
                supportedPatterns: ["LegacyIAccessible", "ScrollItem"],
                frameworkId: "XAML",
                controlType: "Group",
                automationId: "StandardFunctions"
            }))
            .$(UIAPro.Button({
                processName: "CalculatorApp",
                name: "Square",
                path: "Window;Window;Custom;Group;Group;Button",
                supportedPatterns: ["Invoke", "LegacyIAccessible", "ScrollItem"],
                frameworkId: "XAML",
                controlType: "Button",
                automationId: "xpower2Button"
            }));

        // Describe the result text field.
        var resultTextField = Desktop.$(UIAPro.Window({
            processName: "ApplicationFrameHost",
            name: "Calculator",
            path: "Window",
            supportedPatterns: ["LegacyIAccessible", "Transform", "Window"],
            frameworkId: "Win32",
            controlType: "Window",
            automationId: ""
        }))
            .$(UIAPro.UiObject({
                processName: "CalculatorApp",
                name: "",
                path: "Window;Window;Custom",
                supportedPatterns: ["LegacyIAccessible", "ScrollItem", "Selection"],
                frameworkId: "XAML",
                controlType: "Custom",
                automationId: "NavView"
            }))
            .$(UIAPro.Group({
                processName: "CalculatorApp",
                name: "",
                path: "Window;Window;Custom;Group",
                supportedPatterns: ["LegacyIAccessible", "ScrollItem"],
                frameworkId: "XAML",
                controlType: "Group",
                automationId: ""
            }))
            .$(UIAPro.Text({
                processName: "CalculatorApp",
                path: "Window;Window;Custom;Group;Text",
                supportedPatterns: ["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().then(invokeResult => {
            // Click the square button.
            squareButton.click().then(clickResult => {
                // Get the result value from the text field.
                resultTextField.legacyAccessiblePattern().getValue().then(resultValue => {
                    // Verify the expected result (as string) matches with the actual result.
                    expect(resultValue).toEqual("4");
                });
            });
        });

        whenDone(done);
    });

    afterEach(function (done) {
        LFT.afterTest();
        whenDone(done);
    });

    // For Jasmine, use afterAll(); for Mocha, use after()
    after(function (done) {
        LFT.cleanup();
        whenDone(done);
    });
});

Back to top

Retrieve the values of AI objects

Supported in UFT Developer 2021 R1 and later

This example shows how to retrieve the value of an input field:

Copy code
var LFT = require("leanft");
var AI = LFT.AI;
var Web = LFT.Web;
var Reporter = LFT.Reporter;
var expect = require("leanft/expect");



jasmine.DEFAULT_TIMEOUT_INTERVAL = 200000;



var browserName = "Chrome";
var browser;
var aiInput;


describe("AI Tests", function () {

    beforeAll(function (done) {

        LFT.init();

        LFT.whenDone(done);

    });


    beforeEach(function (done) {

        LFT.beforeTest();
        Reporter.setSnapshotCaptureLevel(Reporter.CaptureLevel.All);
        Web.Browser.launch(Web.BrowserType[browserName]).then(function (launched_browser) {
            browser = launched_browser;
            browser.navigate("https://www.advantageonlineshopping.com/#/register");
            //browser.navigate("https://www.advantageonlineshopping.com/#/category/Speakers/4");
            browser.sync();

            aiInput = browser.$(AI.AiObject({
                aiClass: AI.AiTypes.input,
                locator: {
                    direction: "fromRight",
                    index: 0
                }

            }));

        });

        LFT.whenDone(done);

    });



    fit("INPUT getValue", function (done) {

        //aiInput.click();
        aiInput.sendKeys("Hello world!").then(function () {
            aiInput.getValue().then(function (text) {
                console.log("text: " + text);
                expect(text).toEqual("Hello world!");
            });

        });
        
        LFT.whenDone(done);

    });


    afterEach(function (done) {

        LFT.afterTest();
        if (browser) {
            browser.exists(2).then(function (result) {
                if (result)
                    browser.closeAllTabs();
            });

        }

        LFT.whenDone(done);

    });


    afterAll(function (done) {

        LFT.cleanup();
        LFT.whenDone(done);

    });

});

Back to top

See also: