Skip to content

Commit

Permalink
Merge pull request #13 from aquality-automation/feature/Swipe-Actions
Browse files Browse the repository at this point in the history
Feature/swipe actions
  • Loading branch information
n-verbitsky authored Jun 4, 2020
2 parents 9f5dba2 + 0f90061 commit 37fa04a
Show file tree
Hide file tree
Showing 26 changed files with 546 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.aquality-automation</groupId>
<artifactId>aquality-appium-mobile</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>

<packaging>jar</packaging>
<name>Aquality Appium Mobile</name>
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/aquality/appium/mobile/actions/IActionsModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package aquality.appium.mobile.actions;

import aquality.appium.mobile.configuration.IConfigurationsModule;

/**
* Describes implementations of actions services to be registered in DI container.
*/
public interface IActionsModule extends IConfigurationsModule {

/**
* @return class which implements {@link ITouchActions}
*/
default Class<? extends ITouchActions> getTouchActionsImplementation() {
return TouchActions.class;
}
}
25 changes: 25 additions & 0 deletions src/main/java/aquality/appium/mobile/actions/ITouchActions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package aquality.appium.mobile.actions;

import org.openqa.selenium.Point;

/**
* Describes general Touch Actions.
*/
public interface ITouchActions {

/**
* Swipes from start point to end point using TouchAction.
*
* @param startPoint point on screen to swipe from.
* @param endPoint point on screen to swipe to.
*/
void swipe(Point startPoint, Point endPoint);

/**
* Performs long press action and moves cursor from a start point to an end point imitating the swipe action.
*
* @param startPoint point on screen to swipe from.
* @param endPoint point on screen to swipe to.
*/
void swipeWithLongPress(Point startPoint, Point endPoint);
}
11 changes: 11 additions & 0 deletions src/main/java/aquality/appium/mobile/actions/SwipeDirection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package aquality.appium.mobile.actions;

/**
* The enum describing the possible swipe directions.
*/
public enum SwipeDirection {
UP,
DOWN,
LEFT,
RIGHT;
}
44 changes: 44 additions & 0 deletions src/main/java/aquality/appium/mobile/actions/TouchActions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package aquality.appium.mobile.actions;

import aquality.appium.mobile.application.AqualityServices;
import aquality.appium.mobile.configuration.ITouchActionsConfiguration;
import aquality.selenium.core.utilities.IElementActionRetrier;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.Point;
import java.util.function.UnaryOperator;
import static io.appium.java_client.touch.WaitOptions.waitOptions;

public class TouchActions implements ITouchActions {

@Override
public void swipe(Point startPoint, Point endPoint) {
AqualityServices.getLocalizedLogger().info(
"loc.action.swipe",
startPoint.getX(),
startPoint.getY(),
endPoint.getX(),
endPoint.getY());
performTouchAction(touchAction -> touchAction
.press(PointOption.point(startPoint))
.waitAction(waitOptions(AqualityServices.get(ITouchActionsConfiguration.class).getSwipeDuration())),
endPoint);
}

@Override
public void swipeWithLongPress(Point startPoint, Point endPoint) {
AqualityServices.getLocalizedLogger().info(
"loc.action.swipeLongPress",
startPoint.getX(),
startPoint.getY(),
endPoint.getX(),
endPoint.getY());
performTouchAction(touchAction -> touchAction.longPress(PointOption.point(startPoint)), endPoint);
}

protected void performTouchAction(UnaryOperator<TouchAction<?>> function, Point endPoint) {
TouchAction<?> touchAction = new TouchAction<>(AqualityServices.getApplication().getDriver());
AqualityServices.get(IElementActionRetrier.class).doWithRetry(() ->
function.apply(touchAction).moveTo(PointOption.point(endPoint)).release().perform());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package aquality.appium.mobile.application;

import aquality.appium.mobile.actions.ITouchActions;
import aquality.appium.mobile.configuration.IApplicationProfile;
import aquality.appium.mobile.configuration.IConfiguration;
import aquality.appium.mobile.configuration.ILocalServiceSettings;
Expand Down Expand Up @@ -190,4 +191,13 @@ public static ILocalServiceSettings getLocalServiceSettings() {
public static IConfiguration getConfiguration() {
return get(IConfiguration.class);
}

/**
* Gets the the utility used to perform touch actions.
*
* @return instance of touch actions.
*/
public static ITouchActions getTouchActions() {
return get(ITouchActions.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package aquality.appium.mobile.application;

import aquality.appium.mobile.actions.IActionsModule;
import aquality.appium.mobile.actions.ITouchActions;
import aquality.appium.mobile.configuration.IApplicationProfile;
import aquality.appium.mobile.configuration.IConfiguration;
import aquality.appium.mobile.configuration.IConfigurationsModule;
import aquality.appium.mobile.configuration.ILocalServiceSettings;
import aquality.appium.mobile.configuration.ITouchActionsConfiguration;
import aquality.appium.mobile.elements.IElementsModule;
import aquality.appium.mobile.elements.interfaces.IElementFactory;
import aquality.appium.mobile.screens.screenfactory.IScreenFactory;
Expand All @@ -12,7 +15,7 @@
import com.google.inject.Provider;
import com.google.inject.Singleton;

public class MobileModule extends AqualityModule<Application> implements IConfigurationsModule, IElementsModule, IScreensModule {
public class MobileModule extends AqualityModule<Application> implements IConfigurationsModule, IElementsModule, IScreensModule, IActionsModule {

public MobileModule(Provider<Application> applicationProvider) {
super(applicationProvider);
Expand All @@ -26,5 +29,7 @@ protected void configure() {
bind(IConfiguration.class).to(getConfigurationImplementation());
bind(IElementFactory.class).to(getElementFactoryImplementation());
bind(IScreenFactory.class).to(getScreenFactoryImplementation());
bind(ITouchActionsConfiguration.class).to(getTouchActionsConfigurationImplementation());
bind(ITouchActions.class).to(getTouchActionsImplementation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ public class Configuration implements IConfiguration {
private final IApplicationProfile applicationProfile;
private final ILoggerConfiguration loggerConfiguration;
private final IElementCacheConfiguration elementCacheConfiguration;
private final ITouchActionsConfiguration touchActionsConfiguration;

@Inject
public Configuration(ITimeoutConfiguration timeoutConfiguration, IRetryConfiguration retryConfiguration,
IApplicationProfile applicationProfile, ILoggerConfiguration loggerConfiguration,
IElementCacheConfiguration elementCacheConfiguration) {
IElementCacheConfiguration elementCacheConfiguration,
ITouchActionsConfiguration touchActionsConfiguration) {
this.timeoutConfiguration = timeoutConfiguration;
this.retryConfiguration = retryConfiguration;
this.applicationProfile = applicationProfile;
this.loggerConfiguration = loggerConfiguration;
this.elementCacheConfiguration = elementCacheConfiguration;
this.touchActionsConfiguration = touchActionsConfiguration;
}

@Override
Expand All @@ -49,4 +52,9 @@ public ILoggerConfiguration getLoggerConfiguration() {
public IElementCacheConfiguration getElementCacheConfiguration() {
return elementCacheConfiguration;
}

@Override
public ITouchActionsConfiguration getTouchActionsConfiguration() {
return touchActionsConfiguration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ public interface IConfiguration {
* @return Configuration of element caching.
*/
IElementCacheConfiguration getElementCacheConfiguration();

/**
* Gets configuration of touch actions.
*
* @return Configuration of touch actions.
*/
ITouchActionsConfiguration getTouchActionsConfiguration();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ default Class<? extends IConfiguration> getConfigurationImplementation() {
return Configuration.class;
}

/**
* @return class which implements {@link ITouchActionsConfiguration}
*/
default Class<? extends ITouchActionsConfiguration> getTouchActionsConfigurationImplementation() {
return TouchActionsConfiguration.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package aquality.appium.mobile.configuration;

import java.time.Duration;

/**
* Describes Touch Actions settings.
*/
public interface ITouchActionsConfiguration {
/**
* Gets number of retries to perform swipe.
*
* @return arguments map.
*/
int getSwipeRetries();

/**
* Gets the number of seconds required to perform a swipe action.
*
* @return number of seconds required to perform a swipe action.
*/
Duration getSwipeDuration();

/**
* Gets the offset coefficient to adjust the start/end point for swipe action relatively to the parallel screen edge.
* Example for swipe down action:
* The offset coefficient is used to calculate start/end point's Y coordinate.
* If offset coefficient == 0.2, then the start point's Y coordinate == screen's length * (1 - 0.2).
* If offset coefficient == 0.2, then the end point's Y coordinate == screen's length * 0.2.
* The vice versa for swipe up action.
* Example for swipe left action:
* The offset coefficient is used to calculate start/end point's X coordinate.
* If offset coefficient == 0.2, then the start point's X coordinate == screen's width * (1 - 0.2).
* If offset coefficient == 0.2, then the end point's X coordinate == screen's width * 0.2.
* The vice versa for swipe right action.
*
* @return offset coefficient to adjust the start/end point for swipe action relatively to the parallel screen edge
*/
double getSwipeVerticalOffset();

/**
* Gets the offset coefficient to adjust the start/end point for swipe action relatively to the perpendicular screen edge.
* Example for swipe down action:
* The offset coefficient is used to calculate start/end point's X coordinate.
* If offset coefficient == 0.5, then the start point's X coordinate == screen's width * (1 - 0.5).
* If offset coefficient == 0.5, then the end point's X coordinate == screen's width * 0.5.
* The vice versa for swipe up action.
* Example for swipe left action:
* The offset coefficient is used to calculate start/end point's X coordinate.
* If offset coefficient == 0.5, then the start point's Y coordinate == screen's length * (1 - 0.5).
* If offset coefficient == 0.5, then the end point's Y coordinate == screen's length * 0.5.
* The vice versa for swipe right action.
*
* @return offset coefficient to adjust the start/end point for swipe action relatively to the perpendicular screen edge
*/
double getSwipeHorizontalOffset();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package aquality.appium.mobile.configuration;

import aquality.selenium.core.utilities.ISettingsFile;
import com.google.inject.Inject;

import java.time.Duration;

public class TouchActionsConfiguration implements ITouchActionsConfiguration {
private final Duration swipeDuration;
private final int swipeRetries;
private final double swipeVerticalOffset;
private final double swipeHorizontalOffset;

@Inject
public TouchActionsConfiguration(ISettingsFile settingsFile) {
this.swipeDuration = Duration.ofSeconds(Long.parseLong(settingsFile.getValue("/touchActions/swipe/duration").toString()));
this.swipeRetries = (int) settingsFile.getValue("/touchActions/swipe/retries");
this.swipeVerticalOffset = (double) settingsFile.getValue("/touchActions/swipe/verticalOffset");
this.swipeHorizontalOffset = (double) settingsFile.getValue("/touchActions/swipe/horizontalOffset");
}

@Override
public int getSwipeRetries() {
return this.swipeRetries;
}

@Override
public Duration getSwipeDuration() {
return this.swipeDuration;
}

@Override
public double getSwipeVerticalOffset() {
return this.swipeVerticalOffset;
}

@Override
public double getSwipeHorizontalOffset() {
return this.swipeHorizontalOffset;
}
}
7 changes: 7 additions & 0 deletions src/main/java/aquality/appium/mobile/elements/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import aquality.appium.mobile.application.Application;
import aquality.appium.mobile.application.AqualityServices;
import aquality.appium.mobile.elements.actions.ElementTouchActions;
import aquality.appium.mobile.elements.actions.IElementTouchActions;
import aquality.appium.mobile.elements.interfaces.IElement;
import aquality.appium.mobile.elements.interfaces.IElementFactory;
import aquality.selenium.core.configurations.IElementCacheConfiguration;
Expand Down Expand Up @@ -86,4 +88,9 @@ public void sendKeys(Keys key) {
logElementAction("loc.text.sending.keys", Keys.class.getSimpleName().concat(".").concat(key.name()));
doWithRetry(() -> getElement().sendKeys(key));
}

@Override
public IElementTouchActions getTouchActions() {
return new ElementTouchActions(this);
}
}
Loading

0 comments on commit 37fa04a

Please sign in to comment.