AI-based testing code samples (.NET SDK)

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

Enable or disable AI 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

Copy code
[TestMethod]
        public void VerifyIdentification_Enabled_TestPasses()
        {
            // Enable AI Verify Identification.
            AIRunSettings.VerifyIdentificationEnabled = true;

            // Launch Chrome and navigate to the test web page.
            IBrowser browser = BrowserFactory.Launch(BrowserType.Chrome);
            browser.Navigate("https://advantageonlineshopping.com");

            // Describe the profile AI test object.
            IAIObject profile = browser.Describe<IAIObject>(new AIObjectDescription
            {
                AIClass = 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.
            IAIObject profilePopupWindowText = browser.Describe<IAIObject>(new AIObjectDescription
            {
                AIClass = AITypes.TEXT_BLOCK,
                Text = "SIGN IN WITH FACEBOOK"
            });
            profilePopupWindowText.Highlight();

            // The VerifyIdentificationEnabled property can be used to check if AI Verify Identification is enabled.
            Verify.IsTrue(AIRunSettings.VerifyIdentificationEnabled);
        }

Disable Verify Identification

Copy code
[TestMethod]
        public void VerifyIdentification_Disabled_TestFails()
        {
            // Disable AI Verify Identification.
            AIRunSettings.VerifyIdentificationEnabled = false;

            // Launch Chrome and navigate to the test web page.
            IBrowser browser = BrowserFactory.Launch(BrowserType.Chrome);
            browser.Navigate("https://advantageonlineshopping.com");

            // Describe the profile AI test object.
            IAIObject profile = browser.Describe<IAIObject>(new AIObjectDescription
            {
                AIClass = 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.
            IAIObject profilePopupWindowText = browser.Describe<IAIObject>(new AIObjectDescription
            {
                AIClass = AITypes.TEXT_BLOCK,
                Text = "SIGN IN WITH FACEBOOK"
            });
            profilePopupWindowText.Highlight();

            // The VerifyIdentificationEnabled property can be used to check if AI Verify Identification is enabled.
            Verify.IsFalse(AIRunSettings.VerifyIdentificationEnabled);
        }

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.

Copy code
[TestMethod]
public void WebIdentifyByTextWithTextMatchMethod()
{
    // Launch a new browser instance and navigate to the relevant web page.
    IBrowser 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.
    var obj = browser.Describe<IAIObject>(new AIObjectDescription
    {
        AIClass = AITypes.TEXT_BLOCK, 
        TextWithMatchOptions = new TextWithMatchOptions("dvantageDemo", MatchMethod.IGNORE_CASE)
    });

    // Verify that the object was identified successfully.
    Verify.IsTrue(obj.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.

Copy code
[TestMethod]
public void MobileIdentifyByTextWithTextMatchMethod()
{
    // Lock device by its name.
    IDevice device = MobileLab.LockDeviceByName("MyDevice");

    // Describe the AiObject by passing a TextWithMatchOptions property that includes the AI text match method.
    var obj = device.Describe<IAIObject>(new AIObjectDescription
    {
        AIClass = AITypes.TEXT_BLOCK, 
        TextWithMatchOptions = new TextWithMatchOptions("Gallery", MatchMethod.AI)
    });

    // Verify that the object was identified successfully.
    Verify.IsTrue(obj.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.

Copy code
[TestMethod]
public void WebIdentifyByTextWithRegulareExpression()
{
    // Launch a new browser instance and navigate to the relevant web page.
    IBrowser 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
    var aiObject = browser.Describe<IAIObject>(new AIObjectDescription
    {
        AIClass = AITypes.TEXT_BLOCK, 
        TextWithMatchOptions = new TextWithMatchOptions(new RegExpProperty("Subtotal \(\d item\): \$[\d.]+"), MatchMethod.AI)
    });

    // Verify that the object was identified successfully.
    Verify.IsTrue(aiObject.Exists());
    
    // Print the text value.
    Console.WriteLine(aiObject.Value);
}