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

android.AndroidElementHandler Maven / Gradle / Ivy

The newest version!
package android;

import io.appium.java_client.MobileDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import static android.AndroidDriverInitializer.androidDriver;
import static android.AndroidDriverInitializer.wait;
import static core.reports.TestReporter.error;

/**
 * Created by Ismail on 6/4/2018.
 */
public class AndroidElementHandler extends AndroidElementHelper {

    /*************** Class Variables Section ***************/
    // Initialize AndroidElement as static variable
    public static AndroidElement element = null;

    /*************** Class Methods Section ***************/
    // This method move to AndroidElement location on web page
    public static AndroidElement coloringElement(AndroidElement androidElement) {
        // If element static variable has value then remove red circle of old element
        if (element != null)
            removeColoringAndroidElement((AndroidDriver) androidDriver, element);
        // Save androidElement to element static variable
        element = androidElement;
        // Move to element location on web page
        //new Actions(AndroidDriverInitializer.androidDriver).moveToElement(element).build().perform();
        // Coloring new element with red border style
        coloringAndroidElement((AndroidDriver) androidDriver, element);
        // Return element static variable
        return element;
    }

    // This method retrieves android wait object
    public static FluentWait getWaitObject() {
        List> ignoredExceptions = new ArrayList<>();
        ignoredExceptions.add(NoSuchElementException.class);
        ignoredExceptions.add(StaleElementReferenceException.class);
        ignoredExceptions.add(ElementClickInterceptedException.class);
        ignoredExceptions.add(ElementNotVisibleException.class);
        ignoredExceptions.add(ElementNotInteractableException.class);
        ignoredExceptions.add(ElementNotSelectableException.class);
        ignoredExceptions.add(InvalidElementStateException.class);

        return new FluentWait(androidDriver).withTimeout(Duration.ofMillis(5000)).pollingEvery(Duration.ofMillis(50)).ignoreAll(ignoredExceptions);
    }

    // This method retrieves Android Element after wait until condition
    public static AndroidElement waitUntil(AndroidElement androidElement, boolean clickable) {
        try {
            if (clickable)
                wait.until(ExpectedConditions.elementToBeClickable(androidElement));
            else
                wait.until(ExpectedConditions.visibilityOf(androidElement));
            return androidElement;
        } catch (Throwable throwable) {
            error("Unable to find the element.", throwable, true);
            return null;
        }
    }

    // This method retrieves Mobile Element after wait until condition
    public static MobileElement waitUntil(MobileElement androidElement, boolean clickable) {
        try {
            wait.until(ExpectedConditions.visibilityOf(androidElement));

            if (clickable)
                wait.until(ExpectedConditions.elementToBeClickable(androidElement));

            return androidElement;
        } catch (Throwable throwable) {
            error("Unable to find the element.", throwable, true);
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy