diff --git a/BrowserStackLocal/BrowserStackLocal Unit Tests/BrowserStackTunnelTests.cs b/BrowserStackLocal/BrowserStackLocal Unit Tests/BrowserStackTunnelTests.cs index dd88bb6..2a32216 100644 --- a/BrowserStackLocal/BrowserStackLocal Unit Tests/BrowserStackTunnelTests.cs +++ b/BrowserStackLocal/BrowserStackLocal Unit Tests/BrowserStackTunnelTests.cs @@ -13,6 +13,11 @@ namespace BrowserStack_Unit_Tests [TestClass] public class BrowserStackTunnelTests { + static readonly OperatingSystem os = Environment.OSVersion; + static readonly string homepath = os.Platform.ToString() == "Unix" ? + Environment.GetFolderPath(Environment.SpecialFolder.Personal) : + Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); + static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal.exe"; private TunnelClass tunnel; [TestMethod] public void TestInitialState() @@ -33,8 +38,8 @@ public void TestBinaryPathOnNull() { tunnel = new TunnelClass(); tunnel.addBinaryPath(null); - string expectedPath = Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack"); - expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe"); + string expectedPath = Path.Combine(homepath, ".browserstack"); + expectedPath = Path.Combine(expectedPath, binaryName); Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); } [TestMethod] @@ -42,8 +47,8 @@ public void TestBinaryPathOnEmpty() { tunnel = new TunnelClass(); tunnel.addBinaryPath(""); - string expectedPath = Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack"); - expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe"); + string expectedPath = Path.Combine(homepath, ".browserstack"); + expectedPath = Path.Combine(expectedPath, binaryName); Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); } [TestMethod] @@ -55,18 +60,18 @@ public void TestBinaryPathOnFallback() Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); tunnel.fallbackPaths(); - expectedPath = Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack"); - expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe"); + expectedPath = Path.Combine(homepath, ".browserstack"); + expectedPath = Path.Combine(expectedPath, binaryName); Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); tunnel.fallbackPaths(); expectedPath = Directory.GetCurrentDirectory(); - expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe"); + expectedPath = Path.Combine(expectedPath, binaryName); Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); tunnel.fallbackPaths(); expectedPath = Path.GetTempPath(); - expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe"); + expectedPath = Path.Combine(expectedPath, binaryName); Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); } [TestMethod] @@ -79,7 +84,7 @@ public void TestBinaryPathOnNoMoreFallback() tunnel.fallbackPaths(); Assert.Throws(typeof(Exception), new TestDelegate(testFallbackException), - "Binary not found or failed to launch. Make sure that BrowserStackLocal.exe is not already running." + "Binary not found or failed to launch. Make sure that BrowserStackLocal is not already running." ); } [TestMethod] diff --git a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs index 05f499b..fa319e7 100644 --- a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs +++ b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs @@ -14,10 +14,15 @@ public enum LocalState { Idle, Connecting, Connected, Error, Disconnected }; public class BrowserStackTunnel : IDisposable { - static readonly string binaryName = "BrowserStackLocal.exe"; - static readonly string downloadURL = "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal.exe"; + static readonly string binaryName = isDarwin() ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal.exe"; + static readonly string downloadURL = isDarwin() ? + "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-darwin-x64" : + "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal.exe"; + static readonly string homepath = isDarwin() ? + Environment.GetFolderPath(Environment.SpecialFolder.Personal) : + Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); public static readonly string[] basePaths = new string[] { - Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack"), + Path.Combine(homepath, ".browserstack"), Directory.GetCurrentDirectory(), Path.GetTempPath() }; @@ -32,6 +37,12 @@ public class BrowserStackTunnel : IDisposable Process process = null; + static Boolean isDarwin() + { + OperatingSystem os = Environment.OSVersion; + return os.Platform.ToString() == "Unix"; + } + public virtual void addBinaryPath(string binaryAbsolute) { if (binaryAbsolute == null || binaryAbsolute.Trim().Length == 0) @@ -60,11 +71,37 @@ public virtual void fallbackPaths() { if (basePathsIndex >= basePaths.Length - 1) { - throw new Exception("Binary not found or failed to launch. Make sure that BrowserStackLocal.exe is not already running."); + throw new Exception("Binary not found or failed to launch. Make sure that BrowserStackLocal is not already running."); } basePathsIndex++; binaryAbsolute = Path.Combine(basePaths[basePathsIndex], binaryName); } + + public void modifyBinaryPermission() + { + if (isDarwin()) + { + try + { + using (Process proc = Process.Start("/bin/bash", $"-c \"chmod 0755 {this.binaryAbsolute}\"")) + { + proc.WaitForExit(); + } + } + catch + { + throw new Exception("Error in changing permission for file " + this.binaryAbsolute); + } + } + else + { + DirectoryInfo dInfo = new DirectoryInfo(binaryAbsolute); + DirectorySecurity dSecurity = dInfo.GetAccessControl(); + dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); + dInfo.SetAccessControl(dSecurity); + } + } + public void downloadBinary() { string binaryDirectory = Path.Combine(this.binaryAbsolute, ".."); @@ -82,10 +119,7 @@ public void downloadBinary() throw new Exception("Error accessing file " + binaryAbsolute); } - DirectoryInfo dInfo = new DirectoryInfo(binaryAbsolute); - DirectorySecurity dSecurity = dInfo.GetAccessControl(); - dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); - dInfo.SetAccessControl(dSecurity); + modifyBinaryPermission(); } public virtual void Run(string accessKey, string folder, string logFilePath, string processType) @@ -158,6 +192,7 @@ private void RunProcess(string arguments, string processType) else if (connectionState.ToString().ToLower().Equals("disconnected")) { SetTunnelState(LocalState.Disconnected); + throw new Exception("Error while executing BrowserStackLocal " + processType + " " + e.Data); } else { diff --git a/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs b/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs index e2d6c81..c0b1e39 100644 --- a/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs +++ b/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs @@ -11,6 +11,8 @@ namespace BrowserStackLocalIntegrationTests { public class IntegrationTests { + static readonly OperatingSystem os = Environment.OSVersion; + static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal"; private static string username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME"); private static string accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY"); @@ -18,8 +20,6 @@ public class IntegrationTests new KeyValuePair("key", accesskey), new KeyValuePair("verbose", "true"), new KeyValuePair("forcelocal", "true"), - new KeyValuePair("binarypath", "C:\\Users\\Admin\\Desktop\\BrowserStackLocal.exe"), - new KeyValuePair("logfile", "C:\\Users\\Admin\\Desktop\\local.log"), }; [Test] @@ -33,7 +33,7 @@ void startWithOptions() } Assert.DoesNotThrow(new TestDelegate(startWithOptions)); - Process[] binaryInstances = Process.GetProcessesByName("BrowserStackLocal"); + Process[] binaryInstances = Process.GetProcessesByName(binaryName); Assert.AreNotEqual(binaryInstances.Length, 0); IWebDriver driver; @@ -54,7 +54,7 @@ void startWithOptions() driver.Quit(); local.stop(); - binaryInstances = Process.GetProcessesByName("BrowserStackLocal"); + binaryInstances = Process.GetProcessesByName(binaryName); Assert.AreEqual(binaryInstances.Length, 0); } @@ -76,7 +76,7 @@ public void TestBinaryState() [TearDown] public void TestCleanup() { - foreach(Process p in Process.GetProcessesByName("BrowserStackLocal")) + foreach(Process p in Process.GetProcessesByName(binaryName)) { p.Kill(); }