AI-based testing code samples (Java SDK)

Create a shopping account

This example shows how to create an account on the Advantage Online Shopping web site:

Copy code
import com.hp.lft.report.ReportException;
import com.hp.lft.sdk.ai.Position;
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.web.*;
import com.hp.lft.sdk.ai.*;
import com.hp.lft.verifications.*;

import unittesting.*;

.
.
.


    @Test
    public void testCreateAccount() throws GeneralLeanFtException, InterruptedException {
        // Open browser, navigate to website and wait for it to finish loading
        Browser browser = BrowserFactory.launch(BrowserType.CHROME);
        browser.navigate("https://advantageonlineshopping.com/");
        browser.sync();
        Thread.sleep(10000); // necessary if the website UI loads slowly

        // Open sign in form and navigate to account creation form
        browser.describe(AiObject.class, new AiObjectDescription("profile")).click();
        Thread.sleep(10000); // necessary if the website UI loads slowly
        browser.describe(AiObject.class, new AiObjectDescription("text_block", "CREATE NEW ACCOUNT")).click();
        browser.sync();

        // Enter account details in account creation form
        browser.describe(AiObject.class, new AiObjectDescription("input", "ACCOUNT DETAILS")).sendKeys("TestUsername");
        browser.describe(AiObject.class, new AiObjectDescription("input", "Email")).sendKeys("test_email@default.com");
        browser.describe(AiObject.class, new AiObjectDescription("input", "Password")).sendSecureKeys("TestPass!2");
        browser.describe(AiObject.class, new AiObjectDescription("input", "Confirm password")).sendSecureKeys("TestPass!2");

        // Enable scrolling down so AI can find remaining test objects that are not currently visible
        AiRunSettings.updateAutoScrollSettings((new AiAutoScrollSettings()).enable(ScrollDirection.DOWN, 10));

        // Accept terms and finish creating account
        browser.describe(AiObject.class, new AiObjectDescription("check_box", "agree ti LN")).setState(com.hp.lft.sdk.ai.ToggleState.ON);
        browser.describe(AiObject.class, new AiObjectDescription("button", "REGISTER")).click();

        browser.close();
    }  

Back to top

Place an order and make sure it is registered

This examples shows how to log in to the shopping site, place an order and make sure it is registered:

Copy code
@Test
    public void testSignInAndOrder() throws GeneralLeanFtException, ReportException, InterruptedException {
        // Open browser, navigate to website and wait for it to finish loading
        Browser browser = BrowserFactory.launch(BrowserType.CHROME);
        browser.navigate("https://advantageonlineshopping.com/");
        browser.sync();
        Thread.sleep(10000); // the amount of time can be changed

        // Open sign in form and wait for it to load
        browser.describe(AiObject.class, new AiObjectDescription("profile")).click();
        Thread.sleep(10000); // the amount of time can be changed

        // Enter credentials and sign in
        browser.describe(AiObject.class, new AiObjectDescription.Builder()
                .aiClass("input")
                .text("Username")
                .locator(new Position(Direction.FROM_TOP, 0)).build()).sendKeys("TestUsername");
        browser.describe(AiObject.class, new AiObjectDescription("input", "Password")).sendSecureKeys("TestPass!2");
        browser.describe(AiObject.class, new AiObjectDescription("button", "SIGN IN")).click();

        // Navigate to product page
        browser.navigate("https://advantageonlineshopping.com/#/product/20");
        browser.sync();

        // Add item to cart
        browser.describe(AiObject.class, new AiObjectDescription.Builder()
                .aiClass("plus")
                .locator(new Position(Direction.FROM_TOP, 0)).build()).click();
        browser.describe(AiObject.class, new AiObjectDescription("button", "ADD TO CART")).click();

        // Navigate to shopping cart
        browser.describe(AiObject.class, new AiObjectDescription("shopping_cart")).click();
        browser.sync();

        // Initiate checkout
        browser.describe(AiObject.class, new AiObjectDescription.Builder()
                .aiClass("button")
                .text("CHECKOUT ($539.98)")
                .locator(new Position(Direction.FROM_BOTTOM, 0)).build()).click();
        browser.describe(AiObject.class, new AiObjectDescription("button", "NEXT")).click();
        browser.sync();

        // Enter details
        browser.describe(AiObject.class, new AiObjectDescription("input", "SafePay username")).sendKeys("SafePayUser");
        browser.describe(AiObject.class, new AiObjectDescription("input", "SafePay password")).sendSecureKeys("SafePayPass2");

        // Enable scrolling down so AI can find remaining test objects that are not currently visible
        AiRunSettings.updateAutoScrollSettings((new AiAutoScrollSettings()).enable(ScrollDirection.DOWN, 10));

        // Finish order
        browser.describe(AiObject.class, new AiObjectDescription("button", "PAY NOW")).click();
        browser.sync();

        // Verify order registered
        com.hp.lft.report.Reporter.startReportingContext("Verify property: exists", com.hp.lft.report.ReportContextInfo.verificationMode());
        Verify.isTrue(browser.describe(AiObject.class, new AiObjectDescription("text_block", "Thank you for buying with Advantage")).exists(), "Verification", "Verify property: exists");
        com.hp.lft.report.Reporter.endReportingContext();

        browser.close();
    }

Back to top

Configure AI run settings for scrolling and language recognition

This example shows how to customize automatic scrolling and OCR languages for AI identification:

Copy code
 @Test
    public void testAIRunSettings() throws GeneralLeanFtException, ReportException {
        // Open browser and navigate to website
        Browser browser = BrowserFactory.launch(BrowserType.CHROME);
        browser.navigate("https://en.wikipedia.org/wiki/Main_Page");
        browser.sync();

        // Jump to link in lower part of page
        Link completeListLink = browser.describe(Link.class, new LinkDescription.Builder()
                .tagName("A")
                .innerText("Complete list").build());
        completeListLink.highlight();

        // Configure autoscroll settings to enable scrolling up
        AiRunSettings.updateAutoScrollSettings((new AiAutoScrollSettings()).enable(ScrollDirection.UP, 10));

        // Configure OCR settings to enable detection for English, German, French, Hebrew and Traditional Chinese
        AiRunSettings.updateOCRSettings((new AiOCRSettings()).setLanguages(new String[]{"en", "de", "fr", "he", "zht"}));

        // Navigate to French wikipedia section
        browser.describe(AiObject.class, new AiObjectDescription("text_block", "Français")).click();
        browser.sync();

        // Jump to text in lower part of page
        WebElement webElement = browser.describe(WebElement.class, new WebElementDescription.Builder()
                .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 :").build());
        webElement.highlight();

        // Verify text block containing French text exists
        com.hp.lft.report.Reporter.startReportingContext("Verify property: exists", com.hp.lft.report.ReportContextInfo.verificationMode());
        Verify.isTrue(browser.describe(AiObject.class, new AiObjectDescription("text_block", "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(), "Verification", "Verify property: exists");
        com.hp.lft.report.Reporter.endReportingContext();

        browser.close();
    }

Back to top

See also: