From dbf71b1862af711534a1b4937f44874230efb336 Mon Sep 17 00:00:00 2001 From: TikhomirovSergey Date: Mon, 23 Nov 2015 16:07:56 +0300 Subject: [PATCH 1/9] #272 fix: initial commit - #272 fix which should be checked on UNIX-like OS - test improvements - AndroidGestureTest. The improvement of different API levels - Tests of compatibility with desktop browsers were migrated to Chrome. --- .../local/AppiumDriverLocalService.java | 15 +- .../service/local/AppiumServiceBuilder.java | 129 +++++++++--------- .../android/AndroidGestureTest.java | 2 +- .../DesktopBrowserCompatibilityTest.java | 128 ++--------------- .../pagefactory_tests/TimeOutResetTest.java | 14 +- .../widgets/HtmlOverrideWidgetTest.java | 22 ++- .../combined/HtmlCombinedWidgetTest.java | 21 ++- .../widgets/html/HtmlWidgetTest.java | 22 ++- 8 files changed, 151 insertions(+), 202 deletions(-) diff --git a/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java b/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java index 46504a9fd..91f180401 100644 --- a/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java +++ b/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java @@ -82,8 +82,13 @@ public URL getUrl() { @Override public boolean isRunning() { lock.lock(); - if (process == null) + if (process == null) { return false; + } + + if (!process.isRunning()) { + return false; + } try { ping(500, TimeUnit.MILLISECONDS); @@ -126,9 +131,11 @@ public void start() throws AppiumServerHasNotBeenStartedLocallyException { destroyProcess(); String msgTxt = "The local appium server has not been started. " + "The given Node.js executable: " + this.nodeJSExec.getAbsolutePath() + " Arguments: " + nodeJSArgs.toString() + " " + "\n"; - String processStream = process.getStdOut(); - if (!StringUtils.isBlank(processStream)) - msgTxt = msgTxt + "Process output: " + processStream + "\n"; + if (process != null) { + String processStream = process.getStdOut(); + if (!StringUtils.isBlank(processStream)) + msgTxt = msgTxt + "Process output: " + processStream + "\n"; + } throw new AppiumServerHasNotBeenStartedLocallyException(msgTxt, e); 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 3a9131d0e..7b635a948 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 @@ -19,6 +19,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import io.appium.java_client.service.local.flags.ServerArgument; +import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.validator.routines.InetAddressValidator; import org.openqa.selenium.Platform; @@ -34,7 +35,6 @@ public final class AppiumServiceBuilder extends DriverService.Builder { - private static final String NODE_MODULES_FOLDER = "node_modules"; private static final String APPIUM_FOLDER = "appium"; private static final String BIN_FOLDER = "bin"; private static final String APPIUM_JS = "appium.js"; @@ -46,15 +46,13 @@ public final class AppiumServiceBuilder extends DriverService.Builder serverArguments = new HashMap<>(); @@ -67,53 +65,63 @@ public final class AppiumServiceBuilder extends DriverService.Builder envVariables = System.getenv(); + Set keys = envVariables.keySet(); + + for (String key : keys) { + if (key.toUpperCase().trim().equals("Path".toUpperCase())) { + return key; + } + } + return null; + } + + private static String getTheLastStringFromsOutput(InputStream stream ) throws IOException { BufferedReader reader = new BufferedReader( new InputStreamReader(stream)); - String result = reader.readLine(); + String current; + String result = null; + while ((current = reader.readLine()) != null) { + result = current; + } reader.close(); return result; } - private static void validateNodeJSVersion(){ - Runtime rt = Runtime.getRuntime(); - String result = null; - Process p = null; - try { - p = rt.exec(NODE_COMMAND_PREFIX + " node -v"); - p.waitFor(); - result = getProcessOutput(p.getInputStream()); - } catch (Exception e) { - throw new InvalidNodeJSInstance("Node.js is not installed", e); + private static Process getSearchingProcess(String... command) throws Throwable { + ProcessBuilder processBuilder = new ProcessBuilder(command); + if (!StringUtils.isBlank(PATH_NAME)) { + String path = System.getenv().get(PATH_NAME); + processBuilder.environment().put(PATH_NAME, path); } - finally { - if (p != null) - p.destroy(); - } - - String versionNum = result.replace("v",""); - String[] tokens = versionNum.split("\\."); - if (Integer.parseInt(tokens[0]) < REQUIRED_MAJOR_NODE_JS || - Integer.parseInt(tokens[1]) < REQUIRED_MINOR_NODE_JS) - throw new InvalidNodeJSInstance("Current node.js version " + versionNum + "is lower than " + - "required (" + REQUIRED_MAJOR_NODE_JS + "." + REQUIRED_MINOR_NODE_JS + " or greater)"); + return processBuilder.start(); } private static File findNodeInCurrentFileSystem(){ - Runtime rt = Runtime.getRuntime(); String instancePath; Process p = null; try { - p = rt.exec(returnCommandThatSearchesForDefaultNode()); + p = getSearchingProcess(returnCommandThatSearchesForDefaultNode()); p.waitFor(); - instancePath = getProcessOutput(p.getInputStream()); - } catch (Exception e) { + instancePath = getTheLastStringFromsOutput(p.getInputStream()); + } catch (Throwable e) { throw new RuntimeException(e); } finally { @@ -122,33 +130,28 @@ private static File findNodeInCurrentFileSystem(){ } File result; - if (StringUtils.isBlank(instancePath) || !(result = new File(instancePath + File.separator + NODE_MODULES_FOLDER + - APPIUM_NODE_MASK)).exists()) - throw new InvalidServerInstanceException( "There is no installed nodes! Please install " + + if (StringUtils.isBlank(instancePath) || !(result = new File(instancePath + File.separator + + APPIUM_NODE_MASK)).exists()) { + throw new InvalidServerInstanceException("There is no installed nodes! Please install " + " node via NPM (https://www.npmjs.com/package/appium#using-node-js) or download and " + "install Appium app (http://appium.io/downloads.html)", new IOException("The installed appium node package has not been found.")); - + } return result; } private static void validateNodeStructure(File node){ String absoluteNodePath = node.getAbsolutePath(); - if (!node.exists()) + if (!node.exists()) { throw new InvalidServerInstanceException("The invalid appium node " + absoluteNodePath + " has been defined", new IOException("The node " + absoluteNodePath + "doesn't exist")); + } - if (!absoluteNodePath.endsWith(APPIUM_NODE_MASK)) + if (!absoluteNodePath.endsWith(APPIUM_NODE_MASK)) { throw new InvalidServerInstanceException("It is probably there is the corrupted appium server installation. Path " + absoluteNodePath + "doesn't match " + APPIUM_NODE_MASK); - } - - private static String defineNodeCommandPrefix() { - if (Platform.getCurrent().is(Platform.WINDOWS)) - return "cmd.exe /C"; - else - return "/bin/bash -l -c"; + } } public AppiumServiceBuilder() { @@ -157,13 +160,11 @@ public AppiumServiceBuilder() { @Override protected File findDefaultExecutable() { - validateNodeJSVersion(); - Runtime rt = Runtime.getRuntime(); Process p; try { - p = rt.exec(NODE_COMMAND_PREFIX + " node"); - } catch (IOException e) { - throw new RuntimeException(e); + p = getSearchingProcess(ArrayUtils.add(NODE_COMMAND_PREFIX, NODE)); + } catch (Throwable t) { + throw new RuntimeException(t); } try { @@ -171,8 +172,7 @@ protected File findDefaultExecutable() { PrintStream out = new PrintStream(outputStream) ; out.println("console.log(process.execPath);") ; out.close(); - - return new File(getProcessOutput(p.getInputStream())); + return new File(getTheLastStringFromsOutput(p.getInputStream())); } catch (Throwable t){ throw new RuntimeException(t); @@ -217,6 +217,11 @@ public AppiumServiceBuilder withIPAddress(String ipAddress){ return this; } + /** + * @param time a time value for the service starting up + * @param timeUnit a time unit for the service starting up + * @return self-reference + */ public AppiumServiceBuilder withStartUpTimeOut(long time, TimeUnit timeUnit){ checkNotNull(timeUnit); checkArgument(time > 0, "Time value should be greater than zero", time); @@ -251,8 +256,9 @@ protected ImmutableList createArgs() { argList.add("--port"); argList.add(String.valueOf(getPort())); - if (StringUtils.isBlank(ipAddress)) + if (StringUtils.isBlank(ipAddress)) { ipAddress = DEFAULT_LOCAL_IP_ADDRESS; + } else { InetAddressValidator validator = InetAddressValidator.getInstance(); if (!validator.isValid(ipAddress) && !validator.isValidInet4Address(ipAddress) && @@ -269,9 +275,7 @@ protected ImmutableList createArgs() { } Set> entries = serverArguments.entrySet(); - Iterator> iterator = entries.iterator(); - while (iterator.hasNext()){ - Map.Entry entry = iterator.next(); + for (Map.Entry entry : entries) { String argument = entry.getKey(); String value = entry.getValue(); if (StringUtils.isBlank(argument) || value == null) @@ -282,8 +286,7 @@ protected ImmutableList createArgs() { argList.add(value); } - ImmutableList result = new ImmutableList.Builder().addAll(argList).build(); - return result; + return new ImmutableList.Builder().addAll(argList).build(); } /** diff --git a/src/test/java/io/appium/java_client/android/AndroidGestureTest.java b/src/test/java/io/appium/java_client/android/AndroidGestureTest.java index 3714902be..1e2b6a93c 100644 --- a/src/test/java/io/appium/java_client/android/AndroidGestureTest.java +++ b/src/test/java/io/appium/java_client/android/AndroidGestureTest.java @@ -87,7 +87,7 @@ public void dragNDropTest() { driver.findElementByAccessibilityId("Views").click(); driver.findElement(MobileBy.AndroidUIAutomator("description(\"Drag and Drop\")")).click(); - WebElement actionBarTitle = driver.findElement(By.id("android:id/action_bar_title")); + WebElement actionBarTitle = driver.findElement(MobileBy.AndroidUIAutomator("text(\"Views/Drag and Drop\")")); assertEquals("Wrong title.", "Views/Drag and Drop", actionBarTitle.getText()); WebElement dragDot1 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_1")); diff --git a/src/test/java/io/appium/java_client/pagefactory_tests/DesktopBrowserCompatibilityTest.java b/src/test/java/io/appium/java_client/pagefactory_tests/DesktopBrowserCompatibilityTest.java index 08c3bdd50..14b8a3b8b 100644 --- a/src/test/java/io/appium/java_client/pagefactory_tests/DesktopBrowserCompatibilityTest.java +++ b/src/test/java/io/appium/java_client/pagefactory_tests/DesktopBrowserCompatibilityTest.java @@ -45,108 +45,21 @@ public class DesktopBrowserCompatibilityTest { - private static enum AvailableDrivers { - FIREFOX(FirefoxDriver.class, new ArrayList() { - private static final long serialVersionUID = 1L; - { - add(Platform.WINDOWS); - add(Platform.MAC); - } - }, new HashMap(), null), - - CHROME(ChromeDriver.class, - new ArrayList() { - private static final long serialVersionUID = 1L; - { - add(Platform.WINDOWS); - add(Platform.MAC); - } - - }, new HashMap() { - private static final long serialVersionUID = 1L; - - { - put(Platform.WINDOWS, - new File( - "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver.exe")); - put(Platform.MAC, - new File( - "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver")); - } - }, ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY), - SAFARI( - SafariDriver.class, new ArrayList() { - private static final long serialVersionUID = 1L; - { - add(Platform.MAC); - } - - }, new HashMap(), null); - // TODO Linux can be added if is necessary - - private final Class driverClazz; - private final List platformCompatible; - private final Map serviceBinaries; - private final String propertyName; - - private AvailableDrivers(Class driverClazz, - List platformCompatible, - Map serviceBinaries, String property) { - this.driverClazz = driverClazz; - this.platformCompatible = platformCompatible; - this.serviceBinaries = serviceBinaries; - this.propertyName = property; + public void setUp() { + if (current.is(Platform.WINDOWS)) { + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver.exe"); } - - private static AvailableDrivers getAvailableDriver( - Class driverClass, Platform p) { - AvailableDrivers[] availableDrivers = AvailableDrivers.values(); - for (AvailableDrivers availableDriver : availableDrivers) { - if (!availableDriver.driverClazz.equals(driverClass)){ - continue; - } - - for (Platform compatible: availableDriver.platformCompatible){ - if (p.is(compatible)){ - return availableDriver; - } - } - } - return null; - } - - private void setSystemProperty(Platform p) { - Platform platform = null; - for (Platform compatible: platformCompatible){ - if (p.is(compatible)){ - platform = compatible; - break; - } - } - - if ((platform != null) && (propertyName != null) - && (serviceBinaries.get(platform) != null)) { - System.setProperty(propertyName, serviceBinaries.get(platform) - .getAbsolutePath()); - } - } - } - - - public void setUp(Class driverClass) { - AvailableDrivers availableDriver = AvailableDrivers.getAvailableDriver(driverClass, current); - if (availableDriver != null){ - availableDriver.setSystemProperty(current); + else { + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver"); } } private final Platform current = Platform.getCurrent(); private final long IMPLICITLY_WAIT = 15; - - - @AndroidFindBy(className = "someClass") @iOSFindBys({ @iOSFindBy(xpath = "//selector[1]"), @@ -159,7 +72,8 @@ public void setUp(Class driverClass) { private WebDriver trap1; private List> trap2; - private void test(WebDriver driver){ + private void test(){ + WebDriver driver = new ChromeDriver(); try { PageFactory.initElements(new AppiumFieldDecorator(driver, IMPLICITLY_WAIT, TimeUnit.SECONDS), this); driver.get("file:///" + new File("src/test/java/io/appium/java_client/hello appium - saved page.htm").getAbsolutePath()); @@ -172,29 +86,9 @@ private void test(WebDriver driver){ } } - @Test - public void fireFoxTest() { - if (AvailableDrivers.getAvailableDriver(FirefoxDriver.class, current)!=null){ - setUp(FirefoxDriver.class); - test(new FirefoxDriver()); - } - } - @Test public void chromeTest() { - System.getProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY); - if (AvailableDrivers.getAvailableDriver(ChromeDriver.class, current)!=null){ - setUp(ChromeDriver.class); - test(new ChromeDriver()); - } + setUp(); + test(); } - - @Test - public void safariTest() { - if (AvailableDrivers.getAvailableDriver(SafariDriver.class, current)!=null){ - setUp(SafariDriver.class); - test(new SafariDriver()); - } - } - } diff --git a/src/test/java/io/appium/java_client/pagefactory_tests/TimeOutResetTest.java b/src/test/java/io/appium/java_client/pagefactory_tests/TimeOutResetTest.java index c560aa6e9..641bc451f 100644 --- a/src/test/java/io/appium/java_client/pagefactory_tests/TimeOutResetTest.java +++ b/src/test/java/io/appium/java_client/pagefactory_tests/TimeOutResetTest.java @@ -23,9 +23,11 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; -import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.support.FindAll; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; @@ -52,7 +54,15 @@ public class TimeOutResetTest { @Before public void setUp() throws Exception { - driver = new FirefoxDriver(); + if (Platform.getCurrent().is(Platform.WINDOWS)) { + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver.exe"); + } + else { + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver"); + } + driver = new ChromeDriver(); timeOutDuration = new TimeOutDuration(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT); PageFactory.initElements(new AppiumFieldDecorator(driver, timeOutDuration), this); diff --git a/src/test/java/io/appium/java_client/pagefactory_tests/widgets/HtmlOverrideWidgetTest.java b/src/test/java/io/appium/java_client/pagefactory_tests/widgets/HtmlOverrideWidgetTest.java index 1c146807a..04c49f462 100644 --- a/src/test/java/io/appium/java_client/pagefactory_tests/widgets/HtmlOverrideWidgetTest.java +++ b/src/test/java/io/appium/java_client/pagefactory_tests/widgets/HtmlOverrideWidgetTest.java @@ -2,13 +2,16 @@ import io.appium.java_client.pagefactory.AppiumFieldDecorator; import io.appium.java_client.pagefactory.TimeOutDuration; +import io.appium.java_client.pagefactory_tests.widgets.html.RottenTomatoesSite; import org.apache.commons.lang3.StringUtils; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.NoSuchElementException; -import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.Platform; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.support.PageFactory; import java.io.File; @@ -19,14 +22,23 @@ public class HtmlOverrideWidgetTest implements WidgetTest{ - private static FirefoxDriver driver; + private static ChromeDriver driver; private static RottenTomatoes rottenTomatoes; @BeforeClass public static void beforeClass() throws Exception { - driver = new FirefoxDriver(); - driver.get("file:///" + new File("src/test/java/io/appium/java_client/RottenTomatoesSnapshot.html").getAbsolutePath()); + if (Platform.getCurrent().is(Platform.WINDOWS)) { + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver.exe"); + } + else { + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver"); + } + + driver = new ChromeDriver(); + driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); rottenTomatoes = new RottenTomatoes(); PageFactory.initElements(new AppiumFieldDecorator(driver, new TimeOutDuration(5, TimeUnit.SECONDS)), rottenTomatoes); } @@ -34,7 +46,7 @@ public static void beforeClass() throws Exception { @Before public void setUp() throws Exception { if (driver != null) - driver.navigate().back(); + driver.get("file:///" + new File("src/test/java/io/appium/java_client/RottenTomatoesSnapshot.html").getAbsolutePath()); } @AfterClass diff --git a/src/test/java/io/appium/java_client/pagefactory_tests/widgets/combined/HtmlCombinedWidgetTest.java b/src/test/java/io/appium/java_client/pagefactory_tests/widgets/combined/HtmlCombinedWidgetTest.java index e0557fad1..34e5b0c78 100644 --- a/src/test/java/io/appium/java_client/pagefactory_tests/widgets/combined/HtmlCombinedWidgetTest.java +++ b/src/test/java/io/appium/java_client/pagefactory_tests/widgets/combined/HtmlCombinedWidgetTest.java @@ -10,7 +10,9 @@ import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.NoSuchElementException; -import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.Platform; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.support.PageFactory; import java.io.File; @@ -21,14 +23,23 @@ public class HtmlCombinedWidgetTest implements WidgetTest{ - private static FirefoxDriver driver; + private static ChromeDriver driver; private static RottenTomatoesAppWithCombinedWidgets rottenTomatoes; @BeforeClass public static void beforeClass() throws Exception { - driver = new FirefoxDriver(); - driver.get("file:///" + new File("src/test/java/io/appium/java_client/RottenTomatoesSnapshot.html").getAbsolutePath()); + if (Platform.getCurrent().is(Platform.WINDOWS)) { + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver.exe"); + } + else { + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver"); + } + + driver = new ChromeDriver(); + driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); rottenTomatoes = new RottenTomatoesAppWithCombinedWidgets(); PageFactory.initElements(new AppiumFieldDecorator(driver, new TimeOutDuration(5, TimeUnit.SECONDS)), rottenTomatoes); } @@ -36,7 +47,7 @@ public static void beforeClass() throws Exception { @Before public void setUp() throws Exception { if (driver != null) - driver.navigate().back(); + driver.get("file:///" + new File("src/test/java/io/appium/java_client/RottenTomatoesSnapshot.html").getAbsolutePath()); } @AfterClass diff --git a/src/test/java/io/appium/java_client/pagefactory_tests/widgets/html/HtmlWidgetTest.java b/src/test/java/io/appium/java_client/pagefactory_tests/widgets/html/HtmlWidgetTest.java index efac13ec1..eeb1d9352 100644 --- a/src/test/java/io/appium/java_client/pagefactory_tests/widgets/html/HtmlWidgetTest.java +++ b/src/test/java/io/appium/java_client/pagefactory_tests/widgets/html/HtmlWidgetTest.java @@ -4,13 +4,16 @@ import io.appium.java_client.pagefactory.TimeOutDuration; import io.appium.java_client.pagefactory_tests.widgets.Movie; import io.appium.java_client.pagefactory_tests.widgets.WidgetTest; +import io.appium.java_client.pagefactory_tests.widgets.combined.RottenTomatoesAppWithCombinedWidgets; import org.apache.commons.lang3.StringUtils; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.NoSuchElementException; -import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.Platform; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.support.PageFactory; import java.io.File; @@ -21,14 +24,23 @@ public class HtmlWidgetTest implements WidgetTest{ - private static FirefoxDriver driver; + private static ChromeDriver driver; private static RottenTomatoesSite rottenTomatoesSite; @BeforeClass public static void beforeClass() throws Exception { - driver = new FirefoxDriver(); - driver.get("file:///" + new File("src/test/java/io/appium/java_client/RottenTomatoesSnapshot.html").getAbsolutePath()); + if (Platform.getCurrent().is(Platform.WINDOWS)) { + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver.exe"); + } + else { + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, + "src/test/java/io/appium/java_client/pagefactory_tests/chromedriver"); + } + + driver = new ChromeDriver(); + driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); rottenTomatoesSite = new RottenTomatoesSite(); PageFactory.initElements(new AppiumFieldDecorator(driver, new TimeOutDuration(5, TimeUnit.SECONDS)), rottenTomatoesSite); } @@ -36,7 +48,7 @@ public static void beforeClass() throws Exception { @Before public void setUp() throws Exception { if (driver != null) - driver.navigate().back(); + driver.get("file:///" + new File("src/test/java/io/appium/java_client/RottenTomatoesSnapshot.html").getAbsolutePath()); } @AfterClass From 5d7297b9ced3d4bb32b2e4be58fb10eaca9c204e Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Mon, 23 Nov 2015 23:23:26 +0300 Subject: [PATCH 2/9] README update --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5b9efccc1..31dbacbc6 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ If you are using the Eclipse IDE, make sure you are using verison Luna or later. - bug fix and enhancements of io.appium.java_client.service.local.AppiumDriverLocalService - FIXED bug which was found and reproduced with Eclipse for Mac OS X. Please read about details here: [#252](https://github.com/appium/java-client/issues/252) Thanks to [saikrishna321](https://github.com/saikrishna321) for the bug report + - FIXED bug which was found out by [Jonahss](https://github.com/Jonahss). Thanks for the reporting. Details: [#272](https://github.com/appium/java-client/issues/272) - The ability to set additional output streams was provided - The additional __startActivity()__ method was added to AndroidDriver. It allows to start activities without the stopping of a target app Thanks to [deadmoto](https://github.com/deadmoto) for the contribution From 383c9b0125cfd0d04aeb199f2e6af558fc197638 Mon Sep 17 00:00:00 2001 From: TikhomirovSergey Date: Tue, 24 Nov 2015 18:17:57 +0300 Subject: [PATCH 3/9] #272 fix: an additional improvement The working with npm was moved to scripts which are packed in the jar file. These scripts should be used in runtime. Changes: - added scripts - AppiumServiceBuilder was refactored once again :) - engines which work with these scrips and npm were implemented --- pom.xml | 225 +++++++++--------- .../service/local/AppiumServiceBuilder.java | 69 ++++-- .../java_client/service/local/NPMScript.java | 75 ++++++ .../scripts/get_path_to_default_node.cmd | 1 + .../scripts/get_path_to_default_node.sh | 2 + 5 files changed, 236 insertions(+), 136 deletions(-) create mode 100644 src/main/java/io/appium/java_client/service/local/NPMScript.java create mode 100644 src/main/resources/scripts/get_path_to_default_node.cmd create mode 100644 src/main/resources/scripts/get_path_to_default_node.sh diff --git a/pom.xml b/pom.xml index 9263dafd7..25575045a 100644 --- a/pom.xml +++ b/pom.xml @@ -16,8 +16,8 @@ --> - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 io.appium java-client @@ -65,97 +65,102 @@ Java client for Appium Mobile Webdriver http://appium.io - - - Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + - - https://github.com/appium/java-client - scm:git:git://github.com/appium/java-client.git - scm:git:git@github.com:appium/java-client.git - + + https://github.com/appium/java-client + scm:git:git://github.com/appium/java-client.git + scm:git:git@github.com:appium/java-client.git + - - - jonahss@gmail.com - Jonah Stiennon - https://github.com/jonahss - jonahss - + + + jonahss@gmail.com + Jonah Stiennon + https://github.com/jonahss + jonahss + tichomirovsergey@gmail.com Sergey Tikhomirov https://github.com/TikhomirovSergey TikhomirovSergey - + - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + - - - - org.apache.maven.plugins - maven-source-plugin - 2.2.1 - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - - attach-javadocs - - jar - - - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.5 - - - sign-artifacts - verify - - sign - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.3 - - 1.7 - 1.7 + + + + src/main/resources + + + + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.7 + 1.7 eclipse - + org.codehaus.plexus @@ -163,32 +168,32 @@ 2.5 - - - org.apache.maven.plugins - maven-surefire-plugin - 2.18.1 - - - org.apache.maven.surefire - surefire-junit47 - 2.18.1 - - - - - - test - - integration-test - - - **/*Test.java - - - - - - - + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18.1 + + + org.apache.maven.surefire + surefire-junit47 + 2.18.1 + + + + + + test + + integration-test + + + **/*Test.java + + + + + + + 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 7b635a948..bf6552d84 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 @@ -19,6 +19,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import io.appium.java_client.service.local.flags.ServerArgument; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.validator.routines.InetAddressValidator; @@ -45,42 +46,22 @@ public final class AppiumServiceBuilder extends DriverService.Builder serverArguments = new HashMap<>(); private File appiumJS; private String ipAddress = DEFAULT_LOCAL_IP_ADDRESS; + private File npmScript; //The first starting is slow sometimes on some //environment private long startupTimeout = 120; private TimeUnit timeUnit = TimeUnit.SECONDS; - - private static String[] returnCommandThatSearchesForDefaultNode(){ - if (Platform.getCurrent().is(Platform.WINDOWS)) { - return COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM_WIN; - } - return COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM; - } - - private static String[] defineNodeCommandPrefix() { - if (Platform.getCurrent().is(Platform.WINDOWS)) { - return new String[]{"cmd.exe", "/C"}; - } - else { - return new String[]{"/bin/bash", "-l", "-c"}; - } - } - private static String getPathVarName(){ Map envVariables = System.getenv(); Set keys = envVariables.keySet(); @@ -114,11 +95,31 @@ private static Process getSearchingProcess(String... command) throws Throwable { return processBuilder.start(); } - private static File findNodeInCurrentFileSystem(){ + private void setUpNPMScript(){ + if (npmScript != null) { + return; + } + + if (Platform.getCurrent().is(Platform.WINDOWS)) { + npmScript = NPMScript.GET_PATH_TO_DEFAULT_NODE_WIN.getScriptFile(); + } + else { + npmScript = NPMScript.GET_PATH_TO_DEFAULT_NODE_UNIX.getScriptFile(); + } + } + + private File findNodeInCurrentFileSystem(){ + setUpNPMScript(); + String instancePath; Process p = null; try { - p = getSearchingProcess(returnCommandThatSearchesForDefaultNode()); + if (Platform.getCurrent().is(Platform.WINDOWS)) { + p = getSearchingProcess(ArrayUtils.add(CMD_EXE, npmScript.getAbsolutePath())); + } + else { + p = getSearchingProcess(ArrayUtils.add(BIN_BASH, npmScript.getAbsolutePath())); + } p.waitFor(); instancePath = getTheLastStringFromsOutput(p.getInputStream()); } catch (Throwable e) { @@ -162,7 +163,12 @@ public AppiumServiceBuilder() { protected File findDefaultExecutable() { Process p; try { - p = getSearchingProcess(ArrayUtils.add(NODE_COMMAND_PREFIX, NODE)); + if (Platform.getCurrent().is(Platform.WINDOWS)) { + p = getSearchingProcess(ArrayUtils.add(CMD_EXE, NODE)); + } + else { + p = getSearchingProcess(ArrayUtils.add(BIN_BASH, NODE)); + } } catch (Throwable t) { throw new RuntimeException(t); } @@ -351,4 +357,15 @@ protected AppiumDriverLocalService createDriverService(File nodeJSExecutable, in throw new RuntimeException(e); } } + + @Override + protected void finalize() throws Throwable { + if (npmScript != null) { + try { + FileUtils.forceDelete(npmScript); + } catch (IOException ignored) { + } + } + super.finalize(); + } } diff --git a/src/main/java/io/appium/java_client/service/local/NPMScript.java b/src/main/java/io/appium/java_client/service/local/NPMScript.java new file mode 100644 index 000000000..1d9a46d2c --- /dev/null +++ b/src/main/java/io/appium/java_client/service/local/NPMScript.java @@ -0,0 +1,75 @@ +/* + * 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. + */ + +package io.appium.java_client.service.local; + +import org.apache.commons.io.IOUtils; +import java.io.*; + +enum NPMScript { + GET_PATH_TO_DEFAULT_NODE_WIN("get_path_to_default_node.cmd"), + GET_PATH_TO_DEFAULT_NODE_UNIX("get_path_to_default_node.sh") + ; + private static final String RESOURCE_FOLDER = "/scripts/"; + private final String script; + + NPMScript(String script) { + this.script = script; + } + + public File getScriptFile() { + InputStream inputStream = getClass().getResourceAsStream(RESOURCE_FOLDER + this.script); + byte[] bytes; + try { + bytes = IOUtils.toByteArray(inputStream); + } catch (IOException e) { + throw new RuntimeException(e); + } + + String[] splittedName = this.script.split("\\."); + File scriptFile; + try { + scriptFile = File.createTempFile(splittedName[0], "." + splittedName[1]); + } catch (IOException e) { + throw new RuntimeException(e); + } + + if (!scriptFile.exists()) { + try { + scriptFile.createNewFile(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + FileOutputStream output; + try { + output = new FileOutputStream(scriptFile, true); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + + try { + output.write(bytes); + output.flush(); + output.close(); + return scriptFile; + } catch (IOException e) { + throw new RuntimeException(e); + } + + } +} diff --git a/src/main/resources/scripts/get_path_to_default_node.cmd b/src/main/resources/scripts/get_path_to_default_node.cmd new file mode 100644 index 000000000..2a48ab846 --- /dev/null +++ b/src/main/resources/scripts/get_path_to_default_node.cmd @@ -0,0 +1 @@ +npm root -g \ No newline at end of file diff --git a/src/main/resources/scripts/get_path_to_default_node.sh b/src/main/resources/scripts/get_path_to_default_node.sh new file mode 100644 index 000000000..9312047af --- /dev/null +++ b/src/main/resources/scripts/get_path_to_default_node.sh @@ -0,0 +1,2 @@ +#!bin/bash +npm root -g \ No newline at end of file From b25d256291ded459b972676328786fcd762443c1 Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Thu, 26 Nov 2015 00:55:34 +0300 Subject: [PATCH 4/9] #228 fix: Attempt to fix UNIX issues --- .../service/local/AppiumServiceBuilder.java | 16 +++++++++++----- .../scripts/get_path_to_default_node.sh | 6 ++++-- 2 files changed, 15 insertions(+), 7 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 bf6552d84..f107b4f2c 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 @@ -27,6 +27,7 @@ import org.openqa.selenium.remote.service.DriverService; import java.io.*; +import java.nio.charset.Charset; import java.util.*; import java.util.concurrent.TimeUnit; @@ -46,7 +47,8 @@ public final class AppiumServiceBuilder extends DriverService.Builder Date: Thu, 26 Nov 2015 23:25:25 +0300 Subject: [PATCH 5/9] #272 fix: AppiumServerBuilder was completely fixed --- .../service/local/AppiumServiceBuilder.java | 70 +++++++++++++------ .../service/local/InvalidNodeJSInstance.java | 4 -- 2 files changed, 50 insertions(+), 24 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 f107b4f2c..f78938186 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 @@ -47,8 +47,7 @@ public final class AppiumServiceBuilder extends DriverService.Builder Date: Fri, 27 Nov 2015 12:46:56 +0300 Subject: [PATCH 6/9] =?UTF-8?q?README=20update=20=E2=84=962?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../appium/java_client/service/local/AppiumServiceBuilder.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 31dbacbc6..ab11c13d2 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ If you are using the Eclipse IDE, make sure you are using verison Luna or later. - FIXED bug which was found and reproduced with Eclipse for Mac OS X. Please read about details here: [#252](https://github.com/appium/java-client/issues/252) Thanks to [saikrishna321](https://github.com/saikrishna321) for the bug report - FIXED bug which was found out by [Jonahss](https://github.com/Jonahss). Thanks for the reporting. Details: [#272](https://github.com/appium/java-client/issues/272) + and [#273](https://github.com/appium/java-client/issues/273) - The ability to set additional output streams was provided - The additional __startActivity()__ method was added to AndroidDriver. It allows to start activities without the stopping of a target app Thanks to [deadmoto](https://github.com/deadmoto) for the contribution 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 f78938186..85450a107 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 @@ -100,7 +100,7 @@ private static String readErrorStream(Process process) { if (StringUtils.isBlank(current)) { continue; } - result = result + current; + result = result + current + "\n"; } reader.close(); } catch (IOException e) { From dc70c9ce1a8a0ed18705d035f0bde1b29ab34031 Mon Sep 17 00:00:00 2001 From: TikhomirovSergey Date: Tue, 8 Dec 2015 19:17:19 +0300 Subject: [PATCH 7/9] #272 fix: Command invocation was improved Also a new system property "appium.node.js.exec.path" has been added and it is supported now. --- .../service/local/AppiumServiceBuilder.java | 144 ++++++------------ .../local/{NPMScript.java => Scripts.java} | 8 +- src/main/resources/scripts/getExe.js | 1 + .../scripts/get_path_to_default_node.cmd | 1 - 4 files changed, 55 insertions(+), 99 deletions(-) rename src/main/java/io/appium/java_client/service/local/{NPMScript.java => Scripts.java} (95%) create mode 100644 src/main/resources/scripts/getExe.js delete mode 100644 src/main/resources/scripts/get_path_to_default_node.cmd 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 85450a107..66cf81abe 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 @@ -20,14 +20,13 @@ import com.google.common.collect.ImmutableMap; import io.appium.java_client.service.local.flags.ServerArgument; import org.apache.commons.io.FileUtils; -import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.validator.routines.InetAddressValidator; import org.openqa.selenium.Platform; +import org.openqa.selenium.os.CommandLine; import org.openqa.selenium.remote.service.DriverService; import java.io.*; -import java.nio.charset.Charset; import java.util.*; import java.util.concurrent.TimeUnit; @@ -37,123 +36,74 @@ public final class AppiumServiceBuilder extends DriverService.Builder { + public static final String APPIUM_NODE_PROPERTY = "appium.node.path"; + public static final String NODE_JS_EXECUTABLE_PROPERTY = "appium.node.js.exec.path"; + private static final String APPIUM_FOLDER = "appium"; private static final String BIN_FOLDER = "bin"; private static final String APPIUM_JS = "appium.js"; private static final String APPIUM_NODE_MASK = File.separator + APPIUM_FOLDER + File.separator + BIN_FOLDER + File.separator + APPIUM_JS; - public static final String APPIUM_NODE_PROPERTY = "appium.node.path"; public static final String DEFAULT_LOCAL_IP_ADDRESS = "0.0.0.0"; private static final int DEFAULT_APPIUM_PORT = 4723; private final static String BASH = "bash"; - private final static String CMD_EXE[] = {"cmd.exe", "/C"}; + private final static String CMD_EXE = "cmd.exe"; private final static String NODE = "node"; - private final static String PATH_NAME = getPathVarName(); - final Map serverArguments = new HashMap<>(); private File appiumJS; private String ipAddress = DEFAULT_LOCAL_IP_ADDRESS; private File npmScript; + private File getNodeJSExecutable; //The first starting is slow sometimes on some //environment private long startupTimeout = 120; private TimeUnit timeUnit = TimeUnit.SECONDS; - private static String getPathVarName(){ - Map envVariables = System.getenv(); - Set keys = envVariables.keySet(); - - for (String key : keys) { - if (key.toUpperCase().trim().equals("Path".toUpperCase())) { - return key; - } - } - return null; - } - - private static String getTheLastStringFromsOutput(InputStream stream ) throws IOException { - BufferedReader reader = new BufferedReader( - new InputStreamReader(stream, Charset.forName("UTF-8"))); - String current; - String result = null; - while ((current = reader.readLine()) != null) { - if (StringUtils.isBlank(current)) { - continue; - } - result = current; - } - reader.close(); - return result; - } - - private static String readErrorStream(Process process) { - BufferedReader reader = new BufferedReader( - new InputStreamReader(process.getErrorStream(), Charset.forName("UTF-8"))); - String current; - String result = StringUtils.EMPTY; - try { - while ((current = reader.readLine()) != null) { - if (StringUtils.isBlank(current)) { - continue; - } - result = result + current + "\n"; - } - reader.close(); - } catch (IOException e) { - throw new RuntimeException(e); + private void setUpNPMScript(){ + if (npmScript != null) { + return; } - return result; - } - private static Process getSearchingProcess(String... command) throws Throwable { - ProcessBuilder processBuilder = new ProcessBuilder(command); - if (!StringUtils.isBlank(PATH_NAME)) { - String path = System.getenv().get(PATH_NAME); - processBuilder.environment().put(PATH_NAME, path); + if (!Platform.getCurrent().is(Platform.WINDOWS)) { + npmScript = Scripts.GET_PATH_TO_DEFAULT_NODE_UNIX.getScriptFile(); } - return processBuilder.start(); } - private void setUpNPMScript(){ - if (npmScript != null) { + private void setUpGetNodeJSExecutableScript() { + if (getNodeJSExecutable != null) { return; } - if (Platform.getCurrent().is(Platform.WINDOWS)) { - npmScript = NPMScript.GET_PATH_TO_DEFAULT_NODE_WIN.getScriptFile(); - } - else { - npmScript = NPMScript.GET_PATH_TO_DEFAULT_NODE_UNIX.getScriptFile(); - } + getNodeJSExecutable = Scripts.GET_NODE_JS_EXECUTABLE.getScriptFile(); } private File findNodeInCurrentFileSystem(){ setUpNPMScript(); String instancePath; - Process p; + CommandLine commandLine; try { if (Platform.getCurrent().is(Platform.WINDOWS)) { - p = getSearchingProcess(ArrayUtils.add(CMD_EXE, npmScript.getAbsolutePath())); - p.waitFor(); + commandLine = new CommandLine(CMD_EXE, "/C", "npm root -g"); } else { - p = getSearchingProcess(BASH, "-l", npmScript.getAbsolutePath()); + commandLine = new CommandLine(BASH, "-l", npmScript.getAbsolutePath()); } - instancePath = getTheLastStringFromsOutput(p.getInputStream()); + commandLine.execute(); } catch (Throwable e) { throw new RuntimeException(e); } + instancePath = (commandLine.getStdOut()).trim(); try { File result; if (StringUtils.isBlank(instancePath) || !(result = new File(instancePath + File.separator + APPIUM_NODE_MASK)).exists()) { - String errorOutput = readErrorStream(p); + String errorOutput = commandLine.getStdOut(); throw new InvalidServerInstanceException("There is no installed nodes! Please install " + " node via NPM (https://www.npmjs.com/package/appium#using-node-js) or download and " + "install Appium app (http://appium.io/downloads.html)", @@ -162,7 +112,7 @@ private File findNodeInCurrentFileSystem(){ return result; } finally { - p.destroy(); + commandLine.destroy(); } } @@ -186,41 +136,42 @@ public AppiumServiceBuilder() { @Override protected File findDefaultExecutable() { - Process p; + + String nodeJSExec = System.getProperty(NODE_JS_EXECUTABLE_PROPERTY); + if (!StringUtils.isBlank(nodeJSExec)) { + File result = new File(nodeJSExec); + if (result.exists()) { + return result; + } + } + + CommandLine commandLine; + setUpGetNodeJSExecutableScript(); try { if (Platform.getCurrent().is(Platform.WINDOWS)) { - p = getSearchingProcess(ArrayUtils.add(CMD_EXE, NODE)); + commandLine = new CommandLine(NODE + ".exe", getNodeJSExecutable.getAbsolutePath()); } else { - p = getSearchingProcess(BASH, "-l", "-c", NODE); + commandLine = new CommandLine(NODE, getNodeJSExecutable.getAbsolutePath()); } + commandLine.execute(); } catch (Throwable t) { throw new InvalidNodeJSInstance("Node.js is not installed!", t); } - String filePath; - try { - OutputStream outputStream = p.getOutputStream(); - PrintStream out = new PrintStream(outputStream) ; - out.println("console.log(process.execPath);") ; - out.close(); - filePath = getTheLastStringFromsOutput(p.getInputStream()); - } - catch (Throwable t){ - p.destroy(); - throw new RuntimeException(t); - } + + String filePath = (commandLine.getStdOut()).trim(); try { - if (StringUtils.isBlank(filePath)) { - String errorOutput = readErrorStream(p); + if (StringUtils.isBlank(filePath) || !new File(filePath).exists()) { + String errorOutput = commandLine.getStdOut(); String errorMessage = "Can't get a path to the default Node.js instance"; throw new InvalidNodeJSInstance(errorMessage, new IOException(errorOutput)); } return new File(filePath); } finally { - p.destroy(); + commandLine.destroy(); } } @@ -394,14 +345,19 @@ protected AppiumDriverLocalService createDriverService(File nodeJSExecutable, in } } - @Override - protected void finalize() throws Throwable { - if (npmScript != null) { + private static void disposeCachedFile(File file) { + if (file != null) { try { - FileUtils.forceDelete(npmScript); + FileUtils.forceDelete(file); } catch (IOException ignored) { } } + } + + @Override + protected void finalize() throws Throwable { + disposeCachedFile(npmScript); + disposeCachedFile(getNodeJSExecutable); super.finalize(); } -} +} \ No newline at end of file diff --git a/src/main/java/io/appium/java_client/service/local/NPMScript.java b/src/main/java/io/appium/java_client/service/local/Scripts.java similarity index 95% rename from src/main/java/io/appium/java_client/service/local/NPMScript.java rename to src/main/java/io/appium/java_client/service/local/Scripts.java index 1d9a46d2c..21376eea6 100644 --- a/src/main/java/io/appium/java_client/service/local/NPMScript.java +++ b/src/main/java/io/appium/java_client/service/local/Scripts.java @@ -19,14 +19,14 @@ import org.apache.commons.io.IOUtils; import java.io.*; -enum NPMScript { - GET_PATH_TO_DEFAULT_NODE_WIN("get_path_to_default_node.cmd"), - GET_PATH_TO_DEFAULT_NODE_UNIX("get_path_to_default_node.sh") +enum Scripts { + GET_PATH_TO_DEFAULT_NODE_UNIX("get_path_to_default_node.sh"), + GET_NODE_JS_EXECUTABLE("getExe.js") ; private static final String RESOURCE_FOLDER = "/scripts/"; private final String script; - NPMScript(String script) { + Scripts(String script) { this.script = script; } diff --git a/src/main/resources/scripts/getExe.js b/src/main/resources/scripts/getExe.js new file mode 100644 index 000000000..15cfdb975 --- /dev/null +++ b/src/main/resources/scripts/getExe.js @@ -0,0 +1 @@ +console.log(process.execPath); \ No newline at end of file diff --git a/src/main/resources/scripts/get_path_to_default_node.cmd b/src/main/resources/scripts/get_path_to_default_node.cmd deleted file mode 100644 index 2a48ab846..000000000 --- a/src/main/resources/scripts/get_path_to_default_node.cmd +++ /dev/null @@ -1 +0,0 @@ -npm root -g \ No newline at end of file From 1d71d5ed13b58ed7491db47ba89e4114b32f8261 Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Tue, 8 Dec 2015 23:51:15 +0300 Subject: [PATCH 8/9] #272 fix: improvement of the environment variable extracting --- .../java_client/service/local/AppiumServiceBuilder.java | 8 +++++++- 1 file changed, 7 insertions(+), 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 66cf81abe..a909eb082 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 @@ -138,6 +138,9 @@ public AppiumServiceBuilder() { protected File findDefaultExecutable() { String nodeJSExec = System.getProperty(NODE_JS_EXECUTABLE_PROPERTY); + if (StringUtils.isBlank(nodeJSExec)) { + nodeJSExec = System.getenv(NODE_JS_EXECUTABLE_PROPERTY); + } if (!StringUtils.isBlank(nodeJSExec)) { File result = new File(nodeJSExec); if (result.exists()) { @@ -231,7 +234,10 @@ void checkAppiumJS(){ } String appiumJS = System.getProperty(APPIUM_NODE_PROPERTY); - if (appiumJS !=null){ + if (StringUtils.isBlank(appiumJS)) { + appiumJS = System.getenv(APPIUM_NODE_PROPERTY); + } + if (!StringUtils.isBlank(appiumJS)){ File node = new File(appiumJS); validateNodeStructure(node); this.appiumJS = node; From 7cc7e9e52f020c9bb623ca9cf99e19e2a82746ba Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Thu, 10 Dec 2015 22:31:23 +0300 Subject: [PATCH 9/9] #272 fix: Property name was changed --- .../java_client/service/local/AppiumServiceBuilder.java | 6 +++--- 1 file changed, 3 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 a909eb082..91697db1c 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 @@ -37,7 +37,7 @@ public final class AppiumServiceBuilder extends DriverService.Builder { public static final String APPIUM_NODE_PROPERTY = "appium.node.path"; - public static final String NODE_JS_EXECUTABLE_PROPERTY = "appium.node.js.exec.path"; + public static final String APPIUM_NODE_JS_EXEC_PATH = "appium.node.js.exec.path"; private static final String APPIUM_FOLDER = "appium"; private static final String BIN_FOLDER = "bin"; @@ -137,9 +137,9 @@ public AppiumServiceBuilder() { @Override protected File findDefaultExecutable() { - String nodeJSExec = System.getProperty(NODE_JS_EXECUTABLE_PROPERTY); + String nodeJSExec = System.getProperty(APPIUM_NODE_JS_EXEC_PATH); if (StringUtils.isBlank(nodeJSExec)) { - nodeJSExec = System.getenv(NODE_JS_EXECUTABLE_PROPERTY); + nodeJSExec = System.getenv(APPIUM_NODE_JS_EXEC_PATH); } if (!StringUtils.isBlank(nodeJSExec)) { File result = new File(nodeJSExec);