Get Count task | Zoho QEngine Help

Get Count task

In a nutshell

The Get Count task counts the number of elements matching a locator during test execution and stores the result in a variable. This lets a script confirm how many matching elements are present on a page, such as the number of product cards in a listing, without checking each one individually.

1. Overview

The Get Count task returns the count of elements matching the specified locator during test execution and stores the result in a variable as a number. This is useful when the test needs to check whether the page contains the expected number of matching elements, such as validating a search result set or confirming a repeated UI pattern before moving to the next step.
During test execution, a count of zero confirms that an element is genuinely absent from the page. This differs from a visibility check, which only confirms whether an element is displayed, not whether it exists in the DOM.
NotesNote: If the page loads content asynchronously, pair Get Count task with a <Wait> task to ensure the element is rendered before the count is captured.

2. Syntax

<variable> = getCount(<locator>);

ParameterData TypeDescription
variableLONGStores the number of matching elements found.
locatorSTRING

Identifies the element whose count is to be fetched.


NotesNote: Locators can be fetched in one of two ways, either by manually inspecting the webpage to identify the element, or by using a recorded element saved during a recording session. Learn more about Elements.


Return Type

LONG

3. Examples

Example 1: Counting the products shown on the home page

Confirms the ZWatch Commerce home page shows the expected number of product cards.
// Open the ZWatch Commerce home page using the Open URL task
openURL("https://zwatch.zohocommerce.com/", "same window");
// Wait until the product cards are present on the page
wait(10, ".theme-prod-box", event.PRESENT);
// Create a variable 'productCount' to store the number of product cards found
productCount = getCount(".theme-prod-box");
// output: 8
// Confirm the count matches the expected number of featured products using conditional logic
if (productCount != 8)
{
throwError("Home page does not show the expected number of product cards"); //Learn more about Throw Error task
}

where:

"https://zwatch.zohocommerce.com/"ZWatch Commerce home page URL.
"same window"Opens the page in the window already running the test.
10Maximum time to wait for the product cards to be present, in seconds.
.theme-prod-boxLocator matching each product card.
event.PRESENTWaits until the product cards exist on the page.
productCountVariable storing the number of product cards found.
8Expected number of product cards on the home page.

Example 2: Confirming a search returns at least one result

Enters a search term into the ZWatch Commerce search box and submits it. The script then counts the returned result rows to confirm the search isn't empty.

// Open the ZWatch Commerce home page using the Open URL task
openURL("https://zwatch.zohocommerce.com/", "same window");
// Click the search icon to reveal the search input using the Click task
click(".theme-search");
// Enter a search term into the search box using the Set Value task
setValue("[data-search-input]", "Chrono 44");
// Submit the search by pressing Enter using the Send Keys task
sendKeys("[data-search-input]", [keys.ENTER]);
// Wait until the search result rows are present
wait(10, ui.zwatch.search-result-row, event.PRESENT);
// Create a variable 'resultCount' to store the number of search results found
resultCount = getCount(ui.zwatch.search-result-row);
// Confirm at least one result was found using conditional logic
if (resultCount == 0)
{
throwError("No search results were found"); //Learn more about Throw Error task
}

where:

"https://zwatch.zohocommerce.com/"ZWatch Commerce home page URL.
"same window"Opens the page in the window already running the test.
.theme-searchLocator for the search icon.
[data-search-input]Locator for the search box.
"Chrono 44"Search term entered into the search box.
keys.ENTERSimulated key press used to submit the search.
10Maximum time to wait for the search result rows to be present, in seconds.
ui.zwatch.search-result-rowLocator matching each search result row.
resultCountVariable storing the number of results found.

Example 3: Counting the navigation links in the site header

Verifies that the ZWatch Commerce site header shows the expected number of navigation links.

// Open the ZWatch Commerce home page using the Open URL task
openURL("https://zwatch.zohocommerce.com/", "same window");
// Wait until the navigation links are present on the page
wait(10, ".theme-menu a", event.PRESENT);
// Create a variable 'navLinkCount' to store the number of navigation links found
navLinkCount = getCount(".theme-menu a");
// output: 3
// Confirm the count matches the expected number of navigation links using conditional logic
if (navLinkCount != 3)
{
throwError("Site header does not show the expected number of navigation links"); //Learn more about Throw Error task
}

where:

"https://zwatch.zohocommerce.com/"ZWatch Commerce home page URL.
"same window"Opens the page in the window already running the test.
10Maximum time to wait for the navigation links to be present, in seconds.
.theme-menu aLocator matching each navigation link.
event.PRESENTWaits until the navigation links exist on the page.
navLinkCountVariable storing the number of navigation links found.
3Expected number of navigation links in the site header.

Example 4: Counting flagged records in a Inventory Manager application

Opens the Inventory report in a Zoho Creator Inventory Manager application, applies a Flagged for Reorder filter, and confirms the number of filtered records matches the expected count.

// Open the Inventory report in a Zoho Creator Inventory Manager application using the Open URL task
openURL("https://creatorapp.zoho.com/<orgname>/inventory-manager#Report:Inventory", "same window");
// Wait until the report is present on the page
wait(10, ui.inventorymanager.inventory-report, event.PRESENT);
// Click the filter icon to open the filter panel using the Click task
click(ui.inventorymanager.filter-icon);
// Click the Flagged for Reorder option under the filter using the Click task
click(ui.inventorymanager.filter-flagged-for-reorder);
// Wait until the filtered record rows are present
wait(10, ui.inventorymanager.record-row, event.PRESENT);
// Create a variable 'flaggedCount' to store the number of filtered records found
flaggedCount = getCount(ui.inventorymanager.record-row);
// output: 2
// Confirm the count matches the expected number of flagged records using conditional logic tasks
if (flaggedCount != 2)
{
throwError("Unexpected number of records flagged for reorder"); //Learn more about Throw Error task
}

where:

"https://creatorapp.zoho.com/<orgname>/inventory-manager#Report:Inventory"Inventory report URL.
"same window"Opens the report in the window already running the test.
ui.inventorymanager.inventory-reportLocator for the Inventory report.
ui.inventorymanager.filter-iconLocator for the filter icon.
ui.inventorymanager.filter-flagged-for-reorderLocator for the Flagged for Reorder filter option.
10Maximum time to wait for the report and filtered rows to be present, in seconds.
ui.inventorymanager.record-rowLocator matching each filtered record row.
flaggedCountVariable storing the number of flagged records found.
2Expected number of records flagged for reorder.

4. Use cases

Scenario 1: Confirming an out-of-stock product doesn't offer an Add to Cart option

When a product is out of stock, shoppers should not be able to add it to their cart. Instead of displaying a disabled or non-functional Add to Cart button, the product page should remove the option entirely to avoid misleading users.
A tester uses Zoho QEngine to verify that the product page does not display any Add to Cart button for an out-of-stock product by confirming that no matching elements are found.
Steps
  1. Open the Sol Analog product page in the same window.
  2. Wait for the product page to load.
  3. Count the Add to Cart buttons on the page.
  4. Confirm the count is zero.

Implementation

// Step 1: Open the Sol Analog product page on the ZWatch Commerce website in the same window using the Open URL task
openURL("https://zwatch.zohocommerce.com/products/sol-analog/3531661000000075636", "same window");
// Step 2: Wait until the product page has loaded
wait(10, "[data-zs-selling-price]", event.PRESENT);
// Step 3: Create a variable 'addToCartCount' to store the number of Add to Cart buttons found
addToCartCount = getCount(".theme-product-qty-and-add-cart");
// output: 0
// Step 4: Confirm the count is zero using conditional logic tasks
if (addToCartCount != 0)
{
throwError("Add to Cart option is present for an out-of-stock product"); //Learn more about Throw Error task
}
Scenario 2: Confirming a search returns the expected number of results

When a shopper searches for a specific watch model, the results should include only that model and not unrelated products or an empty result set. Showing more or fewer matches than expected would leave the shopper uncertain whether the item actually exists in the catalog.
A tester uses Zoho QEngine to verify that searching for the Diver Chronomat model on ZWatch Commerce returns exactly one matching result by counting the search result rows returned.
Steps
  1. Open the home page in the same window.
  2. Search for "Diver Chronomat."
  3. Wait for the search results to load.
  4. Count the search result rows.
  5. Confirm exactly one result is shown.

Implementation

// Step 1: Open the ZWatch Commerce home page in the same window using the Open URL task
openURL("https://zwatch.zohocommerce.com/", "same window");
// Step 2: Click the search icon to reveal the search input using the Click task
click(".theme-search");
// Step 3: Enter a search term into the search box using the Set Value task
setValue("[data-search-input]", "Diver Chronomat");
// Step 4: Submit the search by pressing Enter using the Send Keys task
sendKeys("[data-search-input]", [keys.ENTER]);
// Step 5: Wait until the search result rows are present
wait(10, ui.zwatch.search-result-row, event.PRESENT);
// Step 6: Create a variable 'resultCount' to store the number of search results found
resultCount = getCount(ui.zwatch.search-result-row);
// output: 1
// Step 7: Confirm exactly one result is shown using conditional logic tasks
if (resultCount != 1)
{
throwError("Search did not return exactly one matching result"); //Learn more about Throw Error task
}
Scenario 3: Confirming a department search returns the correct number of matching employees in an embedded report

Consider a Zoho Creator HR Portal application that HR managers use to manage employee records. The Employee Directory page includes a search input and an embedded Employees report displayed in an iframe. Managers type a department name into the search field to filter the embedded report and confirm only matching employees are listed.
A tester uses Zoho QEngine to confirm that entering a department name in the search field and clicking Search returns the correct number of matching employee records in the embedded report.
Steps
  1. Open the Employee Directory page in the HR Portal application.
  2. Wait for the search field to be present.
  3. Enter the department name in the search field.
  4. Click the Search button to filter the embedded report.
  5. Wait for the filtered results to load in the embedded report.
  6. Switch to the embedded report iframe.
  7. Wait for the employee record rows to be present within the iframe.
  8. Count the matching employee record rows.
  9. Confirm the count matches the expected number of employees in that department.

Implementation

// Step 1: Open the Employee Directory page in the HR Portal application using the Open URL task
openURL("https://creatorapp.zoho.com/<orgname>/hr-portal#Page:Employee_Directory", "same window");
// Step 2: Wait until the search field is present on the page
wait(10, ui.hrportal.search-field, event.PRESENT);
// Step 3: Enter the department name into the search field using the Set Value task
setValue(ui.hrportal.search-field, "Engineering");
// Step 4: Click the Search button to filter the embedded report using the Click task
click(ui.hrportal.search-button);
// Step 5: Wait for the filtered results to load in the embedded report
wait(10, ui.hrportal.employees-report-frame, event.PRESENT);
// Step 6: Switch into the embedded report iframe using the Switch To task
switchTo(ui.hrportal.employees-report-frame, True);
// Step 7: Wait until the employee record rows are present within the iframe
wait(10, ui.hrportal.employee-record-row, event.PRESENT);
// Step 8: Create a variable 'employeeCount' to store the number of matching employee records found
employeeCount = getCount(ui.hrportal.employee-record-row);
// output: 4
// Step 9: Confirm the count matches the expected number of employees in the Engineering department using conditional logic tasks
if (employeeCount != 4)
{
throwError("Employee count does not match the expected number for the selected department"); //Learn more about Throw Error task
}

5. Points to note

  1. For pages that load content asynchronously, use a Wait task before Get Count to ensure the elements have rendered before retrieving the count.
  2. Get Count checks the number of matching elements without considering their order or position on the page. Combine it with Get Text or Assert Text to check the content of a specific match.
  3. Get Count task only counts elements within the currently active window, tab, or iframe. Use the Switch To task first if the elements being counted sit in a different context.
  4. Prefer a stable, specific locator such as an ID, name, or CSS attribute selector, when using the Get Count task. Full XPath locators may break if the page structure changes, and broad locators can produce inaccurate counts.

6. Related topics

  1. Get Text
  2. Get Attribute
  3. Assert Visible

7. What's next

Next step
Previous step
Next step

Now that you've learned how to count matching elements using the Get Count task, learn how to retrieve an element's width using the Get Element Width task.

Previous step

Before using the Get Count task, ensure that you've learned how to retrieve an element's attribute value using the Get Attribute task