From ea2c5c1852b2871ccbf779937352ee47c9c4df4f Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Sun, 17 Sep 2017 00:46:06 +0300 Subject: [PATCH 1/6] Tests of local appium DriverService were re-designed. --- .../localserver/custom_node_path.properties | 19 - .../local}/ServerBuilderTest.java | 602 +++++++++--------- .../local}/StartingAppLocallyTest.java | 2 +- .../local}/ThreadSafetyTest.java | 2 +- .../java_client/service/local/appium.js | 0 5 files changed, 293 insertions(+), 332 deletions(-) delete mode 100644 src/test/java/io/appium/java_client/localserver/custom_node_path.properties rename src/test/java/io/appium/java_client/{localserver => service/local}/ServerBuilderTest.java (84%) rename src/test/java/io/appium/java_client/{localserver => service/local}/StartingAppLocallyTest.java (99%) rename src/test/java/io/appium/java_client/{localserver => service/local}/ThreadSafetyTest.java (99%) create mode 100644 src/test/java/io/appium/java_client/service/local/appium.js diff --git a/src/test/java/io/appium/java_client/localserver/custom_node_path.properties b/src/test/java/io/appium/java_client/localserver/custom_node_path.properties deleted file mode 100644 index 15534a1cb..000000000 --- a/src/test/java/io/appium/java_client/localserver/custom_node_path.properties +++ /dev/null @@ -1,19 +0,0 @@ -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# See the NOTICE file distributed with this work for additional -# information regarding copyright ownership. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -path.to.custom.node.win=C:/Program Files (x86)/Appium/node_modules/appium/bin/appium.js -path.to.custom.node.macos=/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js -path.to.custom.node.linux=specify your path on your own diff --git a/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java similarity index 84% rename from src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java rename to src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java index a3e8ec945..7c1859579 100644 --- a/src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java @@ -1,311 +1,291 @@ -package io.appium.java_client.localserver; - -import static io.appium.java_client.remote.AndroidMobileCapabilityType.APP_ACTIVITY; -import static io.appium.java_client.remote.AndroidMobileCapabilityType.APP_PACKAGE; -import static io.appium.java_client.remote.AndroidMobileCapabilityType.CHROMEDRIVER_EXECUTABLE; -import static io.appium.java_client.remote.MobileCapabilityType.APP; -import static io.appium.java_client.remote.MobileCapabilityType.FULL_RESET; -import static io.appium.java_client.remote.MobileCapabilityType.NEW_COMMAND_TIMEOUT; -import static io.appium.java_client.remote.MobileCapabilityType.PLATFORM_NAME; -import static io.appium.java_client.service.local.AppiumDriverLocalService.buildDefaultService; -import static io.appium.java_client.service.local.AppiumServiceBuilder.APPIUM_PATH; -import static io.appium.java_client.service.local.flags.GeneralServerFlag.CALLBACK_ADDRESS; -import static io.appium.java_client.service.local.flags.GeneralServerFlag.PRE_LAUNCH; -import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE; -import static java.lang.System.setProperty; -import static java.util.Arrays.asList; -import static java.util.concurrent.TimeUnit.SECONDS; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import io.appium.java_client.service.local.AppiumDriverLocalService; -import io.appium.java_client.service.local.AppiumServiceBuilder; -import org.apache.commons.validator.routines.InetAddressValidator; -import org.junit.After; -import org.junit.BeforeClass; -import org.junit.Test; -import org.openqa.selenium.Platform; -import org.openqa.selenium.remote.DesiredCapabilities; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.OutputStream; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.util.Enumeration; -import java.util.List; -import java.util.Properties; - -public class ServerBuilderTest { - - private static Properties properties; - private static String testIP; - private AppiumDriverLocalService service; - private File file; - private OutputStream stream; - - - /** - * initialization. - */ - @BeforeClass public static void beforeClass() throws Exception { - File file = - new File("src/test/java/io/appium/java_client/localserver/custom_node_path.properties"); - FileInputStream fileInput = new FileInputStream(file); - properties = new Properties(); - properties.load(fileInput); - fileInput.close(); - - for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en - .hasMoreElements(); ) { - NetworkInterface intf = en.nextElement(); - for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr - .hasMoreElements(); ) { - InetAddress inetAddress = enumIpAddr.nextElement(); - if (!inetAddress.isLoopbackAddress()) { - InetAddressValidator validator = InetAddressValidator.getInstance(); - testIP = inetAddress.getHostAddress().toString(); - if (validator.isValid(testIP)) { - break; - } - testIP = null; - } - } - if (testIP != null) { - break; - } - } - } - - @After - public void tearDown() throws Exception { - if (service != null) { - service.stop(); - } - if (stream != null) { - stream.close(); - } - if (file != null && file.exists()) { - file.delete(); - } - System.clearProperty(APPIUM_PATH); - } - - private static File findCustomNode() { - Platform current = Platform.getCurrent(); - if (current.is(Platform.WINDOWS)) { - return new File(String.valueOf(properties.get("path.to.custom.node.win"))); - } - - if (current.is(Platform.MAC)) { - return new File(String.valueOf(properties.get("path.to.custom.node.macos"))); - } - - return new File(String.valueOf(properties.get("path.to.custom.node.linux"))); - } - - @Test public void checkAbilityToStartDefaultService() { - service = buildDefaultService(); - service.start(); - assertEquals(true, service.isRunning()); - } - - @Test public void checkAbilityToStartServiceUsingNodeDefinedInProperties() { - String definedNode = findCustomNode().getAbsolutePath(); - setProperty(APPIUM_PATH, definedNode); - service = buildDefaultService(); - service.start(); - assertTrue(service.isRunning()); - } - - @Test public void checkAbilityToStartServiceUsingNodeDefinedExplicitly() { - File node = findCustomNode(); - service = new AppiumServiceBuilder().withAppiumJS(node).build(); - service.start(); - assertTrue(service.isRunning()); - } - - @Test public void checkAbilityToStartServiceOnAFreePort() { - service = new AppiumServiceBuilder().usingAnyFreePort().build(); - service.start(); - assertTrue(service.isRunning()); - } - - @Test public void checkAbilityToStartServiceUsingNonLocalhostIP() throws Exception { - service = new AppiumServiceBuilder().withIPAddress(testIP).build(); - service.start(); - assertTrue(service.isRunning()); - } - - @Test public void checkAbilityToStartServiceUsingFlags() throws Exception { - service = new AppiumServiceBuilder() - .withArgument(CALLBACK_ADDRESS, testIP) - .withArgument(SESSION_OVERRIDE) - .withArgument(PRE_LAUNCH) - .build(); - service.start(); - assertTrue(service.isRunning()); - } - - @Test public void checkAbilityToStartServiceUsingCapabilities() throws Exception { - File appDir = new File("src/test/java/io/appium/java_client"); - File app = new File(appDir, "ApiDemos-debug.apk"); - File pageFactoryDir = new File("src/test/java/io/appium/java_client/pagefactory_tests"); - File chrome = new File(pageFactoryDir, "chromedriver.exe"); - - DesiredCapabilities caps = new DesiredCapabilities(); - caps.setCapability(PLATFORM_NAME, "Android"); - caps.setCapability(FULL_RESET, true); - caps.setCapability(NEW_COMMAND_TIMEOUT, 60); - caps.setCapability(APP_PACKAGE, "io.appium.android.apis"); - caps.setCapability(APP_ACTIVITY, ".view.WebView1"); - caps.setCapability(APP, app.getAbsolutePath()); - caps.setCapability(CHROMEDRIVER_EXECUTABLE, chrome.getAbsolutePath()); - - service = new AppiumServiceBuilder().withCapabilities(caps).build(); - service.start(); - assertTrue(service.isRunning()); - } - - @Test public void checkAbilityToStartServiceUsingCapabilitiesAndFlags() throws Exception { - File appDir = new File("src/test/java/io/appium/java_client"); - File app = new File(appDir, "ApiDemos-debug.apk"); - File pageFactoryDir = new File("src/test/java/io/appium/java_client/pagefactory_tests"); - File chrome = new File(pageFactoryDir, "chromedriver.exe"); - - DesiredCapabilities caps = new DesiredCapabilities(); - caps.setCapability(PLATFORM_NAME, "Android"); - caps.setCapability(FULL_RESET, true); - caps.setCapability(NEW_COMMAND_TIMEOUT, 60); - caps.setCapability(APP_PACKAGE, "io.appium.android.apis"); - caps.setCapability(APP_ACTIVITY, ".view.WebView1"); - caps.setCapability(APP, app.getAbsolutePath()); - caps.setCapability(CHROMEDRIVER_EXECUTABLE, chrome.getAbsolutePath()); - - service = new AppiumServiceBuilder() - .withArgument(CALLBACK_ADDRESS, testIP) - .withArgument(SESSION_OVERRIDE) - .withArgument(PRE_LAUNCH) - .withCapabilities(caps).build(); - service.start(); - assertTrue(service.isRunning()); - } - - @Test public void checkAbilityToChangeOutputStream() throws Exception { - file = new File("test"); - file.createNewFile(); - stream = new FileOutputStream(file); - service = buildDefaultService(); - service.addOutPutStream(stream); - service.start(); - assertTrue(file.length() > 0); - } - - @Test public void checkAbilityToChangeOutputStreamAfterTheServiceIsStarted() throws Exception { - file = new File("test"); - file.createNewFile(); - stream = new FileOutputStream(file); - service = buildDefaultService(); - service.start(); - service.addOutPutStream(stream); - service.isRunning(); - assertTrue(file.length() > 0); - } - - @Test public void checkAbilityToShutDownService() { - service = buildDefaultService(); - service.start(); - service.stop(); - assertFalse(service.isRunning()); - } - - @Test public void checkAbilityToStartAndShutDownFewServices() throws Exception { - List services = asList( - new AppiumServiceBuilder().usingAnyFreePort().build(), - new AppiumServiceBuilder().usingAnyFreePort().build(), - new AppiumServiceBuilder().usingAnyFreePort().build(), - new AppiumServiceBuilder().usingAnyFreePort().build()); - services.parallelStream().forEach(AppiumDriverLocalService::start); - assertTrue(services.stream().allMatch(AppiumDriverLocalService::isRunning)); - SECONDS.sleep(1); - services.parallelStream().forEach(AppiumDriverLocalService::stop); - assertTrue(services.stream().noneMatch(AppiumDriverLocalService::isRunning)); - } - - @Test public void checkAbilityToStartServiceWithLogFile() throws Exception { - file = new File("Log.txt"); - file.createNewFile(); - service = new AppiumServiceBuilder().withLogFile(file).build(); - service.start(); - assertTrue(file.exists()); - assertTrue(file.length() > 0); - } - - @Test public void checkAbilityToStartServiceWithPortUsingFlag() throws Exception { - String port = "8996"; - String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); - - service = new AppiumServiceBuilder() - .withArgument(() -> "--port", port) - .build(); - String actualUrl = service.getUrl().toString(); - assertEquals(expectedUrl, actualUrl); - service.start(); - } - - @Test public void checkAbilityToStartServiceWithPortUsingShortFlag() throws Exception { - String port = "8996"; - String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); - - service = new AppiumServiceBuilder() - .withArgument(() -> "-p", port) - .build(); - String actualUrl = service.getUrl().toString(); - assertEquals(expectedUrl, actualUrl); - service.start(); - } - - @Test public void checkAbilityToStartServiceWithIpUsingFlag() throws Exception { - String expectedUrl = String.format("http://%s:4723/wd/hub", testIP); - - service = new AppiumServiceBuilder() - .withArgument(() -> "--address", testIP) - .build(); - String actualUrl = service.getUrl().toString(); - assertEquals(expectedUrl, actualUrl); - service.start(); - } - - @Test public void checkAbilityToStartServiceWithIpUsingShortFlag() throws Exception { - String expectedUrl = String.format("http://%s:4723/wd/hub", testIP); - - service = new AppiumServiceBuilder() - .withArgument(() -> "-a", testIP) - .build(); - String actualUrl = service.getUrl().toString(); - assertEquals(expectedUrl, actualUrl); - service.start(); - } - - @Test public void checkAbilityToStartServiceWithLogFileUsingFlag() throws Exception { - file = new File("Log2.txt"); - - service = new AppiumServiceBuilder() - .withArgument(() -> "--log", file.getAbsolutePath()) - .build(); - service.start(); - assertTrue(file.exists()); - } - - @Test public void checkAbilityToStartServiceWithLogFileUsingShortFlag() throws Exception { - file = new File("Log3.txt"); - - service = new AppiumServiceBuilder() - .withArgument(() -> "-g", file.getAbsolutePath()) - .build(); - service.start(); - assertTrue(file.exists()); - } -} +package io.appium.java_client.service.local; + +import static io.appium.java_client.remote.AndroidMobileCapabilityType.APP_ACTIVITY; +import static io.appium.java_client.remote.AndroidMobileCapabilityType.APP_PACKAGE; +import static io.appium.java_client.remote.AndroidMobileCapabilityType.CHROMEDRIVER_EXECUTABLE; +import static io.appium.java_client.remote.MobileCapabilityType.APP; +import static io.appium.java_client.remote.MobileCapabilityType.FULL_RESET; +import static io.appium.java_client.remote.MobileCapabilityType.NEW_COMMAND_TIMEOUT; +import static io.appium.java_client.remote.MobileCapabilityType.PLATFORM_NAME; +import static io.appium.java_client.service.local.AppiumDriverLocalService.buildDefaultService; +import static io.appium.java_client.service.local.AppiumServiceBuilder.APPIUM_PATH; +import static io.appium.java_client.service.local.flags.GeneralServerFlag.CALLBACK_ADDRESS; +import static io.appium.java_client.service.local.flags.GeneralServerFlag.PRE_LAUNCH; +import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE; +import static java.lang.System.setProperty; +import static java.util.Arrays.asList; +import static java.util.Optional.ofNullable; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import org.apache.commons.validator.routines.InetAddressValidator; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openqa.selenium.remote.DesiredCapabilities; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.Enumeration; +import java.util.List; + +public class ServerBuilderTest { + + private static String pathToAppiumNodeInProperties; + private static String testIP; + private AppiumDriverLocalService service; + private File file; + private OutputStream stream; + + /** + * initialization. + */ + @BeforeClass public static void beforeClass() throws Exception { + pathToAppiumNodeInProperties = System.getProperty(APPIUM_PATH); + + for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en + .hasMoreElements(); ) { + NetworkInterface intf = en.nextElement(); + for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr + .hasMoreElements(); ) { + InetAddress inetAddress = enumIpAddr.nextElement(); + if (!inetAddress.isLoopbackAddress()) { + InetAddressValidator validator = InetAddressValidator.getInstance(); + testIP = inetAddress.getHostAddress().toString(); + if (validator.isValid(testIP)) { + break; + } + testIP = null; + } + } + if (testIP != null) { + break; + } + } + } + + @After + public void tearDown() throws Exception { + ofNullable(service).ifPresent(AppiumDriverLocalService::stop); + + if (stream != null) { + stream.close(); + } + + ofNullable(file).ifPresent(fileArg -> { + if (file.exists()) { + file.delete(); + } + }); + + System.clearProperty(APPIUM_PATH); + ofNullable(pathToAppiumNodeInProperties).ifPresent(s -> System.setProperty(APPIUM_PATH, s)); + } + + @Test public void checkAbilityToStartDefaultService() { + service = buildDefaultService(); + service.start(); + assertEquals(true, service.isRunning()); + } + + @Test public void checkAbilityToFindNodeDefinedInProperties() { + String definedNode = new File("src/test/java/io/appium/java_client/service/local/appium.js").getAbsolutePath(); + setProperty(APPIUM_PATH, definedNode); + assertThat(new File(new AppiumServiceBuilder().createArgs().get(0)), is(new File(definedNode))); + } + + @Test public void checkAbilityToUseNodeDefinedExplicitly() { + File node = new File("src/test/java/io/appium/java_client/service/local/appium.js"); + AppiumServiceBuilder builder = new AppiumServiceBuilder().withAppiumJS(node); + assertThat(new File(builder.createArgs().get(0)).getAbsolutePath(), is(node.getAbsolutePath())); + } + + @Test public void checkAbilityToStartServiceOnAFreePort() { + service = new AppiumServiceBuilder().usingAnyFreePort().build(); + service.start(); + assertTrue(service.isRunning()); + } + + @Test public void checkAbilityToStartServiceUsingNonLocalhostIP() throws Exception { + service = new AppiumServiceBuilder().withIPAddress(testIP).build(); + service.start(); + assertTrue(service.isRunning()); + } + + @Test public void checkAbilityToStartServiceUsingFlags() throws Exception { + service = new AppiumServiceBuilder() + .withArgument(CALLBACK_ADDRESS, testIP) + .withArgument(SESSION_OVERRIDE) + .withArgument(PRE_LAUNCH) + .build(); + service.start(); + assertTrue(service.isRunning()); + } + + @Test public void checkAbilityToStartServiceUsingCapabilities() throws Exception { + File appDir = new File("src/test/java/io/appium/java_client"); + File app = new File(appDir, "ApiDemos-debug.apk"); + File pageFactoryDir = new File("src/test/java/io/appium/java_client/pagefactory_tests"); + File chrome = new File(pageFactoryDir, "chromedriver.exe"); + + DesiredCapabilities caps = new DesiredCapabilities(); + caps.setCapability(PLATFORM_NAME, "Android"); + caps.setCapability(FULL_RESET, true); + caps.setCapability(NEW_COMMAND_TIMEOUT, 60); + caps.setCapability(APP_PACKAGE, "io.appium.android.apis"); + caps.setCapability(APP_ACTIVITY, ".view.WebView1"); + caps.setCapability(APP, app.getAbsolutePath()); + caps.setCapability(CHROMEDRIVER_EXECUTABLE, chrome.getAbsolutePath()); + + service = new AppiumServiceBuilder().withCapabilities(caps).build(); + service.start(); + assertTrue(service.isRunning()); + } + + @Test public void checkAbilityToStartServiceUsingCapabilitiesAndFlags() throws Exception { + File appDir = new File("src/test/java/io/appium/java_client"); + File app = new File(appDir, "ApiDemos-debug.apk"); + File pageFactoryDir = new File("src/test/java/io/appium/java_client/pagefactory_tests"); + File chrome = new File(pageFactoryDir, "chromedriver.exe"); + + DesiredCapabilities caps = new DesiredCapabilities(); + caps.setCapability(PLATFORM_NAME, "Android"); + caps.setCapability(FULL_RESET, true); + caps.setCapability(NEW_COMMAND_TIMEOUT, 60); + caps.setCapability(APP_PACKAGE, "io.appium.android.apis"); + caps.setCapability(APP_ACTIVITY, ".view.WebView1"); + caps.setCapability(APP, app.getAbsolutePath()); + caps.setCapability(CHROMEDRIVER_EXECUTABLE, chrome.getAbsolutePath()); + + service = new AppiumServiceBuilder() + .withArgument(CALLBACK_ADDRESS, testIP) + .withArgument(SESSION_OVERRIDE) + .withArgument(PRE_LAUNCH) + .withCapabilities(caps).build(); + service.start(); + assertTrue(service.isRunning()); + } + + @Test public void checkAbilityToChangeOutputStream() throws Exception { + file = new File("test"); + file.createNewFile(); + stream = new FileOutputStream(file); + service = buildDefaultService(); + service.addOutPutStream(stream); + service.start(); + assertTrue(file.length() > 0); + } + + @Test public void checkAbilityToChangeOutputStreamAfterTheServiceIsStarted() throws Exception { + file = new File("test"); + file.createNewFile(); + stream = new FileOutputStream(file); + service = buildDefaultService(); + service.start(); + service.addOutPutStream(stream); + service.isRunning(); + assertTrue(file.length() > 0); + } + + @Test public void checkAbilityToShutDownService() { + service = buildDefaultService(); + service.start(); + service.stop(); + assertFalse(service.isRunning()); + } + + @Test public void checkAbilityToStartAndShutDownFewServices() throws Exception { + List services = asList( + new AppiumServiceBuilder().usingAnyFreePort().build(), + new AppiumServiceBuilder().usingAnyFreePort().build(), + new AppiumServiceBuilder().usingAnyFreePort().build(), + new AppiumServiceBuilder().usingAnyFreePort().build()); + services.parallelStream().forEach(AppiumDriverLocalService::start); + assertTrue(services.stream().allMatch(AppiumDriverLocalService::isRunning)); + SECONDS.sleep(1); + services.parallelStream().forEach(AppiumDriverLocalService::stop); + assertTrue(services.stream().noneMatch(AppiumDriverLocalService::isRunning)); + } + + @Test public void checkAbilityToStartServiceWithLogFile() throws Exception { + file = new File("Log.txt"); + file.createNewFile(); + service = new AppiumServiceBuilder().withLogFile(file).build(); + service.start(); + assertTrue(file.exists()); + assertTrue(file.length() > 0); + } + + @Test public void checkAbilityToStartServiceWithPortUsingFlag() throws Exception { + String port = "8996"; + String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); + + service = new AppiumServiceBuilder() + .withArgument(() -> "--port", port) + .build(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); + service.start(); + } + + @Test public void checkAbilityToStartServiceWithPortUsingShortFlag() throws Exception { + String port = "8996"; + String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port); + + service = new AppiumServiceBuilder() + .withArgument(() -> "-p", port) + .build(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); + service.start(); + } + + @Test public void checkAbilityToStartServiceWithIpUsingFlag() throws Exception { + String expectedUrl = String.format("http://%s:4723/wd/hub", testIP); + + service = new AppiumServiceBuilder() + .withArgument(() -> "--address", testIP) + .build(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); + service.start(); + } + + @Test public void checkAbilityToStartServiceWithIpUsingShortFlag() throws Exception { + String expectedUrl = String.format("http://%s:4723/wd/hub", testIP); + + service = new AppiumServiceBuilder() + .withArgument(() -> "-a", testIP) + .build(); + String actualUrl = service.getUrl().toString(); + assertEquals(expectedUrl, actualUrl); + service.start(); + } + + @Test public void checkAbilityToStartServiceWithLogFileUsingFlag() throws Exception { + file = new File("Log2.txt"); + + service = new AppiumServiceBuilder() + .withArgument(() -> "--log", file.getAbsolutePath()) + .build(); + service.start(); + assertTrue(file.exists()); + } + + @Test public void checkAbilityToStartServiceWithLogFileUsingShortFlag() throws Exception { + file = new File("Log3.txt"); + + service = new AppiumServiceBuilder() + .withArgument(() -> "-g", file.getAbsolutePath()) + .build(); + service.start(); + assertTrue(file.exists()); + } +} diff --git a/src/test/java/io/appium/java_client/localserver/StartingAppLocallyTest.java b/src/test/java/io/appium/java_client/service/local/StartingAppLocallyTest.java similarity index 99% rename from src/test/java/io/appium/java_client/localserver/StartingAppLocallyTest.java rename to src/test/java/io/appium/java_client/service/local/StartingAppLocallyTest.java index a7c037bf3..e8ebdd1ff 100644 --- a/src/test/java/io/appium/java_client/localserver/StartingAppLocallyTest.java +++ b/src/test/java/io/appium/java_client/service/local/StartingAppLocallyTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.appium.java_client.localserver; +package io.appium.java_client.service.local; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; diff --git a/src/test/java/io/appium/java_client/localserver/ThreadSafetyTest.java b/src/test/java/io/appium/java_client/service/local/ThreadSafetyTest.java similarity index 99% rename from src/test/java/io/appium/java_client/localserver/ThreadSafetyTest.java rename to src/test/java/io/appium/java_client/service/local/ThreadSafetyTest.java index f4ea4ebe8..293057f8c 100644 --- a/src/test/java/io/appium/java_client/localserver/ThreadSafetyTest.java +++ b/src/test/java/io/appium/java_client/service/local/ThreadSafetyTest.java @@ -1,4 +1,4 @@ -package io.appium.java_client.localserver; +package io.appium.java_client.service.local; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; diff --git a/src/test/java/io/appium/java_client/service/local/appium.js b/src/test/java/io/appium/java_client/service/local/appium.js new file mode 100644 index 000000000..e69de29bb From 61360cc4df6ef9d0f75d0b2b9112498edf7befae Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Sun, 17 Sep 2017 22:41:17 +0300 Subject: [PATCH 2/6] ServerBuilderTest: code improvement. --- .../service/local/ServerBuilderTest.java | 84 +++++++++++-------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java index 7c1859579..670d61b55 100644 --- a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java @@ -12,10 +12,12 @@ import static io.appium.java_client.service.local.flags.GeneralServerFlag.CALLBACK_ADDRESS; import static io.appium.java_client.service.local.flags.GeneralServerFlag.PRE_LAUNCH; import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE; +import static java.lang.System.getProperty; import static java.lang.System.setProperty; import static java.util.Arrays.asList; import static java.util.Optional.ofNullable; import static java.util.concurrent.TimeUnit.SECONDS; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -38,18 +40,26 @@ public class ServerBuilderTest { - private static String pathToAppiumNodeInProperties; + /** + * It may be impossible to find the path to the instance of appium server due to different circumstance. + * So user may use environment variables/system properties to define the full path to the server + * appium.js that is supposed to be default. + */ + private static final String PATH_TO_APPIUM_NODE_IN_PROPERTIES = getProperty(APPIUM_PATH); + /** + * This is the path to the stub appium.js file + */ + private static final String PATH_T0_TEST_APPIUM_JS = "src/test/java/io/appium/java_client/service/local/appium.js"; + private static String testIP; private AppiumDriverLocalService service; - private File file; + private File testLogFile; private OutputStream stream; /** * initialization. */ @BeforeClass public static void beforeClass() throws Exception { - pathToAppiumNodeInProperties = System.getProperty(APPIUM_PATH); - for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements(); ) { NetworkInterface intf = en.nextElement(); @@ -58,16 +68,16 @@ public class ServerBuilderTest { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { InetAddressValidator validator = InetAddressValidator.getInstance(); - testIP = inetAddress.getHostAddress().toString(); - if (validator.isValid(testIP)) { - break; - } - testIP = null; + + testIP = ofNullable(testIP).orElseGet(() -> { + String result = inetAddress.getHostAddress(); + if (validator.isValid(result)) { + return result; + } + return null; + }); } } - if (testIP != null) { - break; - } } } @@ -79,14 +89,14 @@ public void tearDown() throws Exception { stream.close(); } - ofNullable(file).ifPresent(fileArg -> { - if (file.exists()) { - file.delete(); + ofNullable(testLogFile).ifPresent(savedTestLogFile -> { + if (savedTestLogFile.exists()) { + savedTestLogFile.delete(); } }); System.clearProperty(APPIUM_PATH); - ofNullable(pathToAppiumNodeInProperties).ifPresent(s -> System.setProperty(APPIUM_PATH, s)); + ofNullable(PATH_TO_APPIUM_NODE_IN_PROPERTIES).ifPresent(s -> setProperty(APPIUM_PATH, s)); } @Test public void checkAbilityToStartDefaultService() { @@ -96,13 +106,13 @@ public void tearDown() throws Exception { } @Test public void checkAbilityToFindNodeDefinedInProperties() { - String definedNode = new File("src/test/java/io/appium/java_client/service/local/appium.js").getAbsolutePath(); + String definedNode = new File(PATH_T0_TEST_APPIUM_JS).getAbsolutePath(); setProperty(APPIUM_PATH, definedNode); assertThat(new File(new AppiumServiceBuilder().createArgs().get(0)), is(new File(definedNode))); } @Test public void checkAbilityToUseNodeDefinedExplicitly() { - File node = new File("src/test/java/io/appium/java_client/service/local/appium.js"); + File node = new File(PATH_T0_TEST_APPIUM_JS); AppiumServiceBuilder builder = new AppiumServiceBuilder().withAppiumJS(node); assertThat(new File(builder.createArgs().get(0)).getAbsolutePath(), is(node.getAbsolutePath())); } @@ -174,24 +184,24 @@ public void tearDown() throws Exception { } @Test public void checkAbilityToChangeOutputStream() throws Exception { - file = new File("test"); - file.createNewFile(); - stream = new FileOutputStream(file); + testLogFile = new File("test"); + testLogFile.createNewFile(); + stream = new FileOutputStream(testLogFile); service = buildDefaultService(); service.addOutPutStream(stream); service.start(); - assertTrue(file.length() > 0); + assertThat(testLogFile.length(), greaterThan(0L)); } @Test public void checkAbilityToChangeOutputStreamAfterTheServiceIsStarted() throws Exception { - file = new File("test"); - file.createNewFile(); - stream = new FileOutputStream(file); + testLogFile = new File("test"); + testLogFile.createNewFile(); + stream = new FileOutputStream(testLogFile); service = buildDefaultService(); service.start(); service.addOutPutStream(stream); service.isRunning(); - assertTrue(file.length() > 0); + assertThat(testLogFile.length(), greaterThan(0L)); } @Test public void checkAbilityToShutDownService() { @@ -215,12 +225,12 @@ public void tearDown() throws Exception { } @Test public void checkAbilityToStartServiceWithLogFile() throws Exception { - file = new File("Log.txt"); - file.createNewFile(); - service = new AppiumServiceBuilder().withLogFile(file).build(); + testLogFile = new File("Log.txt"); + testLogFile.createNewFile(); + service = new AppiumServiceBuilder().withLogFile(testLogFile).build(); service.start(); - assertTrue(file.exists()); - assertTrue(file.length() > 0); + assertTrue(testLogFile.exists()); + assertThat(testLogFile.length(), greaterThan(0L)); } @Test public void checkAbilityToStartServiceWithPortUsingFlag() throws Exception { @@ -270,22 +280,22 @@ public void tearDown() throws Exception { } @Test public void checkAbilityToStartServiceWithLogFileUsingFlag() throws Exception { - file = new File("Log2.txt"); + testLogFile = new File("Log2.txt"); service = new AppiumServiceBuilder() - .withArgument(() -> "--log", file.getAbsolutePath()) + .withArgument(() -> "--log", testLogFile.getAbsolutePath()) .build(); service.start(); - assertTrue(file.exists()); + assertTrue(testLogFile.exists()); } @Test public void checkAbilityToStartServiceWithLogFileUsingShortFlag() throws Exception { - file = new File("Log3.txt"); + testLogFile = new File("Log3.txt"); service = new AppiumServiceBuilder() - .withArgument(() -> "-g", file.getAbsolutePath()) + .withArgument(() -> "-g", testLogFile.getAbsolutePath()) .build(); service.start(); - assertTrue(file.exists()); + assertTrue(testLogFile.exists()); } } From c1903152e9a9afa78e8cb4eb1113c914710e9cc2 Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Sun, 17 Sep 2017 22:52:14 +0300 Subject: [PATCH 3/6] ServerBuilderTest: magic strings were turned into final values --- .../java_client/service/local/ServerBuilderTest.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java index 670d61b55..9ba93374e 100644 --- a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java @@ -46,10 +46,13 @@ public class ServerBuilderTest { * appium.js that is supposed to be default. */ private static final String PATH_TO_APPIUM_NODE_IN_PROPERTIES = getProperty(APPIUM_PATH); + + private static final String ROOT_TEST_PATH = "src/test/java/io/appium/java_client/"; + /** * This is the path to the stub appium.js file */ - private static final String PATH_T0_TEST_APPIUM_JS = "src/test/java/io/appium/java_client/service/local/appium.js"; + private static final String PATH_T0_TEST_APPIUM_JS = ROOT_TEST_PATH + "service/local/appium.js"; private static String testIP; private AppiumDriverLocalService service; @@ -160,10 +163,8 @@ public void tearDown() throws Exception { } @Test public void checkAbilityToStartServiceUsingCapabilitiesAndFlags() throws Exception { - File appDir = new File("src/test/java/io/appium/java_client"); - File app = new File(appDir, "ApiDemos-debug.apk"); - File pageFactoryDir = new File("src/test/java/io/appium/java_client/pagefactory_tests"); - File chrome = new File(pageFactoryDir, "chromedriver.exe"); + File app = new File(ROOT_TEST_PATH, "ApiDemos-debug.apk"); + File chrome = new File(new File(ROOT_TEST_PATH, "pagefactory_tests"), "chromedriver.exe"); DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(PLATFORM_NAME, "Android"); From 2b86b18880c692252e236852b2119d980425d95b Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Sun, 17 Sep 2017 22:58:24 +0300 Subject: [PATCH 4/6] ServerBuilderTest: magic strings were turned into final values --- .../appium/java_client/service/local/ServerBuilderTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java index 9ba93374e..98ad7fc43 100644 --- a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java @@ -143,9 +143,8 @@ public void tearDown() throws Exception { } @Test public void checkAbilityToStartServiceUsingCapabilities() throws Exception { - File appDir = new File("src/test/java/io/appium/java_client"); - File app = new File(appDir, "ApiDemos-debug.apk"); - File pageFactoryDir = new File("src/test/java/io/appium/java_client/pagefactory_tests"); + File app = new File(ROOT_TEST_PATH, "ApiDemos-debug.apk"); + File pageFactoryDir = new File(ROOT_TEST_PATH, "pagefactory_tests"); File chrome = new File(pageFactoryDir, "chromedriver.exe"); DesiredCapabilities caps = new DesiredCapabilities(); From 916afef5bc2f3cfb03f325efe9cacb41e009dbc0 Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Tue, 19 Sep 2017 23:30:40 +0300 Subject: [PATCH 5/6] ServerBuilderTest: the path resolving --- .../service/local/ServerBuilderTest.java | 71 +++++++++++-------- .../service/local/{appium.js => main.js} | 0 2 files changed, 41 insertions(+), 30 deletions(-) rename src/test/java/io/appium/java_client/service/local/{appium.js => main.js} (100%) diff --git a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java index 98ad7fc43..0439b3da5 100644 --- a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java @@ -14,6 +14,7 @@ import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE; import static java.lang.System.getProperty; import static java.lang.System.setProperty; +import static java.nio.file.FileSystems.getDefault; import static java.util.Arrays.asList; import static java.util.Optional.ofNullable; import static java.util.concurrent.TimeUnit.SECONDS; @@ -35,6 +36,7 @@ import java.io.OutputStream; import java.net.InetAddress; import java.net.NetworkInterface; +import java.nio.file.Path; import java.util.Enumeration; import java.util.List; @@ -43,43 +45,51 @@ public class ServerBuilderTest { /** * It may be impossible to find the path to the instance of appium server due to different circumstance. * So user may use environment variables/system properties to define the full path to the server - * appium.js that is supposed to be default. + * main.js that is supposed to be default. */ private static final String PATH_TO_APPIUM_NODE_IN_PROPERTIES = getProperty(APPIUM_PATH); - private static final String ROOT_TEST_PATH = "src/test/java/io/appium/java_client/"; + private static final Path ROOT_TEST_PATH = getDefault().getPath("src") + .resolve("test").resolve("java").resolve("io").resolve("appium").resolve("java_client"); /** - * This is the path to the stub appium.js file + * This is the path to the stub main.js file */ - private static final String PATH_T0_TEST_APPIUM_JS = ROOT_TEST_PATH + "service/local/appium.js"; + private static final Path PATH_T0_TEST_MAIN_JS = ROOT_TEST_PATH + .resolve("service").resolve("local").resolve("main.js"); private static String testIP; private AppiumDriverLocalService service; private File testLogFile; private OutputStream stream; + private static String getLocalIP(NetworkInterface intf) { + String result = null; + for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr + .hasMoreElements(); ) { + String calculated; + InetAddress inetAddress = enumIpAddr.nextElement(); + if (!inetAddress.isLoopbackAddress()) { + InetAddressValidator validator = InetAddressValidator.getInstance(); + calculated = inetAddress.getHostAddress().toString(); + if (validator.isValid(calculated)) { + result = calculated; + break; + } + } + } + return result; + } + /** * initialization. */ @BeforeClass public static void beforeClass() throws Exception { for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en - .hasMoreElements(); ) { - NetworkInterface intf = en.nextElement(); - for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements(); ) { - InetAddress inetAddress = enumIpAddr.nextElement(); - if (!inetAddress.isLoopbackAddress()) { - InetAddressValidator validator = InetAddressValidator.getInstance(); - - testIP = ofNullable(testIP).orElseGet(() -> { - String result = inetAddress.getHostAddress(); - if (validator.isValid(result)) { - return result; - } - return null; - }); - } + NetworkInterface intf = en.nextElement(); + if ((testIP = getLocalIP(intf)) != null) { + break; } } } @@ -109,15 +119,17 @@ public void tearDown() throws Exception { } @Test public void checkAbilityToFindNodeDefinedInProperties() { - String definedNode = new File(PATH_T0_TEST_APPIUM_JS).getAbsolutePath(); - setProperty(APPIUM_PATH, definedNode); - assertThat(new File(new AppiumServiceBuilder().createArgs().get(0)), is(new File(definedNode))); + File definedNode = PATH_T0_TEST_MAIN_JS.toFile(); + setProperty(APPIUM_PATH, definedNode.getAbsolutePath()); + assertThat(new AppiumServiceBuilder().createArgs().get(0), is(definedNode.getAbsolutePath())); } @Test public void checkAbilityToUseNodeDefinedExplicitly() { - File node = new File(PATH_T0_TEST_APPIUM_JS); - AppiumServiceBuilder builder = new AppiumServiceBuilder().withAppiumJS(node); - assertThat(new File(builder.createArgs().get(0)).getAbsolutePath(), is(node.getAbsolutePath())); + File mainJS = PATH_T0_TEST_MAIN_JS.toFile(); + AppiumServiceBuilder builder = new AppiumServiceBuilder() + .withAppiumJS(mainJS); + assertThat(builder.createArgs().get(0), + is(mainJS.getAbsolutePath())); } @Test public void checkAbilityToStartServiceOnAFreePort() { @@ -143,9 +155,8 @@ public void tearDown() throws Exception { } @Test public void checkAbilityToStartServiceUsingCapabilities() throws Exception { - File app = new File(ROOT_TEST_PATH, "ApiDemos-debug.apk"); - File pageFactoryDir = new File(ROOT_TEST_PATH, "pagefactory_tests"); - File chrome = new File(pageFactoryDir, "chromedriver.exe"); + File app = ROOT_TEST_PATH.resolve("ApiDemos-debug.apk").toFile(); + File chrome = ROOT_TEST_PATH.resolve("pagefactory_tests").resolve("chromedriver.exe").toFile(); DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(PLATFORM_NAME, "Android"); @@ -162,8 +173,8 @@ public void tearDown() throws Exception { } @Test public void checkAbilityToStartServiceUsingCapabilitiesAndFlags() throws Exception { - File app = new File(ROOT_TEST_PATH, "ApiDemos-debug.apk"); - File chrome = new File(new File(ROOT_TEST_PATH, "pagefactory_tests"), "chromedriver.exe"); + File app = ROOT_TEST_PATH.resolve("ApiDemos-debug.apk").toFile(); + File chrome = ROOT_TEST_PATH.resolve("pagefactory_tests").resolve("chromedriver.exe").toFile(); DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(PLATFORM_NAME, "Android"); diff --git a/src/test/java/io/appium/java_client/service/local/appium.js b/src/test/java/io/appium/java_client/service/local/main.js similarity index 100% rename from src/test/java/io/appium/java_client/service/local/appium.js rename to src/test/java/io/appium/java_client/service/local/main.js From 5e300d521efe6ec96e37a049d4e4a4da5751f4dc Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Fri, 22 Sep 2017 22:37:17 +0300 Subject: [PATCH 6/6] ServerBuilderTest: ip calculation was improved --- .../java_client/service/local/ServerBuilderTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java index 0439b3da5..ce2999faf 100644 --- a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java @@ -64,21 +64,18 @@ public class ServerBuilderTest { private OutputStream stream; private static String getLocalIP(NetworkInterface intf) { - String result = null; for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements(); ) { - String calculated; InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { InetAddressValidator validator = InetAddressValidator.getInstance(); - calculated = inetAddress.getHostAddress().toString(); + String calculated = inetAddress.getHostAddress().toString(); if (validator.isValid(calculated)) { - result = calculated; - break; + return calculated; } } } - return result; + return null; } /**