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

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

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

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
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.exception.SeleniumUnexpectedException;
import com.onevizion.uitest.api.vo.ListboxElement;

@Component
public class Listbox {

    private static final String NAV_PANEL = "navigation";
    private static final String BUTTON_GROUP_FIELD = "trackortypes";
    private static final String BUTTON_GROUP_TASK = "tasks";
    private static final String BUTTON_GROUP_DRILLDOWN = "drilldownfields";
    private static final String BUTTON_GROUP_DATE_PAIR = "datepairs";
    private static final String BUTTON_GROUP_MARKUP = "markup";
    private static final String BUTTON_GROUP_VIEW_COLUMNS = "selectedColumns";
    private static final String BUTTON_GROUP_GRID_ROW_EDITOR_FIELDS = "designer";

    @Autowired
    private SeleniumSettings seleniumSettings;

    @Autowired
    private ListboxJs listboxJs;

    @Autowired
    private ListboxWait listboxWait;

    @Autowired
    private ElementWait elementWait;

    @Autowired
    private Element element;

    public List getElements(String listboxId) {
        return getItems(listboxId);
    }

    public List getGroups(String listboxId) {
        return getItems(listboxId);
    }

    private List getItems(String listboxId) {
        List elements = new ArrayList<>();

        seleniumSettings.getWebDriver().manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
        List webElements = seleniumSettings.getWebDriver().findElement(By.id(listboxId)).findElements(By.className("record"));
        seleniumSettings.getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        for (WebElement webElement : webElements) {
            if (webElement.getAttribute("class").contains("hidden")) {
                continue;
            }

            ListboxElement listboxElement = new ListboxElement();
            listboxElement.setWebElement(webElement);
            listboxElement.setId(webElement.getAttribute("id"));
            listboxElement.setLabel(webElement.findElement(By.className("labelField")).getAttribute("innerText"));
            elements.add(listboxElement);
        }

        return elements;
    }

    public void checkElementsCount(List elements, int size) {
        Assert.assertEquals(elements.size(), size);
    }

    public void checkElementByLabel(List elements, String label) {
        Assert.assertEquals(IntStream.range(0, elements.size()).filter(p -> elements.get(p).getLabel().equals(label)).count(), 1L, "Element with label [" + label + "] not found in Listbox");
    }

    public void checkElementByLabel(List elements, int position, String label) {
        Assert.assertEquals(IntStream.range(0, elements.size()).filter(p -> elements.get(p).getLabel().equals(label)).count(), 1L, "Element with label [" + label + "] not found in Listbox");
        Assert.assertEquals(IntStream.range(0, elements.size()).filter(p -> elements.get(p).getLabel().equals(label) && p == position - 1).count(), 1L, "Element with label [" + label + "] not found in position [" + position + "] in Listbox");
    }

    public void checkElementById(List elements, String id) {
        Assert.assertEquals(IntStream.range(0, elements.size()).filter(p -> elements.get(p).getId().equals(id)).count(), 1L, "Element with id [" + id + "] not found in Listbox");
    }

    public void checkElementById(List elements, int position, String id) {
        Assert.assertEquals(IntStream.range(0, elements.size()).filter(p -> elements.get(p).getId().equals(id)).count(), 1L, "Element with id [" + id + "] not found in Listbox");
        Assert.assertEquals(IntStream.range(0, elements.size()).filter(p -> elements.get(p).getId().equals(id) && p == position - 1).count(), 1L, "Element with id [" + id + "] not found in position [" + position + "] in Listbox");
    }

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

    public ListboxElement getElementByLabel(List elements, String label) {
        checkElementByLabel(elements, label);
        Optional optional = elements.stream().filter(p -> p.getLabel().equals(label)).findFirst();
        return optional.orElseThrow(() -> new SeleniumUnexpectedException());
    }

    private ListboxElement getElementById(List elements, String id) {
        checkElementById(elements, id);
        Optional optional = elements.stream().filter(p -> p.getId().equals(id)).findFirst();
        return optional.orElseThrow(() -> new SeleniumUnexpectedException());
    }

    public void moveElementByLabel(List elements, String label, String buttonId) {
        ListboxElement listboxElement = getElementByLabel(elements, label);
        selectElement(listboxElement);

        seleniumSettings.getWebDriver().findElement(By.id(buttonId)).click();
    }

    public void moveElementById(List elements, String id, String buttonId) {
        ListboxElement listboxElement = getElementById(elements, id);
        selectElement(listboxElement);

        seleniumSettings.getWebDriver().findElement(By.id(buttonId)).click();
    }

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

    public void selectElement(ListboxElement element) {
        listboxJs.scrollToElementInListbox(element.getWebElement());
        elementWait.waitElementVisible(element.getWebElement());
        element.getWebElement().click();
    }

    public void selectElementByLabel(List elements, String label) {
        ListboxElement listboxElement = getElementByLabel(elements, label);
        listboxJs.scrollToElementInListbox(listboxElement.getWebElement());
        elementWait.waitElementVisible(listboxElement.getWebElement());
        listboxElement.getWebElement().click();
    }

    public void switchToRootSubgroup(String listboxId) {
        element.click(seleniumSettings.getWebDriver().findElement(By.id(NAV_PANEL)).findElement(By.className("nav_link")));
        listboxWait.waitIsReadyListbox(listboxId);
    }

    public void switchToParentSubgroup(String listboxId) {
        List links = seleniumSettings.getWebDriver().findElement(By.id(NAV_PANEL)).findElements(By.className("nav_link"));
        element.click(links.get(links.size() - 2));
        listboxWait.waitIsReadyListbox(listboxId);
    }

    public void switchToSubgroupInList(String listboxId, String label) {
        List groups = getGroups(listboxId);
        ListboxElement group = getElementByLabel(groups, label);
        element.click(group.getWebElement());
        listboxWait.waitIsReadyListbox(listboxId);
    }

    public void switchToFieldGroup(String listboxId) {
        element.clickById(BUTTON_GROUP_FIELD);
        listboxWait.waitIsReadyListbox(listboxId);
    }

    public void switchToTaskGroup(String listboxId) {
        element.clickById(BUTTON_GROUP_TASK);
        listboxWait.waitIsReadyListbox(listboxId);
    }

    public void switchToDrillDownGroup(String listboxId) {
        element.clickById(BUTTON_GROUP_DRILLDOWN);
        listboxWait.waitIsReadyListbox(listboxId);
    }

    public void switchToDatePairGroup(String listboxId) {
        element.clickById(BUTTON_GROUP_DATE_PAIR);
        listboxWait.waitIsReadyListbox(listboxId);
    }

    public void switchToMarkupGroup(String listboxId) {
        element.clickById(BUTTON_GROUP_MARKUP);
        listboxWait.waitIsReadyListbox(listboxId);
    }

    public void switchToViewColumns() {
        element.clickById(BUTTON_GROUP_VIEW_COLUMNS);
    }

    public void switchToGridRowEditorFields(String listboxId) {
        element.clickById(BUTTON_GROUP_GRID_ROW_EDITOR_FIELDS);
        listboxWait.waitIsReadyListbox(listboxId);
    }

    public void waitIsReadyListbox(String listboxId) {
        listboxWait.waitIsReadyListbox(listboxId);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy