Highlighted
Super Contributor.
Super Contributor.
1491 views

Verify specific text is present on page in LeanFT

How is it possible to verify, that a specific text is present on webpage and check number of occurences?

0 Likes
4 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

Hi,

 

LeanFT provides 2 API's relevant for this type of operation (I will demonstrate using LeanFT Java SDK):

  • Each test object has the getTextLocations method, which returns a Rectangle[] describing all found locations.
  • The second option is locating text in an image using the ImageUtils.locateText method

Here is a Java code sample:

 

 @Test
    public void countTextOccurrences() throws GeneralLeanFtException {
        Browser browser = BrowserFactory.launch(BrowserType.CHROME);

        browser.navigate("www.google.com");

        System.out.println("number of occurrences of 'Google': " + browser.getTextLocations("Google").length);

        System.out.println("number of occurrences of 'Google': " + ImageUtils.locateText(browser.getSnapshot(),"Google").length);
    }

 

The output is:

number of occurrences of 'Google': 2
number of occurrences of 'Google': 2

 

Regards,

Anton

Highlighted
Micro Focus Expert
Micro Focus Expert

You can also try an use the getInnerText() method to get the text and do string manipulations to find if your string appears inside.

com.hp.lft.sdk.web.Browser b = BrowserFactory.launch(BrowserType.FIREFOX);
b.navigate("http://advantageonlineshopping.com/#/");

WebElement body = b.describe(WebElement.class, new WebElementDescription.Builder().tagName("body").build());
String s = body.getInnerText();
System.out.println(s);

Regards,

Dror

Highlighted
Super Contributor.
Super Contributor.

I used this in this way:

Assert.assertEquals(browser.getTextLocations("Sikeres fájlfeltöltés").length, 6);

I got error message:

com.hp.lft.sdk.GeneralLeanFtException: ERROR

String "Sikeres fájlfeltöltés" occurs 6 times on page.

0 Likes
Highlighted
Super Contributor.
Super Contributor.

I tried to use this code to count occurences, but it says: 0. I see 3 in browser.

while (lastIndex != -1) {
lastIndex = str.indexOf(findStr, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += findStr.length();
}
}

 

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.