All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.onevizion.uitest.api.helper.Grid Maven / Gradle / Ivy

The newest version!
package com.onevizion.uitest.api.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.testng.Assert;

import com.onevizion.uitest.api.SeleniumSettings;
import com.onevizion.uitest.api.helper.page.button.PageButton;

@Component
public class Grid {

    @Autowired
    private Js js;

    @Autowired
    private SeleniumSettings seleniumSettings;

    @Autowired
    private Wait wait;

    @Autowired
    private Checkbox checkbox;

    @Autowired
    private PageButton pageButton;

    public boolean isGridEmpty(Long gridId) {
        int rowsCnt = js.getGridRowsCount(gridId);
        if (rowsCnt == 1) {
            String rowId = js.getGridSelectedRowId(gridId);
            if (rowId.equals("0")) {
                return true;
            }
        }

        return false;
    }

    public int getGridRowsCount(Long gridId) {
        int rowsCnt = js.getGridRowsCount(gridId);
        if (rowsCnt == 1) {
            String rowId = js.getGridSelectedRowId(gridId);
            if ("0".equals(rowId)) {
                return 0;
            }
        }

        return rowsCnt;
    }

    public int getTOGridRowsCount(Long gridId) {
        Double rowsCnt = js.getTOGridRowsCount(gridId);
        if (rowsCnt.equals(0.3333333333333333D)) {
            String rowId = js.getGridSelectedRowId(gridId);
            if ("0".equals(rowId)) {
                return 0;
            }
        }

        return rowsCnt.intValue();
    }

    public void selectFirstRow(Long gridId) {
        js.selectGridRow(gridId, 0);
    }

    public void selectLastRow(Long gridId) {
        js.selectGridRow(gridId, getGridRowsCount(gridId) - 1);
    }

    public void checkTbGridRowByRowIndex(Long gridId, int rowIndex, Map vals) {
        for (Entry val : vals.entrySet()) {
            int columnIndex = js.getColumnIndexById(gridId, val.getKey());
            //String columnType = jsHelper.getGridColumnType(gridId, columnIndex);
            //String value = jsHelper.getGridCellValueTxtByRowIndexAndColIndex(gridId, rowIndex, columnIndex);
            //if (" ".equals(value)) {
            //    value = "";
            //}
            //value = value.replaceAll("^<[aA].*?>", "").replaceAll("$", "");
            //value = StringUtils.substringBefore(value, "\n");
            //if ("RichMemo".equals(columnType)) {
            //    value = value.replaceAll(AbstractSeleniumTest.SPECIAL_CHARACTERS_ENCODED, AbstractSeleniumTest.SPECIAL_CHARACTERS);
            //}
            //Assert.assertEquals(value, val.getValue(), "Check of grid row " + rowIndex + " is failed on " + columnIndex + " column.");

            wait.waitGridCellTxtValue(gridId, columnIndex, rowIndex, val.getValue());
        }
    }

    public void checkGridRowByRowIndexAndColIndex(Long gridId, int rowIndex, Map vals) {
        for (Entry val : vals.entrySet()) {
            String value = js.getGridCellValueByRowIndexAndColIndex(gridId, rowIndex, val.getKey());
            value = value.replaceAll("<[aA].*?>", "").replaceAll("", "");
            value = value.replaceAll("<[tT][aA][bB][lL][eE].*?>", "").replaceAll("", "");
            value = value.replaceAll("<[tT][bB][oO][dD][yY].*?>", "").replaceAll("", "");
            value = value.replaceAll("<[tT][rR].*?>", "").replaceAll("", "");
            value = value.replaceAll("<[tT][dD].*?>", "").replaceAll("", "");
            value = value.replaceAll("<[dD][iI][vV].*?>", "").replaceAll("", "");
            value = value.replaceAll("<[sS][vV][gG].*?>", "").replaceAll("", "");
            value = value.replaceAll("<[uU][sS][eE].*?>", "").replaceAll("", "");
            value = value.trim();
            value = StringUtils.substringBefore(value, "\n");
            Assert.assertEquals(value, val.getValue(), "Check of grid row " + rowIndex + " is failed on columnIndex=[" + val.getKey() + "] columnId=[" + val.getKey() + "].");
        }
    }

    public void checkGridRowByRowIndexAndWait(Long gridId, int rowIndex, Map vals) {
        for (Entry val : vals.entrySet()) {
            int columnIndex = js.getColumnIndexById(gridId, val.getKey());
            wait.waitGridCellValue(gridId, columnIndex, rowIndex, val.getValue());
        }
    }

    public void checkGridRowByRowIndex(Long gridId, int rowIndex, Map vals) {
        for (Entry val : vals.entrySet()) {
            int columnIndex = js.getColumnIndexById(gridId, val.getKey());
            String value = js.getGridCellValueByRowIndexAndColIndex(gridId, rowIndex, columnIndex);
            value = value.replaceAll("^<[aA].*?>", "").replaceAll("$", "");
            value = StringUtils.substringBefore(value, "\n");
            Assert.assertEquals(value, val.getValue(), "Check of grid row " + rowIndex + " is failed on columnIndex=[" + columnIndex + "] columnId=[" + val.getKey() + "].");
        }
    }

    public void checkGridRowsByRowIndex(Long gridId, int rowIndexStart, int rowIndexEnd, Map vals) {
        for (int i = rowIndexStart; i <= rowIndexEnd; i++) {
            checkGridRowByRowIndex(gridId, i, vals);
        }
    }

    public void checkGridRowByRowId(Long gridId, String rowId, Map vals) {
        for (Entry val : vals.entrySet()) {
            int columnIndex = js.getColumnIndexById(gridId, val.getKey());
            String value = js.getGridCellValueByRowIdAndColIndex(gridId, rowId, columnIndex);
            value = value.replaceAll("^<[aA].*?>", "").replaceAll("$", "");
            value = StringUtils.substringBefore(value, "\n");
            Assert.assertEquals(value, val.getValue(), "Check of grid row with id[" + rowId + "] is failed on columnIndex=[" + columnIndex + "] columnId=[" + val.getKey() + "].");
        }
    }

    public void deleteCurrentRow(Long gridId) {
        int oldCnt = js.getGridRowsCount(gridId);
        if (oldCnt > 1) {
            oldCnt = oldCnt - 1;
        }

        pageButton.clickDeleteGridAndWait(gridId);

        int newCnt = js.getGridRowsCount(gridId);
        Assert.assertEquals(newCnt, oldCnt, "Delete row is wrong");
    }

    public List selectPrivilegieGridColumn(Long gridId, int colIdx, int firstRowIndex, int lastRowIndex, String val) {
        List ret = new ArrayList<>();
        for (int i = firstRowIndex; i <= lastRowIndex; i++) {
            js.selectGridCellByRowIndexAndColIndex(gridId, i, colIdx);
            new Select(seleniumSettings.getWebDriver().findElement(By.id("lbpriv"))).selectByVisibleText(val);
            ret.add(js.getGridSelectedRowId(gridId));
        }
        return ret;
    }

    public List selectAssignmentGridColumn(Long gridId, int colIdx, int firstRowIndex, int lastRowIndex) {
        List ret = new ArrayList<>();
        for (int i = firstRowIndex; i <= lastRowIndex; i++) {
            js.selectGridCellByRowIndexAndColIndex(gridId, i, colIdx);
            WebElement webElement = (WebElement) js.getGridCellCheckboxByRowIndexAndColIndex(gridId, i, colIdx);
            checkbox.clickByElement(webElement);
            ret.add(js.getGridSelectedRowId(gridId));
        }
        return ret;
    }

    public void selectAssignmentGridColumnNew(Long gridId, int colIdxCheckbox, int colIdxName, Map values, String priv) {
        int cnt = getGridRowsCount(gridId);

        @SuppressWarnings("unchecked")
        List names = (List) js.getGridCellsValuesForColumnByColIndexNew(gridId, cnt, colIdxName);

        Assert.assertEquals(names.containsAll(values.keySet()), true, "Some of expected values [" + values.keySet() + "] not exist in grid [" + names + "]");

        for (int i = 0; i < cnt; i++) {
            if (values.containsKey(names.get(i)) && values.get(names.get(i)).contains(priv)) {
                js.selectGridCellByRowIndexAndColIndex(gridId, i, colIdxCheckbox);
                WebElement webElement = (WebElement) js.getGridCellCheckboxByRowIndexAndColIndex(gridId, i, colIdxCheckbox);
                checkbox.clickByElement(webElement);
            }
        }
    }

    //TODO remove when getValue will be return 1 and 0 instead of html
    public List selectAssignmentGridColumn2(Long gridId, int colIdx, int firstRowIndex, int lastRowIndex) {
        List ret = new ArrayList<>();
        for (int i = firstRowIndex; i <= lastRowIndex; i++) {
            js.selectGridCellByRowIndexAndColIndex(gridId, i, colIdx);
            WebElement webElement = (WebElement) js.getGridCellCheckboxByRowIndexAndColIndex(gridId, i, colIdx);
            checkbox.clickByElement(webElement);
            ret.add("cb" + js.getGridSelectedRowId(gridId));
        }
        return ret;
    }

    public void selectAssignmentGridColumn2New(Long gridId, int colIdxCheckbox, int colIdxName, List values) {
        int cnt = getGridRowsCount(gridId);

        @SuppressWarnings("unchecked")
        List names = (List) js.getGridCellsValuesForColumnByColIndexNew(gridId, cnt, colIdxName);

        Assert.assertEquals(names.containsAll(values), true, "Some of expected values [" + values + "] not exist in grid [" + names + "]");

        for (int i = 0; i < cnt; i++) {
            if (values.contains(names.get(i))) {
                js.selectGridCellByRowIndexAndColIndex(gridId, i, colIdxCheckbox);
                WebElement webElement = (WebElement) js.getGridCellCheckboxByRowIndexAndColIndex(gridId, i, colIdxCheckbox);
                checkbox.clickByElement(webElement);
            }
        }
    }

    public void clearPrivilegieGridColumn(Long gridId, int colIdx) {
        int cnt = getGridRowsCount(gridId);

        @SuppressWarnings("unchecked")
        List vals = (List) js.getGridCellsValuesForColumnByColIndex(gridId, cnt, colIdx);
        for (int i = 0; i < cnt; i++) {
            if (!"".equals(vals.get(i))) {
                js.selectGridCellByRowIndexAndColIndex(gridId, i, colIdx);
                new Select(seleniumSettings.getWebDriver().findElement(By.id("lbpriv"))).selectByVisibleText("");
            }
        }
    }

    public void clearAssignmentGridColumn(Long gridId, int colIdx) {
        int cnt = getGridRowsCount(gridId);

        @SuppressWarnings("unchecked")
        List vals = (List) js.getGridCellsValuesForColumnByColIndex(gridId, cnt, colIdx);
        for (int i = 0; i < cnt; i++) {
            if ("1".equals(vals.get(i))) {
                js.selectGridCellByRowIndexAndColIndex(gridId, i, colIdx);
                WebElement webElement = (WebElement) js.getGridCellCheckboxByRowIndexAndColIndex(gridId, i, colIdx);
                checkbox.clickByElement(webElement);
            }
        }
    }

    //TODO remove when getValue will be return 1 and 0 instead of html
    public void clearAssignmentGridColumn2(Long gridId, Long colIdx) {
        List checkboxes = seleniumSettings.getWebDriver().findElements(By.name("cb" + gridId + "_" + colIdx));
        for (Long i = 0L; i < checkboxes.size(); i++) {
            if (checkboxes.get(i.intValue()).isSelected() && checkboxes.get(i.intValue()).isEnabled()) {
                checkbox.clickByElement(checkboxes.get(i.intValue()));
            }
        }
    }

    public void checkPrivilegieGridColumn(Long gridId, int colIdx, List vals, String val) {
        int cnt = getGridRowsCount(gridId);

        @SuppressWarnings("unchecked")
        List gridVals = (List) js.getGridCellsValuesForColumnByColIndex(gridId, cnt, colIdx);
        for (int i = 0; i < cnt; i++) {
            String rowId = js.getGridRowIdByIndex(gridId, i);
            if (vals.contains(rowId)) {
                Assert.assertEquals(gridVals.get(i), val, "Check priv for row id=[" + rowId + "] is failed");
            } else {
                Assert.assertEquals(gridVals.get(i), "", "Check priv for row id=[" + rowId + "] is failed");
            }
        }
    }

    public void checkAssignmentGridColumn(Long gridId, int colIdx, List vals) {
        int cnt = getGridRowsCount(gridId);

        @SuppressWarnings("unchecked")
        List gridVals = (List) js.getGridCellsValuesForColumnByColIndex(gridId, cnt, colIdx);
        for (int i = 0; i < cnt; i++) {
            String rowId = js.getGridRowIdByIndex(gridId, i);
            if (vals.contains(rowId)) {
                Assert.assertEquals(gridVals.get(i), "1", "Check priv for row id=[" + rowId + "] is failed");
            } else {
                Assert.assertEquals(gridVals.get(i), "0", "Check priv for row id=[" + rowId + "] is failed");
            }
        }
    }

    public void checkAssignmentGridColumnNew(Long gridId, int colIdxCheckbox, int colIdxName, Map values, String priv) {
        int cnt = getGridRowsCount(gridId);

        @SuppressWarnings("unchecked")
        List names = (List) js.getGridCellsValuesForColumnByColIndexNew(gridId, cnt, colIdxName);

        Assert.assertEquals(names.containsAll(values.keySet()), true, "Some of expected values [" + values.keySet() + "] not exist in grid [" + names + "]");

        @SuppressWarnings("unchecked")
        List gridVals = (List) js.getGridCellsValuesForColumnByColIndex(gridId, cnt, colIdxCheckbox);
        for (int i = 0; i < cnt; i++) {
            if (values.containsKey(names.get(i)) && values.get(names.get(i)).contains(priv)) {
                Assert.assertEquals(gridVals.get(i), "1", "Check priv for row=[" + names.get(i) + "] is failed");
            } else {
                Assert.assertEquals(gridVals.get(i), "0", "Check priv for row=[" + names.get(i) + "] is failed");
            }
        }
    }

    //TODO remove when getValue will be return 1 and 0 instead of html
    public void checkAssignmentGridColumn2(Long gridId, Long colIdx, List vals) {
        int cnt = getGridRowsCount(gridId);

        List checkboxes = seleniumSettings.getWebDriver().findElements(By.name("cb" + gridId + "_" + colIdx));
        for (Long i = 0L; i < cnt; i++) {
            boolean selected = checkboxes.get(i.intValue()).isSelected();
            String id = checkboxes.get(i.intValue()).getAttribute("id");
            if (vals.contains(id)) {
                Assert.assertEquals(selected, true, "Check checkbox with id=[" + id + "] is failed");
            } else {
                Assert.assertEquals(selected, false, "Check checkbox with id=[" + id + "] is failed");
            }
        }
    }

    public void checkAssignmentGridColumn2New(Long gridId, int colIdxCheckbox, int colIdxName, List values) {
        int cnt = getGridRowsCount(gridId);

        @SuppressWarnings("unchecked")
        List names = (List) js.getGridCellsValuesForColumnByColIndexNew(gridId, cnt, colIdxName);

        Assert.assertEquals(names.containsAll(values), true, "Some of expected values [" + values + "] not exist in grid [" + names + "]");

        List checkboxes = seleniumSettings.getWebDriver().findElements(By.name("cb" + gridId + "_" + colIdxCheckbox));
        for (Long i = 0L; i < cnt; i++) {
            boolean selected = checkboxes.get(i.intValue()).isSelected();
            if (values.contains(names.get(i.intValue()))) {
                Assert.assertEquals(selected, true, "Check checkbox with id=[" + names.get(i.intValue()) + "] is failed");
            } else {
                Assert.assertEquals(selected, false, "Check checkbox with id=[" + names.get(i.intValue()) + "] is failed");
            }
        }
    }

    public int checkColumns(Long gridIndex, int columnIndex, List columnNames) {
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(0)); //CHECKBOX
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(1)); //DATE
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(2)); //DB_DROP_DOWN
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(3)); //DB_SELECTOR
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(4)); //DROP_DOWN
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(5)); //ELECTRONIC_FILE
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(6)); //HYPERLINK
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(7)); //LATITUDE
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(8)); //LONGITUDE
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(9)); //MEMO
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(10)); //NUMBER
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(11)); //SELECTOR
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(12)); //TEXT
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(13)); //TRACKOR_SELECTOR
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(14)); //WIKI
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(15)); //MULTI_SELECTOR
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(16)); //DATE_TIME
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(17)); //TIME
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(18)); //TRACKOR_DROPDOWN
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(19)); //CALCULATED
        if (columnNames.get(20) != null) { //Workplan and Tasks and Workflow trackor types not support
            columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(20)); //ROLLUP
        }
        columnIndex = checkColumnByIndex(gridIndex, columnIndex, columnNames.get(21)); //MULTI_TRACKOR_SELECTOR

        return columnIndex;
    }

    public int checkColumnByIndex(Long gridIndex, int columnIndex, String columnIdx) {
        columnIndex = columnIndex + 1;
        Assert.assertEquals(js.isGridColumnHidden(gridIndex, columnIndex).booleanValue(), false, "Grid have wrong columns");
        Assert.assertEquals(js.getGridColIdByIndex(gridIndex, columnIndex), columnIdx, "Grid have wrong columns");
        return columnIndex;
    }

    public int checkColumnByName(Long gridIndex, int columnIndex, String columnName) {
        columnIndex = columnIndex + 1;
        Assert.assertEquals(js.isGridColumnHidden(gridIndex, columnIndex).booleanValue(), false, "Grid have wrong columns");
        Assert.assertEquals(js.getGridColumnLabelByColIndex(gridIndex, columnIndex, 0), columnName, "Grid have wrong columns");
        return columnIndex;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy