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(AiTypes.PROFILE)).click();
        Thread.sleep(10000); // necessary if the website UI loads slowly
        browser.describe(AiObject.class, new AiObjectDescription(AiTypes.TEXT_BLOCK, "CREATE NEW ACCOUNT")).click();
        browser.sync();

        // Enter account details in account creation form
        browser.describe(AiObject.class, new AiObjectDescription(AiTypes.INPUT, "ACCOUNT DETAILS")).sendKeys("TestUsername");
        browser.describe(AiObject.class, new AiObjectDescription(AiTypes.INPUT, "Email")).sendKeys("test_email@default.com");
        browser.describe(AiObject.class, new AiObjectDescription(AiTypes.INPUT, "Password")).sendSecureKeys("TestPass!2");
        browser.describe(AiObject.class, new AiObjectDescription(AiTypes.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(AiTypes.CHECK_BOX, "agree ti LN")).setState(com.hp.lft.sdk.ai.ToggleState.ON);
        browser.describe(AiObject.class, new AiObjectDescription(AiTypes.BUTTON, "REGISTER")).click();
        browser.sync();

        // Search for an item after logging in to the account you created
        browser.describe(AiObject.class, new AiObjectDescription(AiTypes.SEARCH)).search("phone");
        browser.sync();
    }  

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(AiTypes.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(AiTypes.INPUT)
                .text("Username")
                .locator(new Position(Direction.FROM_TOP, 0)).build()).sendKeys("TestUsername");
        browser.describe(AiObject.class, new AiObjectDescription(AiTypes.INPUT, "Password")).sendSecureKeys("TestPass!2");
        browser.describe(AiObject.class, new AiObjectDescription(AiTypes.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(AiTypes.BUTTON, "ADD TO CART")).click();

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

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

        // Enter details
        browser.describe(AiObject.class, new AiObjectDescription(AiTypes.INPUT, "SafePay username")).sendKeys("SafePayUser");
        browser.describe(AiObject.class, new AiObjectDescription(AiTypes.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(AiTypes.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(AiTypes.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(AiTypes.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(AiTypes.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

Retrieve values of AI objects

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

Copy code
@Test

    public void test1() throws GeneralLeanFtException {

        Browser browser = BrowserFactory.launch(BrowserType.CHROME);

        browser.navigate("https://www.advantageonlineshopping.com/#/register");
         AiObjectDescription description = new AiObjectDescription();

        description.setAiClass(AiTypes.INPUT);

        description.setLocator(new Position(Direction.FROM_RIGHT, 0));



        AiObject object = browser.describe(AiObject.class, description);

        object.sendKeys("Hello world!");

        String value = object.getValue();

        System.out.println("Value: " + value);

    }

This example shows how to retrieve the value of a button:

Copy code
 @Test

    public void test2() throws GeneralLeanFtException {

        Browser browser = BrowserFactory.launch(BrowserType.CHROME);

        browser.navigate("https://material.angular.io/components/slide-toggle/overview");



        AiObjectDescription description = new AiObjectDescription();

        description.setAiClass(AiTypes.TOGGLE_BUTTON);

        description.setLocator(new Position(Direction.FROM_RIGHT, 0));



        AiObject object = browser.describe(AiObject.class, description);

        object.click();

        String value = object.getValue();

        System.out.println("Value1: " + value);

    }

Back to top

Enter a text value in a text box

This example shows how to enter text in a text box:

Copy code
 @Test

     public void testAI() throws GeneralLeanFtException {

        Browser browser = BrowserFactory.launch(BrowserType.CHROME);

        browser.navigate("https://advantageonlineshopping.com/#/product/16");

        browser.sync();



        AiObject aiObject =    browser.describe(AiObject.class, new AiObjectDescription(com.hp.lft.sdk.ai.AiTypes.SEARCH));

        aiObject.highlight();

        aiObject.click();

        browser.describe(AiObject.class, new AiObjectDescription(com.hp.lft.sdk.ai.AiTypes.INPUT, "2)")).setText("text");

    }

Back to top

See also: