2

I need a solution how to iterate value of i. Actually my test case contain one webtable and table have five column also fifth column contain four buttons. So depending on the first column value(for ex "admin" which is in first row) validate the value and according to that same row fifth column first button i have to click on that. I am using page object model and my all locator place in java class(for ex AddRoleLocator class name) and my actual logic present in another java class(for ex. AddRole). So that how to iterate value of i. my Locator key is like that

public static By noOfRows=By.Xpath("//table[@id='group-list']/tbody/tr["+i+"]/td[1]");

This is my actual logic

List<WebElement> noOfRows = wau.getData(KumoAccessManageLocator.KumoAccessManageTableRowCount);
            int count = noOfRows.size();
            System.out.println(count);
            for(int i=1;i<=count;i++)
            {
                if(wau.getText(KumoAccessManageLocator.KumoAccessManageRoleName).equals(ConstantProperties.getProperty("kumo.accessMange.newRoleName")))
                {
                    wau.clickButton(KumoAccessManageLocator.KumoAccessManageviewRights);
                }
            }  

so please suggest me how to resolve this problem with using page object model.

1
  • You're defining noOfRows as both a By and as a List<WebElement> It's hard to make sense out of your code shown. What is "wau.getData" If noOfRows contains the List<WebElements> why is it not referred to except to get the size? You need to show a lot more code, I think. Commented Oct 8, 2018 at 15:49

1 Answer 1

1

I'm guessing you are interacting with a table like this: https://jsfiddle.net/zvb01a67/

Sample HTML table

In this case what you would do is:

  1. Create a model for the table and extend a generic model of an HTML table.
  2. Uniquely identify the table on the page.
  3. Automate actions for the business table.

Here's how I do it usually in my automation projects:

public class GenericTable extends BasePage {

    // Constructor
    public GenericTable(By locator, WebDriver driver) {
        super(locator, driver);
    }

    // Generic Table methods
    public int getRowCount() { ... };
    public int getColumnCount() { ... };
    public WebElement searchAndGetRow(String columnHeader, String cellDataValue) { ... };
    public void selectRowContainingValue(String columnHeader, String cellDataValue) { ... };

}

Now let's create a business model table that extends this generic table.

public class UsersTable extends GenericTable {

    // Constructor
    public UsersTable(By locator, WebDriver driver) {
        super(locator, driver);
    }

    // Write this table specific code here
    public void selectUser(String username) {
        selectRowContainingValue("Username", username);
    }

    public int getIndexOfColumn(String columnName) {
        List<WebElement> headers = getTableHeaders();
        int colIndex = -1;
        for(WebElement header : headers) {
            if(header.getText().equalsIgnoreCase(columnName) {
                colIndex = headers.indexOf(header);
                break;
            }
        }
        return colIndex;
    }

    // Modify user
    public void modifyUser(String username) {
        int colUsernameIndex = getIndexOfColumn("Username");
        int colActionsIndex = getIndexOfColumn("Actions");

        // Get all rows
        List<WebElement> rows = getAllRows();
        for(WebElement row : rows) {
            // Match the username
            String usernameCellText = row.findElements(By.tagName("td")).getIndex(colUsernameIndex).getText();
            if(usernameCellText.equalsIgnoreCase(username)) {
                // Get actions cell
                WebElement actionsCell = row.findElements(By.tagName("td")).getIndex(colActionsIndex);
                // Locate the modify button in actions cell
                WebElement btnModify = actionsCell.findElement(By.xpath(".//button[text()='Modify']"));
                // click on it
                btnModify.click();
            }
        }
    }
}

Similarly you can write for other actions/buttons.

This way you just update the model if the developers change the table structure or add functionality to it. I've used this approach and even though setup time is more, it is efficient, maintainable, resuable and coupled with business logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.