diff --git a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java index c168907d2..2b4de49ad 100644 --- a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java +++ b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java @@ -237,7 +237,16 @@ public AppiumServiceBuilder withArgument(ServerArgument argument) { * @return the self-reference. */ public AppiumServiceBuilder withArgument(ServerArgument argument, String value) { - serverArguments.put(argument.getArgument(), value); + String argName = argument.getArgument().trim().toLowerCase(); + if ("--port".equalsIgnoreCase(argName) || "-p".equalsIgnoreCase(argName)) { + usingPort(Integer.valueOf(value)); + } else if ("--address".equalsIgnoreCase(argName) || "-a".equalsIgnoreCase(argName)) { + withIPAddress(value); + } else if ("--log".equalsIgnoreCase(argName) || "-g".equalsIgnoreCase(argName)) { + withLogFile(new File(value)); + } else { + serverArguments.put(argName, value); + } return this; } @@ -434,6 +443,7 @@ private String parseCapabilities() { * @param nodeJSExecutable The executable Node.js to use. * @return A self reference. */ + @Override public AppiumServiceBuilder usingDriverExecutable(File nodeJSExecutable) { return super.usingDriverExecutable(nodeJSExecutable); } @@ -445,6 +455,7 @@ public AppiumServiceBuilder usingDriverExecutable(File nodeJSExecutable) { * @param port The port to use; must be non-negative. * @return A self reference. */ + @Override public AppiumServiceBuilder usingPort(int port) { return super.usingPort(port); } @@ -454,6 +465,7 @@ public AppiumServiceBuilder usingPort(int port) { * * @return A self reference. */ + @Override public AppiumServiceBuilder usingAnyFreePort() { return super.usingAnyFreePort(); } @@ -475,6 +487,7 @@ public AppiumServiceBuilder usingAnyFreePort() { * @param logFile A file to write log to. * @return A self reference. */ + @Override public AppiumServiceBuilder withLogFile(File logFile) { return super.withLogFile(logFile); } diff --git a/src/test/java/io/appium/java_client/android/UIAutomator2Test.java b/src/test/java/io/appium/java_client/android/UIAutomator2Test.java index 5fe7c3251..65e8a73dc 100644 --- a/src/test/java/io/appium/java_client/android/UIAutomator2Test.java +++ b/src/test/java/io/appium/java_client/android/UIAutomator2Test.java @@ -1,5 +1,7 @@ package io.appium.java_client.android; +import static org.junit.Assert.assertEquals; + import io.appium.java_client.remote.AutomationName; import io.appium.java_client.remote.MobileCapabilityType; import io.appium.java_client.service.local.AppiumDriverLocalService; @@ -13,8 +15,6 @@ import java.io.File; -import static org.junit.Assert.assertEquals; - public class UIAutomator2Test { private static AppiumDriverLocalService service; protected static AndroidDriver driver; diff --git a/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java b/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java index 964555eed..c8dd19964 100644 --- a/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java @@ -222,7 +222,7 @@ private static File findCustomNode() { } @Test public void checkAbilityToChangeOutputStream() throws Exception { - File file = new File("target/test"); + File file = new File("test"); file.createNewFile(); OutputStream stream = new FileOutputStream(file); AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService(); @@ -243,7 +243,7 @@ private static File findCustomNode() { } @Test public void checkAbilityToChangeOutputStreamAfterTheServiceIsStarted() throws Exception { - File file = new File("target/test"); + File file = new File("test"); file.createNewFile(); OutputStream stream = new FileOutputStream(file); AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService(); @@ -295,8 +295,7 @@ private static File findCustomNode() { @Test public void checkAbilityToStartServiceWithLogFile() throws Exception { AppiumDriverLocalService service = null; - File rootLogDir = new File("target/"); - File log = new File(rootLogDir, "Log.txt"); + File log = new File("Log.txt"); log.createNewFile(); try { service = new AppiumServiceBuilder().withLogFile(log).build(); @@ -312,4 +311,124 @@ private static File findCustomNode() { } } } + + @Test public void checkAbilityToBuildServiceWithPortUsingFlag() throws Exception { + String port = "8996"; + String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); + + AppiumDriverLocalService service = null; + + try { + service = new AppiumServiceBuilder() + .withArgument(() -> "--port", port) + .build(); + service.start(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); + } finally { + if (service != null) { + service.stop(); + } + } + } + + @Test public void checkAbilityToBuildServiceWithPortUsingShortFlag() throws Exception { + String port = "8996"; + String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); + + AppiumDriverLocalService service = null; + + try { + service = new AppiumServiceBuilder() + .withArgument(() -> "-p", port) + .build(); + service.start(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); + } finally { + if (service != null) { + service.stop(); + } + } + } + + @Test public void checkAbilityToBuildServiceWithIpUsingFlag() throws Exception { + String expectedUrl = String.format("http://%s:%s/wd/hub", testIP, 4723); + + AppiumDriverLocalService service = null; + + try { + service = new AppiumServiceBuilder() + .withArgument(() -> "--address", testIP) + .build(); + service.start(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); + } finally { + if (service != null) { + service.stop(); + } + } + } + + @Test public void checkAbilityToBuildServiceWithIpUsingShortFlag() throws Exception { + String expectedUrl = String.format("http://%s:%s/wd/hub", testIP, 4723); + + AppiumDriverLocalService service = null; + + try { + service = new AppiumServiceBuilder() + .withArgument(() -> "-a", testIP) + .build(); + service.start(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); + } finally { + if (service != null) { + service.stop(); + } + } + } + + @Test public void checkAbilityToBuildServiceWithLogFileUsingFlag() throws Exception { + AppiumDriverLocalService service = null; + + File log = new File("Log2.txt"); + + try { + service = new AppiumServiceBuilder() + .withArgument(() -> "--log", log.getAbsolutePath()) + .build(); + service.start(); + assertTrue(log.exists()); + } finally { + if (service != null) { + service.stop(); + } + if (log.exists()) { + log.delete(); + } + } + } + + @Test public void checkAbilityToBuildServiceWithLogFileUsingShortFlag() throws Exception { + AppiumDriverLocalService service = null; + + File log = new File("Log3.txt"); + + try { + service = new AppiumServiceBuilder() + .withArgument(() -> "-g", log.getAbsolutePath()) + .build(); + service.start(); + assertTrue(log.exists()); + } finally { + if (service != null) { + service.stop(); + } + if (log.exists()) { + log.delete(); + } + } + } }