Skip to content

Commit

Permalink
Add actions lockDevice, unlockDevice and rotateDevice for iOS and And…
Browse files Browse the repository at this point in the history
…roid application
  • Loading branch information
bcivel committed Aug 4, 2024
1 parent 1ee4663 commit ad173c5
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public class TestCaseStepAction {
public static final String ACTION_SETCONTENT = "setContent";
public static final String ACTION_SETSERVICECALLCONTENT = "setServiceCallContent";
public static final String ACTION_SWITCHTOCONTEXT = "switchToContext";
public static final String ACTION_LOCKDEVICE = "lockDevice";
public static final String ACTION_UNLOCKDEVICE = "unlockDevice";
public static final String ACTION_ROTATEDEVICE = "rotateDevice";
public static final String ACTION_DONOTHING = "doNothing";

// ??? TODO. Clean this unused action.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,15 @@ public TestCaseStepActionExecution doAction(TestCaseStepActionExecution actionEx
case TestCaseStepAction.ACTION_GETROBOTFILE:
res = this.doActionGetRobotFile(execution, actionExecution, value1, value2, value3);
break;
case TestCaseStepAction.ACTION_LOCKDEVICE:
res = this.doActionLockDevice(execution);
break;
case TestCaseStepAction.ACTION_UNLOCKDEVICE:
res = this.doActionUnlockDevice(execution);
break;
case TestCaseStepAction.ACTION_ROTATEDEVICE:
res = this.doActionRotateDevice(execution);
break;
case TestCaseStepAction.ACTION_DONOTHING:
res = new MessageEvent(MessageEventEnum.ACTION_SUCCESS);
break;
Expand Down Expand Up @@ -2286,6 +2295,70 @@ private MessageEvent doActionHideKeyboard(TestCaseExecution tCExecution) {
.resolveDescription("APPLICATIONTYPE", tCExecution.getApplicationObj().getType());
}

private MessageEvent doActionLockDevice(TestCaseExecution tCExecution) {
// Check argument
if (tCExecution == null) {
return new MessageEvent(MessageEventEnum.ACTION_FAILED_TYPE);
}

// Hide keyboard according to application type
String applicationType = tCExecution.getApplicationObj().getType();
if (Application.TYPE_APK.equals(applicationType)) {
return androidAppiumService.lockDevice(tCExecution.getSession());
}
if (Application.TYPE_IPA.equals(applicationType)) {
return iosAppiumService.lockDevice(tCExecution.getSession());
}

// Else we are faced with a non supported application
return new MessageEvent(MessageEventEnum.ACTION_NOTEXECUTED_NOTSUPPORTED_FOR_APPLICATION)
.resolveDescription("ACTION", "lockDevice")
.resolveDescription("APPLICATIONTYPE", tCExecution.getApplicationObj().getType());
}

private MessageEvent doActionUnlockDevice(TestCaseExecution tCExecution) {
// Check argument
if (tCExecution == null) {
return new MessageEvent(MessageEventEnum.ACTION_FAILED_TYPE);
}

// Hide keyboard according to application type
String applicationType = tCExecution.getApplicationObj().getType();
if (Application.TYPE_APK.equals(applicationType)) {
return androidAppiumService.unlockDevice(tCExecution.getSession());
}
if (Application.TYPE_IPA.equals(applicationType)) {
return iosAppiumService.unlockDevice(tCExecution.getSession());
}

// Else we are faced with a non supported application
return new MessageEvent(MessageEventEnum.ACTION_NOTEXECUTED_NOTSUPPORTED_FOR_APPLICATION)
.resolveDescription("ACTION", "unlockDevice")
.resolveDescription("APPLICATIONTYPE", tCExecution.getApplicationObj().getType());
}

private MessageEvent doActionRotateDevice(TestCaseExecution tCExecution) {
// Check argument
if (tCExecution == null) {
return new MessageEvent(MessageEventEnum.ACTION_FAILED_TYPE);
}

// Hide keyboard according to application type
String applicationType = tCExecution.getApplicationObj().getType();
if (Application.TYPE_APK.equals(applicationType)) {
return androidAppiumService.rotateDevice(tCExecution.getSession());
}
if (Application.TYPE_IPA.equals(applicationType)) {
return iosAppiumService.rotateDevice(tCExecution.getSession());
}

// Else we are faced with a non supported application
return new MessageEvent(MessageEventEnum.ACTION_NOTEXECUTED_NOTSUPPORTED_FOR_APPLICATION)
.resolveDescription("ACTION", "rotateDevice")
.resolveDescription("APPLICATIONTYPE", tCExecution.getApplicationObj().getType());
}


private MessageEvent doActionSwipe(TestCaseExecution tCExecution, String object, String property) {
// Check arguments
if (tCExecution == null || object == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ public enum MessageEventEnum {
ACTION_SUCCESS_OPENAPP(200, "OK", "Application '%APP%' opened.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_CLOSEAPP(200, "OK", "Application '%APP%' closed.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_CLOSEAPP_GENERIC(200, "OK", "Application closed.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_UNLOCKDEVICE_GENERIC(200, "OK", "Device unlocked.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_LOCKDEVICE_GENERIC(200, "OK", "Device locked.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_ROTATEDEVICE_GENERIC(200, "OK", "Device rotated.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_CLICK(200, "OK", "Element '%ELEMENT%' clicked.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_CLICKANDWAIT(200, "OK", "Element '%ELEMENT%' clicked and waited %TIME% ms.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_CLICKANDNOWAIT(200, "OK", "Element '%ELEMENT%' clicked and waited for page to load", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,22 @@ public interface IAppiumService {
* @return
*/
MessageEvent clearField(Session session, Identifier identifier);

/**
* @param session
* @return
*/
MessageEvent lockDevice(Session session);

/**
* @param session
* @return
*/
MessageEvent unlockDevice(Session session);

/**
* @param session
* @return
*/
MessageEvent rotateDevice(Session session);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.cerberus.core.util.JSONUtil;
import org.cerberus.core.util.StringUtil;
import org.json.JSONException;
import org.openqa.selenium.ScreenOrientation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -252,4 +253,49 @@ public MessageEvent closeApp(Session session) {
}
}

@Override
public MessageEvent lockDevice(Session session) {
try {
AndroidDriver driver = ((AndroidDriver) session.getAppiumDriver());
driver.lockDevice();

return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_LOCKDEVICE_GENERIC);

} catch (Exception e) {
LOG.warn("Unable to close app " + e.getMessage(), e);
return new MessageEvent(MessageEventEnum.ACTION_FAILED_GENERIC)
.resolveDescription("DETAIL", "Unable to close app : " + e.getMessage());
}
}

@Override
public MessageEvent unlockDevice(Session session) {
try {
AndroidDriver driver = ((AndroidDriver) session.getAppiumDriver());
driver.unlockDevice();

return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_UNLOCKDEVICE_GENERIC);

} catch (Exception e) {
LOG.warn("Unable to close app " + e.getMessage(), e);
return new MessageEvent(MessageEventEnum.ACTION_FAILED_GENERIC)
.resolveDescription("DETAIL", "Unable to close app : " + e.getMessage());
}
}

@Override
public MessageEvent rotateDevice(Session session) {
try {
AndroidDriver driver = ((AndroidDriver) session.getAppiumDriver());
driver.rotate(ScreenOrientation.LANDSCAPE == driver.getOrientation() ? ScreenOrientation.PORTRAIT : ScreenOrientation.LANDSCAPE);

return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_UNLOCKDEVICE_GENERIC);

} catch (Exception e) {
LOG.warn("Unable to close app " + e.getMessage(), e);
return new MessageEvent(MessageEventEnum.ACTION_FAILED_GENERIC)
.resolveDescription("DETAIL", "Unable to close app : " + e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.cerberus.core.service.appium.impl;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.apache.logging.log4j.LogManager;
Expand All @@ -34,6 +36,7 @@
import org.cerberus.core.util.StringUtil;
import org.json.JSONException;
import org.openqa.selenium.Keys;
import org.openqa.selenium.ScreenOrientation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -264,4 +267,50 @@ public MessageEvent closeApp(Session session) {
}
}

@Override
public MessageEvent lockDevice(Session session) {
try {
IOSDriver driver = ((IOSDriver) session.getAppiumDriver());
driver.lockDevice();

return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_LOCKDEVICE_GENERIC);

} catch (Exception e) {
LOG.warn("Unable to close app " + e.getMessage(), e);
return new MessageEvent(MessageEventEnum.ACTION_FAILED_GENERIC)
.resolveDescription("DETAIL", "Unable to close app : " + e.getMessage());
}
}

@Override
public MessageEvent unlockDevice(Session session) {
try {
IOSDriver driver = ((IOSDriver) session.getAppiumDriver());
driver.unlockDevice();

return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_UNLOCKDEVICE_GENERIC);

} catch (Exception e) {
LOG.warn("Unable to close app " + e.getMessage(), e);
return new MessageEvent(MessageEventEnum.ACTION_FAILED_GENERIC)
.resolveDescription("DETAIL", "Unable to close app : " + e.getMessage());
}
}

@Override
public MessageEvent rotateDevice(Session session) {
try {
IOSDriver driver = ((IOSDriver) session.getAppiumDriver());
LOG.warn("orientation : " + driver.getOrientation().value());
driver.rotate(ScreenOrientation.LANDSCAPE == driver.getOrientation() ? ScreenOrientation.PORTRAIT : ScreenOrientation.LANDSCAPE);

return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_UNLOCKDEVICE_GENERIC);

} catch (Exception e) {
LOG.warn("Unable to close app " + e.getMessage(), e);
return new MessageEvent(MessageEventEnum.ACTION_FAILED_GENERIC)
.resolveDescription("DETAIL", "Unable to close app : " + e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,30 @@ Force current content of the test case to the console logs in a JSON format. Tha
|===
Force current content of the test case to the data provided in value1 parameter. It can be any format you need (JSON, XML). All controls attached to that action will point to that data. Once all controls of that action are finished, current content are set back to normal value (could be an html or app page or another service).

==== lockDevice
|===

| *[green]#APK#* | *[green]#IPA#*

|===
Lock the device during the execution

==== unlockDevice
|===

| *[green]#APK#* | *[green]#IPA#*

|===
Unlock the locked device during the execution. For APK, capabilities unlockType et unlockKey can be used to unlock devices protected by a key.

==== rotateDevice
|===

| *[green]#APK#* | *[green]#IPA#*

|===
Rotate the screen 90 degrees.

==== doNothing
|===

Expand Down
4 changes: 4 additions & 0 deletions source/src/main/webapp/js/testcase/action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ad173c5

Please sign in to comment.