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

com.epam.jdi.uitests.mobile.appium.elements.complex.Elements Maven / Gradle / Ivy

There is a newer version: 1.1.34
Show newest version
package com.epam.jdi.uitests.mobile.appium.elements.complex;
/*
 * Copyright 2004-2016 EPAM Systems
 *
 * This file is part of JDI project.
 *
 * JDI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JDI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JDI. If not, see .
 */


import com.epam.jdi.uitests.mobile.appium.elements.AppiumCascadeInit;
import com.epam.jdi.uitests.mobile.appium.elements.base.IHasElement;
import com.epam.jdi.uitests.mobile.appium.elements.common.Button;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.*;

import static com.epam.commons.EnumUtils.getEnumValue;
import static com.epam.commons.LinqUtils.first;
import static com.epam.commons.LinqUtils.select;
import static com.epam.jdi.uitests.core.settings.JDISettings.exception;
import static com.epam.jdi.uitests.core.settings.JDISettings.useCache;


/**
 * Created by Roman_Iovlev on 7/8/2015.
 */
public class Elements extends BaseSelector implements List {
    private Class classType;

    public Elements() {
        this(null, null);
    }

    public Elements(Class classType) {
        this(null, classType);
    }
    public Elements(By byLocator) {
        this(byLocator, null);
    }

    protected boolean isSelectedAction(String name) {
        return false;
    }

    protected boolean isSelectedAction(int index) {
        return false;
    }

    protected String getValueAction() {
        return "UNDEFINED";
    }

    public Elements(By byLocator, Class classType) {
        super(byLocator);
        this.classType = classType != null ? classType : (Class) Button.class;
        elements = new ArrayList<>();
    }

    protected boolean getElementByNameAction(WebElement element, String name) {
        return element.getText().equals(name);
    }

    private List elements;

    public List listOfElements() {
        return useCache && !elements.isEmpty()
            ? elements
            : (elements = select(getElements(), el -> {
                try {
                    T element = classType.newInstance();
                    element.setWebElement(el);
                    element.setParent(this);
                    new AppiumCascadeInit().initElements(element, avatar.getDriverName());
                    return element;
                } catch (Exception ex) {
                    throw exception("Can't instantiate list element");
                }
            }));
    }

    public int size() {
        return listOfElements().size();
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    public boolean contains(Object o) {
        return listOfElements().contains(o);
    }

    public Iterator iterator() {
        return listOfElements().iterator();
    }

    public Object[] toArray() {
        return listOfElements().toArray();
    }

    public  E[] toArray(E[] a) {
        return listOfElements().toArray(a);
    }

    public boolean add(T tType) {
        return listOfElements().add(tType);
    }

    public boolean remove(Object o) {
        return listOfElements().remove(o);
    }

    public boolean containsAll(Collection c) {
        return listOfElements().containsAll(c);
    }

    public boolean addAll(Collection c) {
        return listOfElements().addAll(c);
    }

    public boolean addAll(int index, Collection c) {
        return listOfElements().addAll(index, c);
    }

    public boolean removeAll(Collection c) {
        return listOfElements().removeAll(c);
    }

    public boolean retainAll(Collection c) {
        return listOfElements().retainAll(c);
    }

    public void clear() {
        listOfElements().clear();
    }

    public T get(int index) {
        return listOfElements().get(index);
    }
    public T get(String name) {
        return first(listOfElements(), el -> getElementByNameAction(el.getWebElement(), name));
    }
    public T get(Enum name) {
        return get(getEnumValue(name));
    }

    public T set(int index, T element) {
        return listOfElements().set(index, element);
    }

    public void add(int index, T element) {
        listOfElements().add(index, element);
    }

    public T remove(int index) {
        return listOfElements().remove(index);
    }

    public int indexOf(Object o) {
        return listOfElements().indexOf(o);
    }

    public int lastIndexOf(Object o) {
        return listOfElements().lastIndexOf(0);
    }

    public ListIterator listIterator() {
        return listOfElements().listIterator();
    }

    public ListIterator listIterator(int index) {
        return listOfElements().listIterator(index);
    }

    public List subList(int fromIndex, int toIndex) {
        return listOfElements().subList(fromIndex, toIndex);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy