AI-based testing code samples (JavaScript SDK)

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: "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: "text_block",
                    text: "CREATE NEW ACCOUNT"
                })).click();
                browser.sync();

                // Enter account details in account creation form
                browser.$(AI.AiObject({
                    aiClass: "input",
                    text: "ACCOUNT DETAILS"
                })).sendKeys("TestUsername");
                browser.$(AI.AiObject({
                    aiClass: "input",
                    text: "Email"
                })).sendKeys("test_email@default.com");
                browser.$(AI.AiObject({
                    aiClass: "input",
                    text: "Password"
                })).sendSecureKeys("TestPass!2");
                browser.$(AI.AiObject({
                    aiClass: "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: "check_box",
                    text: "agree ti LN"
                })).setState(true);
                browser.$(AI.AiObject({
                    aiClass: "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: "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: "input",
                    text: "Username",
                    locator: {direction: "fromTop", index: 0}
                })).sendKeys("TestUsername");
                browser.$(AI.AiObject({
                    aiClass: "input",
                    text: "Password"
                })).sendSecureKeys("TestPass!2");
                browser.$(AI.AiObject({
                    aiClass: "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: "plus",
                    locator: {direction: "fromTop", index: 0}
                })).click();
                browser.$(AI.AiObject({
                    aiClass: "button",
                    text: "ADD TO CART"
                })).click();

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

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

                // Enter details
                browser.$(AI.AiObject({
                    aiClass: "input",
                    text: "SafePay username"
                })).sendKeys("SafePayUser");
                browser.$(AI.AiObject({
                    aiClass: "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: "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: "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: "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: "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);
    });
});

See also: