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

android.AndroidActions Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package android;

import core.reports.TestReporter;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.touch.offset.PointOption;

import static android.AndroidDriverInitializer.androidDriver;
import static android.TakeScreenShot.captureScreenshotOnStep;
import static core.internal.EnvironmentInterface.SLEEP_TIMEOUT;

/**
 * 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
            TestReporter.fail(errReport, failTestCase);
        } else
            TestReporter.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 tab(AndroidElement androidElement) {
        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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy