How to Run Appium from Terminal on Remote OSX through SSH.NET in C# on Windows

I am attempting to connect to a Mac through SSH to launch an instance of Appium Server when I run my C# automation tests with Visual Studio. I am attempting to use SSH.NET to implement this.

This is where I call to do the commands on the Mac:

string appiumCommand = @" /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 127.0.0.1 --port 4723 --command-timeout "7200" --debug-log-spacing --platform-version "9.2" --platform-name "iOS" --show-ios-log --default-device --native-instruments-lib";
Helper.RunSSH("192.168.1.8", "Testing Practice", "MyPasswordHere", appiumCommand);
Uri iOSServerUri = new Uri("http://192.168.1.8:4723/wd/hub");
driver = new IOSDriver<IWebElement>(iOSServerUri, Capabilities.GetiOS(), TimeSpan.FromSeconds(180));

This is where the SSH is handled:

using System;
using Renci.SshNet;
using Renci.SshNet.Common;

namespace Framework {
    public static class Helper {
        static void HandleKeyEvent(Object sender, AuthenticationPromptEventArgs e) {
            foreach (AuthenticationPrompt prompt in e.Prompts) {
                if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1) {
                    prompt.Response = "MyPasswordHere"; //TODO: Pass in the password variable
                }
            }
        }

        public static void RunSSH(string url, string username, string password, string appiumCommand) {
            KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(username);
            PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(username, password);

            kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(HandleKeyEvent);

            ConnectionInfo connectionInfo = new ConnectionInfo(url, 22, username, pauth, kauth);

            var sshClient = new SshClient(connectionInfo);
            sshClient.Connect();
            using (var cmd = sshClient.CreateCommand(appiumCommand)) {
                cmd.Execute();
                Console.WriteLine("Command>" + cmd.CommandText);
                Console.WriteLine("Return Value = {0}", cmd.ExitStatus);
            }
            sshClient.Disconnect();
        }
    }
}

When I run this code I get this in my Output window in Visual Studio:

Command> /Applications/Appium.app/Contents/Resources/node/bin/node
/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js
–args --address 127.0.0.1 --port 4723 --full-reset --command-timeout ‘7200’ --debug-log-spacing --platform-version ‘9.2’ --platform-name
‘iOS’ --show-ios-log --default-device --native-instruments-lib Return
Value = 2

However, the Appium Server doesn’t actually start on the Mac so my test fails.

Right now I am attempting to even run the Appium Server from the Terminal on the Mac itself. So far I’ve had no luck and come to this:

Automation-MacBook-Pro:~ testingpractice$ '/Applications/Appium.app/Contents/Resources/node/bin/node'
> /Applications/Appium.app/Contents/Resources/node_modules/appium
SyntaxError: Invalid flags supplied to RegExp constructor 'Appium'
    at new RegExp (native)
    at repl:1:1
    at REPLServer.defaultEval (repl.js:132:27)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:279:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:214:10)
    at REPLServer.Interface._line (readline.js:553:8)
    at REPLServer.Interface._ttyWrite (readline.js:830:14)
> /Applications/Appium.app/Contents/Resources/node_modules/appium --address 127.0.0.1 --port 4723 --command-timeout "7200" --debug-log-spacing --platform-version "9.2" --platform-name "iOS" --show-ios-log --default-device --native-instruments-lib
SyntaxError: Unexpected identifier
    at Object.exports.createScript (vm.js:44:10)
    at REPLServer.defaultEval (repl.js:117:23)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:279:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:214:10)
    at REPLServer.Interface._line (readline.js:553:8)
    at REPLServer.Interface._ttyWrite (readline.js:830:14)
    at ReadStream.onkeypress (readline.js:109:10)

I’m pretty stuck on where to move from here…

If I comment out the first 2 lines of code:

string appiumCommand = @" /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 127.0.0.1 --port 4723 --command-timeout "7200" --debug-log-spacing --platform-version "9.2" --platform-name "iOS" --show-ios-log --default-device --native-instruments-lib";
Helper.RunSSH("192.168.1.8", "Testing Practice", "MyPasswordHere", appiumCommand);

and just manually launch Appium through the GUI on the Mac my tests run just fine. However, I’ve been tasked with figuring out how to run everything though the command line so no manual intervention is required other than making sure the Mac is powered on. I’ll be moving the Mac to a static IP once I get this working so that won’t be a factor.

I’m also going to have to figure out how to shut down the Appium Server once my test finishes running.

Any nods in the right direction would be greatly appreciated!

My setup on the Mac is:

  • OS X El Capitan Version 10.11.3
  • MacBook Pro (Retina, 15-inch)
  • 2.5GHz Intel Core i7
  • 16GB DDR3
  • AMD Radeon R9 M370X 2048MB
  • Xcode 7.2.1 (7C1002) with Apple Developer License (with all available Simulators downloaded)
  • Appium 1.4.13 (Draco) GUI installed from dmg downloaded from Appium.io.