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

com.intuit.karate.driver.appium.MobileDriverOptions Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
package com.intuit.karate.driver.appium;

import com.intuit.karate.core.ScenarioRuntime;
import com.intuit.karate.driver.DriverOptions;
import com.intuit.karate.driver.Driver;
import com.intuit.karate.driver.Element;
import com.intuit.karate.driver.DriverElement;
import com.intuit.karate.driver.MissingElement;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author babusekaran
 */
public class MobileDriverOptions extends DriverOptions {

    public MobileDriverOptions(Map options, ScenarioRuntime sr, int defaultPort, String defaultExecutable) {
        super(options, sr, defaultPort, defaultExecutable);
    }

    public boolean isWebSession() {
        // flag to know if driver runs for browser on mobile
        Map sessionPayload = super.getWebDriverSessionPayload();
        return getBrowserName(sessionPayload) != null;
    }

    @Override
    public Element waitForAny(Driver driver, String... locators) {
        if (isWebSession()) {
            return super.waitForAny(driver, locators);
        }
        long startTime = System.currentTimeMillis();
        List list = Arrays.asList(locators);
        boolean found = (boolean)driver.waitUntil(() -> {
            for (String locator: list) {
                try {
                    ((AppiumDriver)driver).elementId(locator);
                    return true;
                }
                catch (RuntimeException re){
                    logger.debug("failed to locate : {}", locator);
                }
            }
            return null;
        });
        // important: un-set the retry flag
        disableRetry();
        if (!found) {
            long elapsedTime = System.currentTimeMillis() - startTime;
            throw new RuntimeException("wait failed for: " + list + " after " + elapsedTime + " milliseconds");
        }
        if (locators.length == 1) {
            return DriverElement.locatorExists(driver, locators[0]);
        }
        for (String locator : locators) {
            Element temp = driver.optional(locator);
            if (temp.isPresent()) {
                return temp;
            }
        }
        // this should never happen
        throw new RuntimeException("unexpected wait failure for locators: " + list);

    }

    @Override
    public Element optional(Driver driver, String locator) {
        if (isWebSession()) {
            return super.optional(driver, locator);
        }
        try{
            retry(() -> {
                try {
                    ((AppiumDriver)driver).elementId(locator);
                    return true;
                } catch (RuntimeException re) {
                    return false;
                }
            }, b -> b, "optional (locator)", true);
            // the element exists, if the above function did not throw an exception
            return DriverElement.locatorExists(driver, locator);
        }
        catch (RuntimeException re) {
            return new MissingElement(driver, locator);
        }
    }

    protected static String getBrowserName(Map sessionPayload) {
        // get browserName from capabilities or desiredCapabilities node
        Map capabilities = (Map) sessionPayload.get("capabilities");
        Map desiredCapabilities = (Map) sessionPayload.get("desiredCapabilities");

        if (capabilities != null) {
            if (capabilities.containsKey("firstMatch")) {
                // sauce labs uses the firstMatch node for some reason
                // see https://support.saucelabs.com/hc/en-us/articles/4412359870231-Migrating-Appium-Real-Device-Tests-to-W3C
                List> firstMatch = (List>) capabilities.get("firstMatch");
                if (firstMatch.size() == 0) {
                    throw new RuntimeException("firstMatch node in webdriver session is empty");
                }
                return (String) firstMatch.get(0).get("browserName");
            } else {
                return (String) capabilities.get("browserName");
            }
        } else if (desiredCapabilities != null) {
            return (String) desiredCapabilities.get("browserName");
        } else {
            // no browserName found
            return null;
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy