android.AndroidActions Maven / Gradle / Ivy
The newest version!
package android;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.LongPressOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.interactions.touch.TouchActions;
import java.time.Duration;
import static android.AndroidDriverInitializer.androidDriver;
import static android.AndroidElementHandler.waitUntil;
import static android.TakeScreenShot.captureScreenshotOnStep;
import static core.internal.EnvironmentInterface.SLEEP_TIMEOUT;
import static core.internal.EnvironmentInterface.TIME_DURATION;
import static core.reports.TestReporter.error;
import static core.reports.TestReporter.fail;
import static core.reports.TestReporter.pass;
import static io.appium.java_client.touch.offset.ElementOption.element;
/**
* Created by Ismail on 1/5/2018.
* This Class contains all related methods and their implementation
* for Android application actions
*/
public class AndroidActions {
/*************** Class Variables Section ***************/
// This variable to save error message if action fail to execute
private static String errMessage = null;
/*************** Class Methods Section ***************/
// This method to log step if passed or failed on test report
private static void reportStepStatus(String passMessage, String errMessage, String screenShot, Boolean failTestCase) {
if (errMessage != null) {
// Save error message step
String errReport = "Something went wrong, Please check message: " + errMessage;
// Report step log to reporter
fail(errReport, failTestCase);
} else
pass(passMessage, screenShot);
}
// This method to do Pull to Refresh action
public static void pullToRefresh() {
for (int attempts = 0; attempts < 3; attempts++) {
try {
// Apply the action
(new TouchAction(androidDriver)).press(new PointOption().withCoordinates(170, 400)).moveTo(new PointOption().withCoordinates(170, 980)).release().perform();
// If success then no errors
errMessage = null;
// If success leave for loop
break;
} catch (Throwable throwable) {
// If fail parse throw Get Throw exception and save to errMessage
errMessage = throwable.getMessage();
// Wait a time then try again
try {
Thread.sleep(SLEEP_TIMEOUT);
} catch (InterruptedException e) {
}
}
}
// Report step log to reporter
reportStepStatus("Screen has been refreshed.", errMessage, captureScreenshotOnStep(), true);
}
// This method implements tab action
public static void tap(MobileElement mobileElement) {
try {
waitUntil(mobileElement, true).click();
} catch (Throwable throwable) {
error("Unable to click the element.", throwable, true);
}
// for (int attempts = 0; attempts < 3; attempts++) {
// try {
// Apply the action
// androidElement.click();
// If success then no errors
// errMessage = null;
// If success leave for loop
// break;
// } catch (Throwable throwable) {
// If fail parse throw Get Throw exception and save to errMessage
// errMessage = throwable.getMessage();
// Wait a time then try again
// try {
// Thread.sleep(SLEEP_TIMEOUT);
// } catch (InterruptedException e) {
// }
// }
// }
// Report step log to reporter
// reportStepStatus("Element has been tabbed.",errMessage, captureScreenshotOnStep(), true);
}
public static void fillField(MobileElement mobileElement, String value) {
try {
waitUntil(mobileElement, false).sendKeys(value);
} catch (Throwable throwable) {
error("Unable to type inside the element.", throwable, true);
}
}
// This method swipes the screen to left
public static void swipeLeft() {
new TouchAction<>(androidDriver).longPress(PointOption.point(250, 1200)).moveTo(PointOption.point(900, 1200)).release().perform();
}
// This method swipes the screen to right
public static void swipeRight() {
new TouchAction<>(androidDriver).longPress(PointOption.point(1000, 450)).moveTo(PointOption.point(500, 450)).release().perform();
}
// This method apply long press action to element
public static void longPress(MobileElement mobileElement) {
try {
MobileElement element = waitUntil(mobileElement, true);
TouchAction action = new TouchAction(androidDriver);
action.longPress(LongPressOptions.longPressOptions().withElement(element(element))).release().perform();
} catch (Throwable throwable) {
error("Unable to apply long press on element.", throwable, true);
}
}
// This method apply long press action to element
public static void longPress(MobileElement mobileElement, int duration) {
try {
MobileElement element = waitUntil(mobileElement, true);
TouchAction action = new TouchAction(androidDriver);
action.longPress(LongPressOptions.longPressOptions().withElement(element(element)).withDuration(Duration.ofSeconds(duration))).release().perform();
} catch (Throwable throwable) {
error("Unable to apply long press on element.", throwable, true);
}
}
}