AI-based testing code samples (JavaScript SDK)

This topic shows code samples for using AI-based testing in OpenText Functional Testing for Developers JavaScript tests:

Create a shopping account and place an order

This example shows how to create an account, log in, place an order, and make sure it is registered:

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

describe("Test", function() {
    jasmine : jasmine.DEFAULT_TIMEOUT_INTERVAL = 100 * 1000;
    //mocha: this.timeout(30 * 1000);

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

    it("Create account", function(done) {
        // Open web browser
        Web.Browser.launch(Web.BrowserType.Chrome).then(function (browser) {
            
            // Navigate to website
            browser.navigate("https://advantageonlineshopping.com/");
            browser.sync();
            
            // Open sign in form
            browser.$(AI.AiObject({
                aiClass: AI.AiTypes.profile
            })).click();
            
            // Wait for sign in form to load (necessary if the website UI loads slowly)
            setTimeout(function () {
                
                // Navigate to account creation form
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.text_block,
                    text: "CREATE NEW ACCOUNT"
                })).click();
                browser.sync();

                // Enter account details in account creation form
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.input,
                    text: "ACCOUNT DETAILS"
                })).sendKeys("TestUsername");
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.input,
                    text: "Email"
                })).sendKeys("test_email@default.com");
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.input,
                    text: "Password"
                })).sendSecureKeys("TestPass!2");
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.input,
                    text: "Confirm password"
                })).sendSecureKeys("TestPass!2");
                
                // Enable scrolling down so AI can find remaining test objects that are not currently visible
                AI.RunSettings.autoScroll.enable({"direction": "down", "maxScrolls": 10});
                
                // Accept terms and finish creating account
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.check_box,
                    text: "agree ti LN"
                })).setState(true);
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.button,
                    text: "REGISTER"
                })).click();
                
                browser.close();
                
                whenDone(done);
            }, 10000);
        });
    });
    
    it("Sign in and order", function(done) {
        // Open web browser
        Web.Browser.launch(Web.BrowserType.Chrome).then(function (browser) {
            
            // Navigate to website
            browser.navigate("https://advantageonlineshopping.com/");
            browser.sync();
            
            // Open sign in form
            browser.$(AI.AiObject({
                aiClass: AI.AiTypes.profile
            })).click();
            
            // Wait for sign in form to load (necessary if the website UI loads slowly)
            setTimeout(function () {
                
                // Enter credentials and sign in
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.input,
                    text: "Username",
                    locator: {direction: "fromTop", index: 0}
                })).sendKeys("TestUsername");
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.input,
                    text: "Password"
                })).sendSecureKeys("TestPass!2");
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.button,
                    text: "SIGN IN"
                })).click();
                browser.sync();
                
                // Navigate to product page
                browser.navigate("https://advantageonlineshopping.com/#/product/20");
                browser.sync();
                
                // Add item to cart
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.plus,
                    locator: {direction: "fromTop", index: 0}
                })).click();
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.button,
                    text: "ADD TO CART"
                })).click();

                // Navigate to shopping cart
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.shopping_cart
                })).click();
                browser.sync();

                // Initiate checkout
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.button,
                    text: "CHECKOUT (S1,079.96)",
                    locator: {direction: "fromBottom", index: 0}
                })).click();
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.button,
                    text: "NEXT"
                })).click();

                // Enter details
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.input,
                    text: "SafePay username"
                })).sendKeys("SafePayUser");
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.input,
                    text: "SafePay password"
                })).sendSecureKeys("SafePayPass2");

                // Enable scrolling down so AI can find remaining test objects that are not currently visible
                AI.RunSettings.autoScroll.enable({"direction": "down", "maxScrolls": 10});
                
                // Finish order
                browser.$(AI.AiObject({
                    aiClass: AI.AiTypes.button,
                    text: "PAY NOW"
                })).click();
                browser.sync();

                // Verify order registered
                LFT.Reporter.startReportingContextWithInfo("Verify property: exists", {contextMode: LFT.Reporter.ReportContextMode.Verification});
                verify(browser.$(AI.AiObject({
                        aiClass: AI.AiTypes.text_block,
                        text: "Thank you for buying with Advantage"
                    })).exists()).toBeTruthy();
                LFT.Reporter.endReportingContext();
                
                browser.close();
                
                whenDone(done);
            }, 10000);
        });
    });
    
    it("AI Run Settings", function(done) {
        // Open web browser
        Web.Browser.launch(Web.BrowserType.Chrome).then(function (browser) {
            
            // Navigate to website
            browser.navigate("https://en.wikipedia.org/wiki/Main_Page");
            browser.sync();
            
            // Jump to link in lower part of page
            var completeListLink = browser.$(Web.Link({
                tagName: "A",
                innerText: "Complete list"
            }));
            completeListLink.highlight();
            
            // Configure autoscroll settings to enable scrolling up
            AI.RunSettings.autoScroll.enable({"direction": "up", "maxScrolls": 10});

            // Configure OCR settings to enable detection for English, German, French, Hebrew and Traditional Chinese
            AI.RunSettings.OCR.set({"languages": ["en,de,fr,he,zht"]});
            
            // Navigate to French Wikipedia section
            browser.$(AI.AiObject({
                aiClass: AI.AiTypes.text_block,
                text: "Français"
            })).click();
            browser.sync();
            
            // Jump to text in lower part of page
            var webElement = browser.$(Web.Element({
                tagName: "DIV",
                innerText: "Wikipédia est hébergée par la Wikimedia Foundation, de même que les projets suivants, coordonnés sur le site Méta-Wiki :"
            }));
            webElement.highlight();

            // Verify text block containing French text exists
            LFT.Reporter.startReportingContextWithInfo("Verify property: exists", {contextMode: LFT.Reporter.ReportContextMode.Verification});
            verify(browser.$(AI.AiObject({
                aiClass: AI.AiTypes.text_block,
                text: "Wikipédia est hébergée par la Wikimedia Foundation, de même que les projets suivants, coordonnés sur le site Méta-Wiki :"
            })).exists()).toBeTruthy();
            LFT.Reporter.endReportingContext();
            
            browser.close();
        });

        whenDone(done);
    });

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

    afterAll(function(done) {
        LFT.cleanup();
        whenDone(done);
    });
});

Back to top

Retrieve the values of AI objects

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

Register a custom class

This example shows a code snippet that registers a custom class:

Copy code
AI.AiUtil.customClass.register("<custom class name>", "<path to image.png>").then(() =>{

        device.$(AI.AiObject({

          aiClass: "<custom class name>"

        })).click();

To unregister the custom class: AI.AiUtil.customClass.unregister("<custom class name>");

To unregister all custom classes: AI.AiUtil.customClass.unregisterAll();

Back to top

See also: