From b03fabaa2dc86aa262051b6ab35bc7195c973f7e Mon Sep 17 00:00:00 2001 From: Alexander Poulikakos Date: Thu, 24 Nov 2016 14:30:44 +0100 Subject: [PATCH 1/8] Make AppiumService aware of port number when using withArgument(...) method. --- .../service/local/AppiumServiceBuilder.java | 11 +++++++--- .../localserver/ServerBuilderTest.java | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) 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 fb51b497d..0294d2d14 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 @@ -236,9 +236,14 @@ public AppiumServiceBuilder withArgument(ServerArgument argument) { * should be defined. * @return the self-reference. */ - public AppiumServiceBuilder withArgument(ServerArgument argument, String value) { - serverArguments.put(argument.getArgument(), value); - return this; + public AppiumServiceBuilder withArgument(ServerArgument argument, String value) { + String argName = argument.getArgument().trim(); + if (argName.equals("--port") || argName.equals("-p")) { + usingPort(Integer.valueOf(value)); + } else { + serverArguments.put(argName, value); + } + return this; } /** 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..3a38dcf7b 100644 --- a/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java @@ -312,4 +312,26 @@ private static File findCustomNode() { } } } + + @Test public void checkAbilityToStartServiceWithPortUsingFlags() throws Exception { + String port = "8996"; + String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); + + AppiumDriverLocalService service = new AppiumServiceBuilder() + .withArgument(() -> "--port", port) + .build(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); + } + + @Test public void checkAbilityToStartServiceWithPortUsingShortFlags() throws Exception { + String port = "8996"; + String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); + + AppiumDriverLocalService service = new AppiumServiceBuilder() + .withArgument(() -> "-p", port) + .build(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); + } } From 4a71ea58b049cd624773726365f54fa5703c2963 Mon Sep 17 00:00:00 2001 From: Alexander Poulikakos Date: Thu, 24 Nov 2016 14:46:19 +0100 Subject: [PATCH 2/8] Use equalsIgnoreCase instead --- .../appium/java_client/service/local/AppiumServiceBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0294d2d14..dc46d78a7 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 @@ -238,7 +238,7 @@ public AppiumServiceBuilder withArgument(ServerArgument argument) { */ public AppiumServiceBuilder withArgument(ServerArgument argument, String value) { String argName = argument.getArgument().trim(); - if (argName.equals("--port") || argName.equals("-p")) { + if (argName.equalsIgnoreCase("--port") || argName.equals("-p")) { usingPort(Integer.valueOf(value)); } else { serverArguments.put(argName, value); From 49f0e15546e09c133134bd47b931391bac57c4dd Mon Sep 17 00:00:00 2001 From: Alexander Poulikakos Date: Thu, 24 Nov 2016 15:15:47 +0100 Subject: [PATCH 3/8] changed to toLowerCase instead. --- .../java_client/service/local/AppiumServiceBuilder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 dc46d78a7..bf3a296da 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,8 +237,8 @@ public AppiumServiceBuilder withArgument(ServerArgument argument) { * @return the self-reference. */ public AppiumServiceBuilder withArgument(ServerArgument argument, String value) { - String argName = argument.getArgument().trim(); - if (argName.equalsIgnoreCase("--port") || argName.equals("-p")) { + String argName = argument.getArgument().trim().toLowerCase(); + if (argName.equals("--port") || argName.equals("-p")) { usingPort(Integer.valueOf(value)); } else { serverArguments.put(argName, value); From a4a0085a9ae556ead272725c9b2361d5dc29ef66 Mon Sep 17 00:00:00 2001 From: Alexander Poulikakos Date: Thu, 24 Nov 2016 16:07:53 +0100 Subject: [PATCH 4/8] switched order of equals check. --- .../service/local/AppiumServiceBuilder.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) 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 bf3a296da..3530939a2 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 @@ -238,7 +238,7 @@ public AppiumServiceBuilder withArgument(ServerArgument argument) { */ public AppiumServiceBuilder withArgument(ServerArgument argument, String value) { String argName = argument.getArgument().trim().toLowerCase(); - if (argName.equals("--port") || argName.equals("-p")) { + if ("--port".equals(argName) || "-p".equals(argName)) { usingPort(Integer.valueOf(value)); } else { serverArguments.put(argName, value); @@ -440,7 +440,8 @@ private String parseCapabilities() { * @param nodeJSExecutable The executable Node.js to use. * @return A self reference. */ - public AppiumServiceBuilder usingDriverExecutable(File nodeJSExecutable) { + @Override + public AppiumServiceBuilder usingDriverExecutable(File nodeJSExecutable) { return super.usingDriverExecutable(nodeJSExecutable); } @@ -451,7 +452,8 @@ public AppiumServiceBuilder usingDriverExecutable(File nodeJSExecutable) { * @param port The port to use; must be non-negative. * @return A self reference. */ - public AppiumServiceBuilder usingPort(int port) { + @Override + public AppiumServiceBuilder usingPort(int port) { return super.usingPort(port); } @@ -460,7 +462,8 @@ public AppiumServiceBuilder usingPort(int port) { * * @return A self reference. */ - public AppiumServiceBuilder usingAnyFreePort() { + @Override + public AppiumServiceBuilder usingAnyFreePort() { return super.usingAnyFreePort(); } @@ -481,7 +484,8 @@ public AppiumServiceBuilder usingAnyFreePort() { * @param logFile A file to write log to. * @return A self reference. */ - public AppiumServiceBuilder withLogFile(File logFile) { + @Override + public AppiumServiceBuilder withLogFile(File logFile) { return super.withLogFile(logFile); } From a443df58d255eed04a1b10bc784fe4d74c13b778 Mon Sep 17 00:00:00 2001 From: Alexander Poulikakos Date: Thu, 24 Nov 2016 16:18:34 +0100 Subject: [PATCH 5/8] code formating fixes. --- .../service/local/AppiumServiceBuilder.java | 24 ++++++------- .../localserver/ServerBuilderTest.java | 34 +++++++++---------- 2 files changed, 29 insertions(+), 29 deletions(-) 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 3530939a2..5b490741a 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 @@ -236,14 +236,14 @@ public AppiumServiceBuilder withArgument(ServerArgument argument) { * should be defined. * @return the self-reference. */ - public AppiumServiceBuilder withArgument(ServerArgument argument, String value) { - String argName = argument.getArgument().trim().toLowerCase(); - if ("--port".equals(argName) || "-p".equals(argName)) { - usingPort(Integer.valueOf(value)); - } else { - serverArguments.put(argName, value); - } - return this; + public AppiumServiceBuilder withArgument(ServerArgument argument, String value) { + String argName = argument.getArgument().trim().toLowerCase(); + if ("--port".equals(argName) || "-p".equals(argName)) { + usingPort(Integer.valueOf(value)); + } else { + serverArguments.put(argName, value); + } + return this; } /** @@ -441,7 +441,7 @@ private String parseCapabilities() { * @return A self reference. */ @Override - public AppiumServiceBuilder usingDriverExecutable(File nodeJSExecutable) { + public AppiumServiceBuilder usingDriverExecutable(File nodeJSExecutable) { return super.usingDriverExecutable(nodeJSExecutable); } @@ -453,7 +453,7 @@ public AppiumServiceBuilder usingDriverExecutable(File nodeJSExecutable) { * @return A self reference. */ @Override - public AppiumServiceBuilder usingPort(int port) { + public AppiumServiceBuilder usingPort(int port) { return super.usingPort(port); } @@ -463,7 +463,7 @@ public AppiumServiceBuilder usingPort(int port) { * @return A self reference. */ @Override - public AppiumServiceBuilder usingAnyFreePort() { + public AppiumServiceBuilder usingAnyFreePort() { return super.usingAnyFreePort(); } @@ -485,7 +485,7 @@ public AppiumServiceBuilder usingAnyFreePort() { * @return A self reference. */ @Override - public AppiumServiceBuilder withLogFile(File logFile) { + public AppiumServiceBuilder withLogFile(File logFile) { return super.withLogFile(logFile); } 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 3a38dcf7b..5ea47f9b2 100644 --- a/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java @@ -314,24 +314,24 @@ private static File findCustomNode() { } @Test public void checkAbilityToStartServiceWithPortUsingFlags() throws Exception { - String port = "8996"; - String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); - - AppiumDriverLocalService service = new AppiumServiceBuilder() - .withArgument(() -> "--port", port) - .build(); - String actualUrl = service.getUrl().toString(); - assertEquals(expectedUrl, actualUrl); - } + String port = "8996"; + String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); + + AppiumDriverLocalService service = new AppiumServiceBuilder() + .withArgument(() -> "--port", port) + .build(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); + } @Test public void checkAbilityToStartServiceWithPortUsingShortFlags() throws Exception { - String port = "8996"; - String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); - - AppiumDriverLocalService service = new AppiumServiceBuilder() - .withArgument(() -> "-p", port) - .build(); - String actualUrl = service.getUrl().toString(); - assertEquals(expectedUrl, actualUrl); + String port = "8996"; + String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); + + AppiumDriverLocalService service = new AppiumServiceBuilder() + .withArgument(() -> "-p", port) + .build(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); } } From 3ca140345946aaa13b72961c01fa860c1ebbdce2 Mon Sep 17 00:00:00 2001 From: Alexander Poulikakos Date: Thu, 24 Nov 2016 16:18:47 +0100 Subject: [PATCH 6/8] Code formating fixes From 5521c72f0427d737b56a01fbdc7120767d7bf797 Mon Sep 17 00:00:00 2001 From: Alexander Poulikakos Date: Thu, 24 Nov 2016 16:24:22 +0100 Subject: [PATCH 7/8] Changed unit test names --- .../io/appium/java_client/localserver/ServerBuilderTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 5ea47f9b2..652c55f4f 100644 --- a/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java @@ -313,7 +313,7 @@ private static File findCustomNode() { } } - @Test public void checkAbilityToStartServiceWithPortUsingFlags() throws Exception { + @Test public void checkAbilityToBuildServiceWithPortUsingFlag() throws Exception { String port = "8996"; String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); @@ -324,7 +324,7 @@ private static File findCustomNode() { assertEquals(expectedUrl, actualUrl); } - @Test public void checkAbilityToStartServiceWithPortUsingShortFlags() throws Exception { + @Test public void checkAbilityToBuildServiceWithPortUsingShortFlag() throws Exception { String port = "8996"; String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); From 3452ca744c7dacc09d9786abe5119eab3a980ef6 Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Thu, 24 Nov 2016 23:19:18 +0300 Subject: [PATCH 8/8] The addition to #522 - the ability use ip and log file as server args was provided - test update/fix --- .../service/local/AppiumServiceBuilder.java | 6 +- .../java_client/android/UIAutomator2Test.java | 4 +- .../localserver/ServerBuilderTest.java | 125 ++++++++++++++++-- 3 files changed, 118 insertions(+), 17 deletions(-) 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 8bd7dc3d7..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 @@ -238,8 +238,12 @@ public AppiumServiceBuilder withArgument(ServerArgument argument) { */ public AppiumServiceBuilder withArgument(ServerArgument argument, String value) { String argName = argument.getArgument().trim().toLowerCase(); - if ("--port".equals(argName) || "-p".equals(argName)) { + 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); } 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 652c55f4f..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(); @@ -317,21 +316,119 @@ private static File findCustomNode() { String port = "8996"; String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); - AppiumDriverLocalService service = new AppiumServiceBuilder() - .withArgument(() -> "--port", port) - .build(); - String actualUrl = service.getUrl().toString(); - assertEquals(expectedUrl, actualUrl); + 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 = new AppiumServiceBuilder() - .withArgument(() -> "-p", port) - .build(); - String actualUrl = service.getUrl().toString(); - assertEquals(expectedUrl, actualUrl); + 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(); + } + } } }