Validate All Rows in a Table Using Custom Code

Overview

When testing applications that display data in tables, you may need to verify that every row contains certain elements or data. For example, you might need to confirm that each row in a results table has a specific button and that a corresponding cell contains text. This article provides a custom JavaScript solution to iterate through each row of a table and validate its contents.

How It Works

The solution uses a custom code action to execute JavaScript directly in the browser. The script identifies all rows in a table, loops through them, and checks for specific conditions within each row. If any row fails the validation, the script immediately stops and the test step fails. The script also stores the failing row number in a local variable for easier debugging.

You will need to update the CSS selectors in this code to match the structure of your application.

(function() {
var rows = document.querySelectorAll("table tbody tr");
var failingRow = -1;

for (var i = 0; i < rows.length; i++) {
    var rowNumber = i + 1;
    var row = rows[i];

// Retrieve button column
    var retrieveBtn = row.querySelector(".retrieve-btn");

// Account name column
    var accountCell = row.querySelector("td.account-name");
    var accountText = accountCell ? accountCell.innerText.trim() : "";

var retrieveExists = !!retrieveBtn;
    var accountValid = accountText.length > 0;

// If either value missing → stop + fail
    if (!retrieveExists || !accountValid) {
        failingRow = rowNumber;

// Save failing row in Functionize local variable
        fze.local.failing_row_number = failingRow.toString();

// Return false → custom action fails
        return false;
    }
}

// All rows valid
fze.local.failing_row_number = "NONE";
return true;
})();

Code Explanation

  1. The script first selects all table rows using document.querySelectorAll("table tbody tr").
  2. It then enters a for loop to process each row individually.
  3. Inside the loop, it uses row.querySelector() to search for specific elements within the current row. In this example, it looks for an element with class .retrieve-btn and a table cell with class .account-name.
  4. It then performs validation. The condition if (!retrieveExists || !accountValid) checks if the button was not found OR if the account name cell was empty.
  5. If a row is invalid, the script saves the row number to a local variable named failing_row_number using fze.local and returns false, which causes the custom action to fail.
  6. If the loop completes without finding any invalid rows, it means all rows are valid. The script then sets the failing_row_number variable to "NONE" and returns true, passing the step.

Limitations

The provided code is a template. The CSS selectors (e.g., table tbody tr, .retrieve-btn, td.account-name) are examples and must be updated to correctly target the elements in your specific application. The validation logic inside the loop can also be adjusted to fit your unique requirements.

Related Information

This approach can be adapted for various row-by-row validation scenarios, such as checking values, element states, or attributes. For more information on advanced validation, please refer to documentation on custom JavaScript actions and using local variables.