Skip to content

Commit bcd6881

Browse files
feat: allow to add custom command dynamically (#1506)
1 parent 3a3e2f9 commit bcd6881

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

src/main/java/io/appium/java_client/AppiumDriver.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import io.appium.java_client.remote.MobileCapabilityType;
3030
import io.appium.java_client.service.local.AppiumDriverLocalService;
3131
import io.appium.java_client.service.local.AppiumServiceBuilder;
32-
3332
import org.openqa.selenium.By;
3433
import org.openqa.selenium.Capabilities;
3534
import org.openqa.selenium.DeviceRotation;
@@ -39,7 +38,6 @@
3938
import org.openqa.selenium.WebDriverException;
4039
import org.openqa.selenium.WebElement;
4140
import org.openqa.selenium.html5.Location;
42-
4341
import org.openqa.selenium.remote.CapabilityType;
4442
import org.openqa.selenium.remote.DesiredCapabilities;
4543
import org.openqa.selenium.remote.DriverCommand;
@@ -49,8 +47,10 @@
4947
import org.openqa.selenium.remote.Response;
5048
import org.openqa.selenium.remote.html5.RemoteLocationContext;
5149
import org.openqa.selenium.remote.http.HttpClient;
50+
import org.openqa.selenium.remote.http.HttpMethod;
5251

5352
import java.net.URL;
53+
import java.util.Arrays;
5454
import java.util.LinkedHashSet;
5555
import java.util.List;
5656
import java.util.Map;
@@ -288,6 +288,31 @@ public void rotate(ScreenOrientation orientation) {
288288
ImmutableMap.of("orientation", orientation.value().toUpperCase()));
289289
}
290290

291+
/**
292+
* This method is used to add custom appium commands in Appium 2.0.
293+
*
294+
* @param httpMethod the available {@link HttpMethod}.
295+
* @param url The url to URL template as https://www.w3.org/TR/webdriver/#endpoints.
296+
* @param methodName The name of custom appium command.
297+
*/
298+
public void addCommand(HttpMethod httpMethod, String url, String methodName) {
299+
switch (httpMethod) {
300+
case GET:
301+
MobileCommand.commandRepository.put(methodName, MobileCommand.getC(url));
302+
break;
303+
case POST:
304+
MobileCommand.commandRepository.put(methodName, MobileCommand.postC(url));
305+
break;
306+
case DELETE:
307+
MobileCommand.commandRepository.put(methodName, MobileCommand.deleteC(url));
308+
break;
309+
default:
310+
throw new WebDriverException(String.format("Unsupported HTTP Method: %s. Only %s methods are supported",
311+
httpMethod,
312+
Arrays.toString(HttpMethod.values())));
313+
}
314+
}
315+
291316
@Override
292317
public ScreenOrientation getOrientation() {
293318
Response response = execute(DriverCommand.GET_SCREEN_ORIENTATION);

src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ public Response execute(Command command) throws WebDriverException {
241241
}
242242
});
243243
}
244+
if (getAdditionalCommands().containsKey(command.getName())) {
245+
super.defineCommand(command.getName(), getAdditionalCommands().get(command.getName()));
246+
}
244247

245248
Response response;
246249
try {

src/test/java/io/appium/java_client/ios/AppIOSTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ public static void beforeClass() throws Exception {
3939
driver = new IOSDriver<>(new URL("http://" + ip + ":" + PORT + "/wd/hub"), capabilities);
4040
}
4141
}
42-
}
42+
}

src/test/java/io/appium/java_client/ios/BaseIOSTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public class BaseIOSTest {
3131
protected static IOSDriver<IOSElement> driver;
3232
protected static final int PORT = 4723;
3333
public static final String DEVICE_NAME = System.getenv("IOS_DEVICE_NAME") != null
34-
? System.getenv("IOS_DEVICE_NAME") : "iPhone X";
34+
? System.getenv("IOS_DEVICE_NAME") : "iPhone 12";
3535
public static final String PLATFORM_VERSION = System.getenv("IOS_PLATFORM_VERSION") != null
36-
? System.getenv("IOS_PLATFORM_VERSION") : "11.4";
36+
? System.getenv("IOS_PLATFORM_VERSION") : "14.5";
3737

3838

3939
/**

src/test/java/io/appium/java_client/ios/IOSDriverTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,52 @@
2222
import static org.hamcrest.Matchers.greaterThan;
2323
import static org.junit.Assert.assertEquals;
2424
import static org.junit.Assert.assertFalse;
25+
import static org.junit.Assert.assertNotNull;
2526
import static org.junit.Assert.assertTrue;
2627
import static org.junit.Assert.fail;
2728

29+
import com.google.common.collect.ImmutableMap;
2830
import io.appium.java_client.MobileElement;
2931
import io.appium.java_client.appmanagement.ApplicationState;
3032
import io.appium.java_client.remote.HideKeyboardStrategy;
3133
import org.junit.Ignore;
3234
import org.junit.Test;
33-
3435
import org.openqa.selenium.By;
3536
import org.openqa.selenium.ScreenOrientation;
3637
import org.openqa.selenium.html5.Location;
38+
import org.openqa.selenium.remote.Response;
39+
import org.openqa.selenium.remote.http.HttpMethod;
3740
import org.openqa.selenium.support.ui.ExpectedConditions;
3841
import org.openqa.selenium.support.ui.WebDriverWait;
3942

4043
import java.time.Duration;
4144

4245
public class IOSDriverTest extends AppIOSTest {
4346

47+
@Test
48+
public void addCustomCommandTest() {
49+
driver.addCommand(HttpMethod.GET, "/sessions", "getSessions");
50+
final Response getSessions = driver.execute("getSessions");
51+
assertNotNull(getSessions.getSessionId());
52+
}
53+
54+
@Test
55+
public void addCustomCommandWithSessionIdTest() {
56+
driver.addCommand(HttpMethod.POST, "/session/" + driver.getSessionId() + "/appium/app/launch", "launchApplication");
57+
final Response launchApplication = driver.execute("launchApplication");
58+
assertNotNull(launchApplication.getSessionId());
59+
}
60+
61+
@Test
62+
public void addCustomCommandWithElementIdTest() {
63+
IOSElement intA = driver.findElementById("IntegerA");
64+
intA.clear();
65+
driver.addCommand(HttpMethod.POST,
66+
String.format("/session/%s/appium/element/%s/value", driver.getSessionId(), intA.getId()), "setNewValue");
67+
final Response setNewValue = driver.execute("setNewValue", ImmutableMap.of("id", intA.getId(), "value", "8"));
68+
assertNotNull(setNewValue.getSessionId());
69+
}
70+
4471
@Test
4572
public void getDeviceTimeTest() {
4673
String time = driver.getDeviceTime();

0 commit comments

Comments
 (0)