vendredi 5 juin 2020

find element in ad-grid with Selenium

I need to test a page on which I can create an order, and the created order should show up in ag-grid. I use Selenium for it. I know, that Selenium is not the best tool to test angular application, but I need to have cross browser test as well, and I didn't find better tool for it.

So initially I tried something like this:

protected String getDataFromGrid(By locator, int rowNum) {
    List<WebElement> elementList = driver.findElements(locator);
    return elementList.get(rowNum).getText();
}

public String getData(int rowNum) {
    String data = getDataFromGrid(colLocator, rowNum);
    if (StringUtils.isEmpty(data)) {
        scrollDown(driver);
        data = getDataFromGrid(colLocator, rowNum);
    }
    return data;
}

public static Integer getMatchingRowNum(String dataToFind) {
    int size = grid.getSize();
    for (int rowNum = 0; rowNum < size; rowNum++) {
        String actualData = getData(rowNum);
        if (dataToFind.equals(actualData)) {
            return rowNum;
        }
    }
    return null;
}

Of course the grid contains more data, than a String, just wanted to post simple code. This code works fine until the created element is rendered at the first part of the ag-grid. When I scroll down the way how it fill the array it seems unpredictable for me. During debug I used this code to see the current content of the elementList:

driver.findElements(colLocator).stream().map(WebElement::getText).collect(Collectors.toList())

The list size typically between 19-30, but only 8-11 contains data. I cannot find a way to map the rowNum to the index of the list. Are there any?

List content exmaple

Now I always iterate through the whole list (even the empty members) before scrolling and try to find matching element. Are there any better way to find an element in ag-grid?

Aucun commentaire:

Enregistrer un commentaire