com.codeborne.selenide.appium.WebdriverUnwrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenide-appium Show documentation
Show all versions of selenide-appium Show documentation
Selenide = concise API for Selenium WebDriver
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;
}
}