AI-based testing code samples (Java SDK)
This topic shows code samples for using AI-based testing in OpenText Functional Testing for Developers Java tests:
Create a shopping account
This example shows how to create an account on the Advantage Online Shopping web site:
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();
}
Place an order and make sure it is registered
This example shows how to log in to the shopping site, place an order and make sure it is registered:
@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();
}
Configure AI run settings for scrolling and language recognition
This example shows how to customize automatic scrolling and OCR languages for AI identification:
@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();
}
Retrieve values of AI objects
This example shows how to retrieve the value of an input field:
@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:
@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);
}
Enter a text value in a text box
This example shows how to enter text in a text box:
@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");
}
Enable or disable Verify Identification
The examples below demonstrate how to enable or disable the Verify Identification feature and how it affects the recognition of AI-based test objects.
Enable Verify Identification
@Test
public void verifyIdentification_enabled_testPasses() throws GeneralLeanFtException {
// Enable AI Verify Identification.
AiRunSettings.setVerifyIdentificationEnabled(true);
// Launch Chrome and navigate to the test web page.
Browser browser = BrowserFactory.launch(BrowserType.CHROME);
browser.navigate("https://advantageonlineshopping.com");
// Describe the profile AI test object.
AiObject profile = browser.describe(AiObject.class, new AiObjectDescription(com.hp.lft.sdk.ai.AiTypes.PROFILE));
// Perfom a click operation. Because AI Verify Identification is enabled,
// the click is performed only when the profile icon is visible and detected.
profile.click();
// Describe a test object contained in the pop-up window that is
// displayed after clicking the profile icon above. Highlight it
// to check that the clicked was performed successfully.
AiObject profilePopupWindowText = browser.describe(AiObject.class, new AiObjectDescription(com.hp.lft.sdk.ai.AiTypes.TEXT_BLOCK, "SIGN IN WITH FACEBOOK"));
profilePopupWindowText.highlight();
// The isVerifyIdentificationEnabled() method can be used to check if AI Verify Identification is enabled.
Verify.isTrue(AiRunSettings.isVerifyIdentificationEnabled());
}
@Test
public void verifyIdentification_disabled_testFails() throws GeneralLeanFtException {
// Disable AI Verify Identification.
AiRunSettings.setVerifyIdentificationEnabled(false);
// Launch Chrome and navigate to the test web page.
Browser browser = BrowserFactory.launch(BrowserType.CHROME);
browser.navigate("https://advantageonlineshopping.com");
// Describe the profile AI test object.
AiObject profile = browser.describe(AiObject.class, new AiObjectDescription(com.hp.lft.sdk.ai.AiTypes.PROFILE));
// Perfom a click operation. Because AI Verify Identification is disabled,
// the click is performed when the control flow reaches this line, with
// the possibility that the page is not fully loaded yet, so the profile test
// object might not exist yet.
profile.click();
// Describe a test object contained in the pop-up window that is
// displayed after clicking the profile icon above. Highlight it
// to check that the clicked was performed successfully. This
// fails because the click action above was not performed.
AiObject profilePopupWindowText = browser.describe(AiObject.class, new AiObjectDescription(com.hp.lft.sdk.ai.AiTypes.TEXT_BLOCK, "SIGN IN WITH FACEBOOK"));
profilePopupWindowText.highlight();
// The isVerifyIdentificationEnabled() method can be used to check if AI Verify Identification is enabled.
Verify.isFalse(AiRunSettings.isVerifyIdentificationEnabled());
}
Enhance identification text recognition
The examples in this section demonstrate ways you can fine tune the text recognition of an object's identification text to achieve an accurate identification:
Exact text matching for a web object
This example shows how to identify an object in a web application by its text using exact text matching instead of AI matching.
@Test
public void webIdentifyByTextWithTextMatchMethod() throws GeneralLeanFtException {
// Launch a new browser instance and navigate to the relevant web page.
Browser browser = BrowserFactory.launch(BrowserType.CHROME);
browser.navigate("https://advantageonlineshopping.com/#/");
browser.sync();
// Describe the AiObject by passing a TextWithMatchOptions property that includes the exact match method.
AiObject object = browser.describe(AiObject.class, new AiObjectDescription.Builder()
.aiClass(com.hp.lft.sdk.ai.AiTypes.TEXT_BLOCK)
.textWithMatchOptions(new TextWithMatchOptions("dvantageDemo", MatchMethod.IGNORE_CASE))
.build());
// Verify that the object was identified successfully.
Verify.isTrue(object.exists());
}
AI text matching for a mobile object
This example shows how to identify an object in a mobile application by its text using AI text matching.
@Test
public void mobileIdentifyByTextWithTextMatchMethod() throws GeneralLeanFtException {
// Lock device by its name.
Device device = MobileLab.lockDeviceByName("MyDevice");
// Describe the AiObject by passing a TextWithMatchOptions property that includes the AI text match method.
AiObject object = device.describe(AiObject.class, new AiObjectDescription.Builder()
.aiClass(com.hp.lft.sdk.ai.AiTypes.TEXT_BLOCK)
.textWithMatchOptions(new TextWithMatchOptions("ga11ery", MatchMethod.AI))
.build());
// Verify that the object was identified successfully.
Verify.isTrue(object.exists());
}
Use a regular expression to identify text
This example shows how to identify a text object that varies in the application by using a regular expression.
@Test
public void webIdentifyByTextWithRegularExpression() throws GeneralLeanFtException {
// Launch a new browser instance and navigate to the relevant web page.
Browser browser = BrowserFactory.launch(BrowserType.CHROME);
browser.navigate("<amazon shopping cart url>");
browser.sync();
//Describe the AiObject by passing a TextWithMatchOptions property that uses a regular expression to identify the object's text.
// The object we want to identify is the shopping cart's title, that looks like this: Subtotal (5 items): $233.57
AiObject aiObject = browser.describe(AiObject.class, new AiObjectDescription.Builder()
.aiClass(com.hp.lft.sdk.ai.AiTypes.TEXT_BLOCK)
.textWithMatchOptions(new TextWithMatchOptions(new RegExpProperty("Subtotal \(\d item\): \$[\d.]+"), MatchMethod.AI))
.build());
// Verify that the object was identified successfully.
Verify.isTrue(aiObject.exists());
// Print the text value.
System.out.println(aiObject.getValue());
}
See also: