com.clarolab.selenium.pages.browser.mobile.AndroidMobileBrowser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pages-framework Show documentation
Show all versions of pages-framework Show documentation
Framework for automated testing using Selenium. Provides easy configuration of WebDrivers with BrowserFactory. Provides a Page abstraction.
The newest version!
package com.clarolab.selenium.pages.browser.mobile;
import com.clarolab.selenium.pages.actions.AndroidSeleniumActions;
import com.clarolab.selenium.pages.config.TimeoutsConfig;
import com.clarolab.selenium.pages.exception.FrameworkWebDriverException;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidKeyCode;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URL;
public class AndroidMobileBrowser extends MobileBrowser {
protected boolean touchMode;
private String appPackage;
private String appActivity;
private static final Logger logger = LoggerFactory.getLogger(AndroidMobileBrowser.class);
public AndroidMobileBrowser(String baseTestUrl,
String browserName,
String platform,
String platformName,
String platformVersion,
String deviceName,
String newCommandTimeout,
String automationName,
String version,
String autoLaunch,
String app,
String appPackage,
String appActivity,
TimeoutsConfig timeouts,
boolean touchMode,
boolean fullReset) throws FrameworkWebDriverException {
super(baseTestUrl, timeouts, browserName, platform, platformName, platformVersion, deviceName,
newCommandTimeout, automationName, version, autoLaunch, app, fullReset);
this.touchMode = touchMode;
this.appPackage = appPackage;
this.appActivity = appActivity;
}
@Override
public DesiredCapabilities getDesiredCapabilities() {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(CapabilityType.BROWSER_NAME, browserName);
desiredCapabilities.setCapability("platform", platform);
desiredCapabilities.setCapability("platformName", platformName);
desiredCapabilities.setCapability("platformVersion", platformVersion);
desiredCapabilities.setCapability("deviceName", deviceName);
desiredCapabilities.setCapability("newCommandTimeout", newCommandTimeout);
desiredCapabilities.setCapability("automationName", automationName);
desiredCapabilities.setCapability("version", version);
desiredCapabilities.setCapability("autoLaunch", autoLaunch);
desiredCapabilities.setCapability("app", app);
desiredCapabilities.setCapability("appPackage", appPackage);
desiredCapabilities.setCapability("appWaitActivity", appActivity);
desiredCapabilities.setCapability("fullReset", fullReset);
desiredCapabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
return desiredCapabilities;
}
protected AndroidDriver createWebDriver() throws FrameworkWebDriverException {
try {
printCapabilities(getDesiredCapabilities());
return new SwipeableWebDriver(new URL(getBaseTestUrl()), getDesiredCapabilities());
} catch (IOException e) {
throw new FrameworkWebDriverException("Error starting appium driver service", e);
}
}
@Override
public AndroidSeleniumActions getActions() {
return new AndroidSeleniumActions(this);
}
public String getAppPackage() {
return appPackage;
}
public String getAppActivity() {
return appActivity;
}
/**
*
* @return true if Android is API 17 or down, and as a result uses touch mode
*/
public boolean isTouchMode() {
return touchMode;
}
/**
*
* @param touchMode - true if Android is API 17 or lower
*/
public void setTouchMode(boolean touchMode) {
this.touchMode = touchMode;
}
/**
* Swipe from the top to bottom for a second
*/
@Override
public void dragDown() {
int midScreen = getScreenWidth() / 2;
if (touchMode) {
TouchActions action = new TouchActions(webDriver);
action.down(midScreen, 360).move(midScreen, 300).up(midScreen, 300).perform();
}
}
/**
* Swipe from the down to up for a second
*/
@Override
public void dragUp() {
int midScreen = webDriver.manage().window().getSize().getWidth() / 2;
if (touchMode) {
TouchActions action = new TouchActions(webDriver);
action.down(midScreen, 300).move(midScreen, 250).up(midScreen, 250).perform();
}
}
/**
* Swipe from the top to bottom for a second
*
* @param yStart - coordinate to start swiping
* @param yEnd - coordinate to stop swiping
*/
@Override
public void drag(int yStart, int yEnd) {
int midScreen = getScreenWidth() / 2;
TouchActions action = new TouchActions(webDriver);
if (touchMode) {
action.down(midScreen, yStart).move(midScreen, yEnd).up(midScreen, yEnd).perform();
}
}
/**
* Swipe from the top to bottom for a second
*
* @param yStart - coordinate to start swiping
* @param yEnd - coordinate to stop swiping
*/
@Override
public void drag(int yStart, int yEnd, int duration) {
int midScreen = getScreenWidth() / 2;
if (touchMode) {
TouchActions action = new TouchActions(webDriver);
action.down(midScreen, yStart).move(midScreen, yEnd).up(midScreen, yEnd).perform();
}
}
@Override
public void tap(int fingersNum, WebElement webElement, int duration) {
if (touchMode) {
TouchActions action = new TouchActions(webDriver);
try {
action.down(webElement.getLocation().getX(), webElement.getLocation().getY()).clickAndHold()
.release(webElement).perform();
} catch (NullPointerException e) {
}
} else {
TouchActions action = new TouchActions(webDriver);
action.singleTap(webElement);
}
}
@Override
public void tap(int fingersNum, int xLocation, int yLocation, int duration) {
if (touchMode) {
TouchActions action = new TouchActions(webDriver);
try {
action.down(xLocation, yLocation).clickAndHold().perform();
} catch (NullPointerException e) {
logger.error("Failed To Tap due to NullPointerException", e.getStackTrace());
}
}
}
public void clickHomePage() {
((AndroidDriver)webDriver).pressKey(new KeyEvent(AndroidKey.HOME));
}
public void clickBack() {
((AndroidDriver)webDriver).pressKey(new KeyEvent(AndroidKey.BACK));
}
@Override
public void scrollToTop() {
logger.error("Method ScrollToTop is not yet implemented");
}
}