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

com.codeborne.selenide.appium.WebdriverUnwrapper Maven / Gradle / Ivy

There is a newer version: 7.5.1
Show newest version
package com.codeborne.selenide.appium;

import com.codeborne.selenide.Driver;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WrapsDriver;
import org.openqa.selenium.WrapsElement;

import java.util.Optional;

/**
 * A temporary solution to fix ...
 * Will be replaced by a better solution in Selenide.
 */
public class WebdriverUnwrapper {
  public static boolean isMobile(Driver driver) {
    return instanceOf(driver, AppiumDriver.class);
  }

  public static boolean isMobile(SearchContext driver) {
    return instanceOf(driver, AppiumDriver.class);
  }

  public static  boolean instanceOf(Driver driver, Class klass) {
    return cast(driver, klass).isPresent();
  }

  public static  boolean instanceOf(SearchContext driver, Class klass) {
    return cast(driver, klass).isPresent();
  }

  public static  Optional cast(Driver driver, Class klass) {
    return cast(driver.getWebDriver(), klass);
  }

  public static  Optional cast(SearchContext driverOrElement, Class klass) {
    SearchContext unwrappedWebdriver = driverOrElement;
    while (unwrappedWebdriver instanceof WrapsDriver wrapper) {
      unwrappedWebdriver = wrapper.getWrappedDriver();
    }

    //noinspection unchecked
    return klass.isAssignableFrom(unwrappedWebdriver.getClass()) ?
      Optional.of((T) unwrappedWebdriver) :
      Optional.empty();
  }

  public static  T cast(WebElement probablyWrappedWebElement, Class klass) {
    WebElement unwrappedWebElement = probablyWrappedWebElement;
    while (unwrappedWebElement instanceof WrapsElement wrapper) {
      unwrappedWebElement = wrapper.getWrappedElement();
    }

    if (!klass.isAssignableFrom(unwrappedWebElement.getClass())) {
      throw new IllegalArgumentException("WebElement " + unwrappedWebElement + " is not instance of " + klass.getName());
    }
    //noinspection unchecked
    return (T) unwrappedWebElement;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy