From ad72b1ed210a350d3d30b2b6f1ada0cf99feed4a Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Tue, 3 Oct 2023 15:00:12 +0300 Subject: [PATCH 01/32] fix: Update WindowsNode path to compile with appium 2 refactor: Move Appium.js path retrieval to helpers.paths class --- .../AppiumLocalServerLaunchingTest.cs | 27 +------ test/integration/helpers/PathToWindowsNode | 2 +- test/integration/helpers/Paths.cs | 73 +++++++++++++++++++ 3 files changed, 77 insertions(+), 25 deletions(-) create mode 100644 test/integration/helpers/Paths.cs diff --git a/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs b/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs index 14bb79a3..5f13841e 100644 --- a/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs +++ b/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs @@ -4,9 +4,8 @@ using System.Net; using System.Text; using System.Threading; -using Appium.Net.Integration.Tests.Properties; +using Appium.Net.Integration.Tests.Helpers; using NUnit.Framework; -using OpenQA.Selenium; using OpenQA.Selenium.Appium; using OpenQA.Selenium.Appium.Enums; using OpenQA.Selenium.Appium.Service; @@ -24,9 +23,6 @@ public class AppiumLocalServerLaunchingTest [OneTimeSetUp] public void BeforeAll() { - var isWindows = Platform.CurrentPlatform.IsPlatformType(PlatformType.Windows); - var isMacOs = Platform.CurrentPlatform.IsPlatformType(PlatformType.Mac); - var isLinux = Platform.CurrentPlatform.IsPlatformType(PlatformType.Linux); IPHostEntry host; var hostName = Dns.GetHostName(); @@ -41,25 +37,8 @@ public void BeforeAll() } Console.WriteLine(_testIp); - byte[] bytes; - if (isWindows) - { - bytes = Resources.PathToWindowsNode; - _pathToCustomizedAppiumJs = Encoding.UTF8.GetString(bytes); - return; - } - if (isMacOs) - { - bytes = Resources.PathToMacOSNode; - _pathToCustomizedAppiumJs = Encoding.UTF8.GetString(bytes); - return; - } - if (isLinux) - { - bytes = Resources.PathToLinuxNode; - _pathToCustomizedAppiumJs = Encoding.UTF8.GetString(bytes); - return; - } + Paths paths = new Paths(); + _pathToCustomizedAppiumJs = paths.PathToCustomizedAppiumJs; } [Test] diff --git a/test/integration/helpers/PathToWindowsNode b/test/integration/helpers/PathToWindowsNode index ed0acb16..0dd72b24 100644 --- a/test/integration/helpers/PathToWindowsNode +++ b/test/integration/helpers/PathToWindowsNode @@ -1 +1 @@ -C:/Program Files (x86)/Appium/node_modules/appium/bin/appium.js \ No newline at end of file +%USERPROFILE%/AppData/Roaming/npm/node_modules/appium/lib/appium.js \ No newline at end of file diff --git a/test/integration/helpers/Paths.cs b/test/integration/helpers/Paths.cs new file mode 100644 index 00000000..96af614a --- /dev/null +++ b/test/integration/helpers/Paths.cs @@ -0,0 +1,73 @@ +using Appium.Net.Integration.Tests.Properties; +using OpenQA.Selenium; +using System; +using System.Text; + +namespace Appium.Net.Integration.Tests.Helpers +{ + internal class Paths + { + private bool _isWindows; + private bool _isMacOs; + private bool _isLinux; + private string _pathToCustomizedAppiumJs; + + public Paths() + { + // Detect the platform during object creation. + _isWindows = Platform.CurrentPlatform.IsPlatformType(PlatformType.Windows); + _isMacOs = Platform.CurrentPlatform.IsPlatformType(PlatformType.Mac); + _isLinux = Platform.CurrentPlatform.IsPlatformType(PlatformType.Linux); + } + + public string PathToCustomizedAppiumJs + { + get + { + if (_pathToCustomizedAppiumJs == null) + { + GetAppiumJsPath(); + } + return _pathToCustomizedAppiumJs; + } + } + + private void GetAppiumJsPath() + { + byte[] bytes; + string appiumJsPath = null; + + if (_isWindows) + { + bytes = Resources.PathToWindowsNode; + appiumJsPath = Encoding.UTF8.GetString(bytes); + } + else if (_isMacOs) + { + bytes = Resources.PathToMacOSNode; + appiumJsPath = Encoding.UTF8.GetString(bytes); + } + else if (_isLinux) + { + bytes = Resources.PathToLinuxNode; + appiumJsPath = Encoding.UTF8.GetString(bytes); + } + else + { + // Handle unsupported platform + throw new PlatformNotSupportedException("Unsupported platform"); + } + + if (appiumJsPath.Contains("%USERPROFILE%")) + { + string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + userProfile = userProfile.Replace("\\", "/"); + _pathToCustomizedAppiumJs = appiumJsPath.Replace("%USERPROFILE%", userProfile); + } + else + { + _pathToCustomizedAppiumJs = appiumJsPath; + } + } + } +} From 0a1df1b57f4c3d3ce0d40632d08779b75a51e001 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Wed, 4 Oct 2023 14:00:46 +0300 Subject: [PATCH 02/32] refactor!: Retrieve npm prefix path and use cross-platform commands to get appium script --- .../Properties/Resources.Designer.cs | 33 ++++++--- test/integration/Properties/Resources.resx | 3 + test/integration/helpers/Npm.cs | 70 +++++++++++++++++++ test/integration/helpers/PathToNode | 1 + test/integration/helpers/PathToWindowsNode | 2 +- test/integration/helpers/Paths.cs | 55 +++------------ 6 files changed, 109 insertions(+), 55 deletions(-) create mode 100644 test/integration/helpers/Npm.cs create mode 100644 test/integration/helpers/PathToNode diff --git a/test/integration/Properties/Resources.Designer.cs b/test/integration/Properties/Resources.Designer.cs index 400c7e3e..7e16699a 100644 --- a/test/integration/Properties/Resources.Designer.cs +++ b/test/integration/Properties/Resources.Designer.cs @@ -8,10 +8,10 @@ // //------------------------------------------------------------------------------ -namespace Appium.Net.Integration.Tests.Properties -{ - - +namespace Appium.Net.Integration.Tests.Properties { + using System; + + /// /// A strongly-typed resource class, for looking up localized strings, etc. /// @@ -19,7 +19,7 @@ namespace Appium.Net.Integration.Tests.Properties // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] public class Resources { @@ -69,37 +69,52 @@ public static byte[] ApiDemos_debug { return ((byte[])(obj)); } } - + /// /// Looks up a localized resource of type System.Byte[]. /// + [Obsolete] public static byte[] PathToLinuxNode { get { object obj = ResourceManager.GetObject("PathToLinuxNode", resourceCulture); return ((byte[])(obj)); } } - + /// /// Looks up a localized resource of type System.Byte[]. /// + [Obsolete] public static byte[] PathToMacOSNode { get { object obj = ResourceManager.GetObject("PathToMacOSNode", resourceCulture); return ((byte[])(obj)); } } - + /// /// Looks up a localized resource of type System.Byte[]. /// + [Obsolete] public static byte[] PathToWindowsNode { get { object obj = ResourceManager.GetObject("PathToWindowsNode", resourceCulture); return ((byte[])(obj)); } } - + + /// + /// Looks up a localized resource of type System.Byte[]. + /// + public static byte[] PathToNode + { + get + { + object obj = ResourceManager.GetObject("PathToNode", resourceCulture); + return ((byte[])(obj)); + } + } + /// /// Looks up a localized resource of type System.Byte[]. /// diff --git a/test/integration/Properties/Resources.resx b/test/integration/Properties/Resources.resx index f6ac0653..72884773 100644 --- a/test/integration/Properties/Resources.resx +++ b/test/integration/Properties/Resources.resx @@ -142,4 +142,7 @@ ..\apps\archives\WebViewApp.app.zip;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + ..\helpers\PathToNode;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + \ No newline at end of file diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs new file mode 100644 index 00000000..41daab7d --- /dev/null +++ b/test/integration/helpers/Npm.cs @@ -0,0 +1,70 @@ +using OpenQA.Selenium; +using System; +using System.Diagnostics; +using System.Linq; + +namespace Appium.Net.Integration.Tests.helpers +{ + internal class Npm + { + static private Platform envPlatform; + + public static string GetNpmPrefixPath() + { + envPlatform = Platform.CurrentPlatform; + string npmPath = GetNpmExecutablePath(); + string result = RunCommand(npmPath, "config get prefix"); + + result = result.Trim(); + + return result; + } + + private static string RunCommand(string command, string arguments) + { + try + { + Process process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = command, + Arguments = arguments, + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + + process.Start(); + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + return output; + } + catch (Exception ex) + { + Console.WriteLine($"Error executing command: {ex.Message}"); + return null; + } + } + + private static string GetNpmExecutablePath() + { + string npmPath; + string command = envPlatform.IsPlatformType(PlatformType.Unix) || envPlatform.IsPlatformType(PlatformType.Mac) ? "which" : "where"; + string result = RunCommand(command, "npm"); + + string[] lines = result?.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); + if (envPlatform.IsPlatformType(PlatformType.Windows)) + { + npmPath = lines?.FirstOrDefault(line => line.EndsWith("npm.cmd")); + } + else + { + npmPath = lines?.FirstOrDefault(line => line.EndsWith("npm")); + } + return npmPath; + } + } +} diff --git a/test/integration/helpers/PathToNode b/test/integration/helpers/PathToNode new file mode 100644 index 00000000..1285ad93 --- /dev/null +++ b/test/integration/helpers/PathToNode @@ -0,0 +1 @@ +node_modules\appium\index.js \ No newline at end of file diff --git a/test/integration/helpers/PathToWindowsNode b/test/integration/helpers/PathToWindowsNode index 0dd72b24..1285ad93 100644 --- a/test/integration/helpers/PathToWindowsNode +++ b/test/integration/helpers/PathToWindowsNode @@ -1 +1 @@ -%USERPROFILE%/AppData/Roaming/npm/node_modules/appium/lib/appium.js \ No newline at end of file +node_modules\appium\index.js \ No newline at end of file diff --git a/test/integration/helpers/Paths.cs b/test/integration/helpers/Paths.cs index 96af614a..c16294d7 100644 --- a/test/integration/helpers/Paths.cs +++ b/test/integration/helpers/Paths.cs @@ -1,25 +1,14 @@ -using Appium.Net.Integration.Tests.Properties; -using OpenQA.Selenium; -using System; +using Appium.Net.Integration.Tests.helpers; +using Appium.Net.Integration.Tests.Properties; +using System.IO; using System.Text; namespace Appium.Net.Integration.Tests.Helpers { internal class Paths { - private bool _isWindows; - private bool _isMacOs; - private bool _isLinux; private string _pathToCustomizedAppiumJs; - public Paths() - { - // Detect the platform during object creation. - _isWindows = Platform.CurrentPlatform.IsPlatformType(PlatformType.Windows); - _isMacOs = Platform.CurrentPlatform.IsPlatformType(PlatformType.Mac); - _isLinux = Platform.CurrentPlatform.IsPlatformType(PlatformType.Linux); - } - public string PathToCustomizedAppiumJs { get @@ -35,39 +24,15 @@ public string PathToCustomizedAppiumJs private void GetAppiumJsPath() { byte[] bytes; - string appiumJsPath = null; + string appiumJsPath; + string npmPath; + + bytes = Resources.PathToNode; + appiumJsPath = Encoding.UTF8.GetString(bytes); - if (_isWindows) - { - bytes = Resources.PathToWindowsNode; - appiumJsPath = Encoding.UTF8.GetString(bytes); - } - else if (_isMacOs) - { - bytes = Resources.PathToMacOSNode; - appiumJsPath = Encoding.UTF8.GetString(bytes); - } - else if (_isLinux) - { - bytes = Resources.PathToLinuxNode; - appiumJsPath = Encoding.UTF8.GetString(bytes); - } - else - { - // Handle unsupported platform - throw new PlatformNotSupportedException("Unsupported platform"); - } + npmPath = Npm.GetNpmPrefixPath(); + _pathToCustomizedAppiumJs = Path.Combine(npmPath, appiumJsPath); - if (appiumJsPath.Contains("%USERPROFILE%")) - { - string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - userProfile = userProfile.Replace("\\", "/"); - _pathToCustomizedAppiumJs = appiumJsPath.Replace("%USERPROFILE%", userProfile); - } - else - { - _pathToCustomizedAppiumJs = appiumJsPath; - } } } } From 7665d5188cbad4d34c2576b13053cdd66904dfe4 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 14:01:35 +0300 Subject: [PATCH 03/32] fix: npm install prefix and npm install path mixup --- test/integration/helpers/Npm.cs | 34 ++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 41daab7d..33dc5276 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -12,12 +12,20 @@ internal class Npm public static string GetNpmPrefixPath() { envPlatform = Platform.CurrentPlatform; - string npmPath = GetNpmExecutablePath(); - string result = RunCommand(npmPath, "config get prefix"); - - result = result.Trim(); + string npmPath; + if (envPlatform.IsPlatformType(PlatformType.Windows)) + { + npmPath = GetNpmExecutablePath(); + } + else + { + npmPath = "npm"; + } + string result = RunCommand(npmPath, "list -g --depth=0"); + string[] lines = result?.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); + string npmPrefixPath = lines[0]; - return result; + return npmPrefixPath; } private static string RunCommand(string command, string arguments) @@ -51,19 +59,15 @@ private static string RunCommand(string command, string arguments) private static string GetNpmExecutablePath() { + string result = RunCommand("where", "npm"); string npmPath; - string command = envPlatform.IsPlatformType(PlatformType.Unix) || envPlatform.IsPlatformType(PlatformType.Mac) ? "which" : "where"; - string result = RunCommand(command, "npm"); + /*string command = envPlatform.IsPlatformType(PlatformType.Unix) || envPlatform.IsPlatformType(PlatformType.Mac) ? "which" : "where"; + string result = RunCommand(command, "npm");*/ string[] lines = result?.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); - if (envPlatform.IsPlatformType(PlatformType.Windows)) - { - npmPath = lines?.FirstOrDefault(line => line.EndsWith("npm.cmd")); - } - else - { - npmPath = lines?.FirstOrDefault(line => line.EndsWith("npm")); - } + + npmPath = lines?.FirstOrDefault(line => line.EndsWith("npm.cmd")); + return npmPath; } } From aee59f447dbe2ec122164619d50279d340a7e5f5 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 14:12:01 +0300 Subject: [PATCH 04/32] fix: make PathToNode compatible with both Windows and Unix-based systems --- test/integration/helpers/PathToNode | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/helpers/PathToNode b/test/integration/helpers/PathToNode index 1285ad93..96133d35 100644 --- a/test/integration/helpers/PathToNode +++ b/test/integration/helpers/PathToNode @@ -1 +1 @@ -node_modules\appium\index.js \ No newline at end of file +node_modules/appium/index.js \ No newline at end of file From 8e862d2c5c578661a499c12226015e7e8ce2ab0d Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 14:24:14 +0300 Subject: [PATCH 05/32] chore: Remove redundant NodePaths from the resources --- .../Properties/Resources.Designer.cs | 41 ++----------------- test/integration/Properties/Resources.resx | 9 ---- test/integration/helpers/PathToLinuxNode | 1 - test/integration/helpers/PathToMacOSNode | 1 - test/integration/helpers/PathToWindowsNode | 1 - 5 files changed, 3 insertions(+), 50 deletions(-) delete mode 100644 test/integration/helpers/PathToLinuxNode delete mode 100644 test/integration/helpers/PathToMacOSNode delete mode 100644 test/integration/helpers/PathToWindowsNode diff --git a/test/integration/Properties/Resources.Designer.cs b/test/integration/Properties/Resources.Designer.cs index 7e16699a..c2f27df5 100644 --- a/test/integration/Properties/Resources.Designer.cs +++ b/test/integration/Properties/Resources.Designer.cs @@ -69,52 +69,17 @@ public static byte[] ApiDemos_debug { return ((byte[])(obj)); } } - - /// - /// Looks up a localized resource of type System.Byte[]. - /// - [Obsolete] - public static byte[] PathToLinuxNode { - get { - object obj = ResourceManager.GetObject("PathToLinuxNode", resourceCulture); - return ((byte[])(obj)); - } - } - - /// - /// Looks up a localized resource of type System.Byte[]. - /// - [Obsolete] - public static byte[] PathToMacOSNode { - get { - object obj = ResourceManager.GetObject("PathToMacOSNode", resourceCulture); - return ((byte[])(obj)); - } - } - + /// /// Looks up a localized resource of type System.Byte[]. /// - [Obsolete] - public static byte[] PathToWindowsNode { + public static byte[] PathToNode { get { - object obj = ResourceManager.GetObject("PathToWindowsNode", resourceCulture); - return ((byte[])(obj)); - } - } - - /// - /// Looks up a localized resource of type System.Byte[]. - /// - public static byte[] PathToNode - { - get - { object obj = ResourceManager.GetObject("PathToNode", resourceCulture); return ((byte[])(obj)); } } - + /// /// Looks up a localized resource of type System.Byte[]. /// diff --git a/test/integration/Properties/Resources.resx b/test/integration/Properties/Resources.resx index 72884773..a61993f2 100644 --- a/test/integration/Properties/Resources.resx +++ b/test/integration/Properties/Resources.resx @@ -118,15 +118,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - ..\helpers\PathToLinuxNode;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - ..\helpers\PathToMacOSNode;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - ..\helpers\PathToWindowsNode;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - ..\apps\ApiDemos-debug.apk;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 diff --git a/test/integration/helpers/PathToLinuxNode b/test/integration/helpers/PathToLinuxNode deleted file mode 100644 index 1932de20..00000000 --- a/test/integration/helpers/PathToLinuxNode +++ /dev/null @@ -1 +0,0 @@ -/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js \ No newline at end of file diff --git a/test/integration/helpers/PathToMacOSNode b/test/integration/helpers/PathToMacOSNode deleted file mode 100644 index 1932de20..00000000 --- a/test/integration/helpers/PathToMacOSNode +++ /dev/null @@ -1 +0,0 @@ -/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js \ No newline at end of file diff --git a/test/integration/helpers/PathToWindowsNode b/test/integration/helpers/PathToWindowsNode deleted file mode 100644 index 1285ad93..00000000 --- a/test/integration/helpers/PathToWindowsNode +++ /dev/null @@ -1 +0,0 @@ -node_modules\appium\index.js \ No newline at end of file From ad2289d1a282f7ced3abce898cc28d090394c7b8 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 14:30:23 +0300 Subject: [PATCH 06/32] chore: Remove comment section --- test/integration/helpers/Npm.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 33dc5276..c1f396eb 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -61,8 +61,6 @@ private static string GetNpmExecutablePath() { string result = RunCommand("where", "npm"); string npmPath; - /*string command = envPlatform.IsPlatformType(PlatformType.Unix) || envPlatform.IsPlatformType(PlatformType.Mac) ? "which" : "where"; - string result = RunCommand(command, "npm");*/ string[] lines = result?.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); From 9ca328d4fdfc2f791bb09efe621315ad34c69d33 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 14:35:07 +0300 Subject: [PATCH 07/32] chore: Add exception if npm path cannot be detected --- test/integration/helpers/Npm.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index c1f396eb..38afe198 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -66,6 +66,11 @@ private static string GetNpmExecutablePath() npmPath = lines?.FirstOrDefault(line => line.EndsWith("npm.cmd")); + if (string.IsNullOrWhiteSpace(npmPath)) + { + throw new ApplicationException("NPM path not found."); + } + return npmPath; } } From aef7d0217be396ac78b8c7305419e96eeec1d3c1 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 14:43:13 +0300 Subject: [PATCH 08/32] chore: Rename _pathToCustomizedAppiumJs --- .../ServerTests/AppiumLocalServerLaunchingTest.cs | 8 ++++---- test/integration/helpers/Paths.cs | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs b/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs index 5f13841e..bc736c3a 100644 --- a/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs +++ b/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs @@ -17,7 +17,7 @@ namespace Appium.Net.Integration.Tests.ServerTests [TestFixture] public class AppiumLocalServerLaunchingTest { - private string _pathToCustomizedAppiumJs; + private string _pathToAppiumPackageIndex; private string _testIp; [OneTimeSetUp] @@ -38,7 +38,7 @@ public void BeforeAll() Console.WriteLine(_testIp); Paths paths = new Paths(); - _pathToCustomizedAppiumJs = paths.PathToCustomizedAppiumJs; + _pathToAppiumPackageIndex = paths.PathToAppiumPackageIndex; } [Test] @@ -84,7 +84,7 @@ public void CheckAbilityToBuildServiceUsingNodeDefinedInProperties() AppiumLocalService service = null; try { - var definedNode = _pathToCustomizedAppiumJs; + var definedNode = _pathToAppiumPackageIndex; Environment.SetEnvironmentVariable(AppiumServiceConstants.AppiumBinaryPath, definedNode); service = AppiumLocalService.BuildDefaultService(); service.Start(); @@ -103,7 +103,7 @@ public void CheckAbilityToBuildServiceUsingNodeDefinedExplicitly() AppiumLocalService service = null; try { - service = new AppiumServiceBuilder().WithAppiumJS(new FileInfo(_pathToCustomizedAppiumJs)).Build(); + service = new AppiumServiceBuilder().WithAppiumJS(new FileInfo(_pathToAppiumPackageIndex)).Build(); service.Start(); Assert.AreEqual(true, service.IsRunning); } diff --git a/test/integration/helpers/Paths.cs b/test/integration/helpers/Paths.cs index c16294d7..96d0c57c 100644 --- a/test/integration/helpers/Paths.cs +++ b/test/integration/helpers/Paths.cs @@ -7,17 +7,17 @@ namespace Appium.Net.Integration.Tests.Helpers { internal class Paths { - private string _pathToCustomizedAppiumJs; + private string _pathToAppiumPackageIndex; - public string PathToCustomizedAppiumJs + public string PathToAppiumPackageIndex { get { - if (_pathToCustomizedAppiumJs == null) + if (_pathToAppiumPackageIndex == null) { GetAppiumJsPath(); } - return _pathToCustomizedAppiumJs; + return _pathToAppiumPackageIndex; } } @@ -31,7 +31,7 @@ private void GetAppiumJsPath() appiumJsPath = Encoding.UTF8.GetString(bytes); npmPath = Npm.GetNpmPrefixPath(); - _pathToCustomizedAppiumJs = Path.Combine(npmPath, appiumJsPath); + _pathToAppiumPackageIndex = Path.Combine(npmPath, appiumJsPath); } } From 4619f2bdfd1b5a7f7742f01e28eccdf4245ea324 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 14:45:58 +0300 Subject: [PATCH 09/32] chore: Add timeout for RunCommand chore: Remove private var envPlatform --- test/integration/helpers/Npm.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 38afe198..cf6857be 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -7,13 +7,13 @@ namespace Appium.Net.Integration.Tests.helpers { internal class Npm { - static private Platform envPlatform; public static string GetNpmPrefixPath() { - envPlatform = Platform.CurrentPlatform; + string npmPath; - if (envPlatform.IsPlatformType(PlatformType.Windows)) + + if (Platform.CurrentPlatform.IsPlatformType(PlatformType.Windows)) { npmPath = GetNpmExecutablePath(); } @@ -30,6 +30,7 @@ public static string GetNpmPrefixPath() private static string RunCommand(string command, string arguments) { + int timeoutMilliseconds = 30000; try { Process process = new Process @@ -46,7 +47,7 @@ private static string RunCommand(string command, string arguments) process.Start(); string output = process.StandardOutput.ReadToEnd(); - process.WaitForExit(); + process.WaitForExit(timeoutMilliseconds); return output; } From 8bab6ef02b45e42b1f20b6824a20d80feb3ebf66 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 14:56:19 +0300 Subject: [PATCH 10/32] chore: Optimize code performance --- test/integration/helpers/Npm.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index cf6857be..ce45327c 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -11,19 +11,16 @@ internal class Npm public static string GetNpmPrefixPath() { - string npmPath; + string npmPath = "npm"; if (Platform.CurrentPlatform.IsPlatformType(PlatformType.Windows)) { npmPath = GetNpmExecutablePath(); } - else - { - npmPath = "npm"; - } + string result = RunCommand(npmPath, "list -g --depth=0"); string[] lines = result?.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); - string npmPrefixPath = lines[0]; + string npmPrefixPath = lines.FirstOrDefault(); return npmPrefixPath; } From 9cfd93c3685c17559a2e26d0a3837e302adee97d Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 14:58:02 +0300 Subject: [PATCH 11/32] chore: Optimize path readability on Windows platforms --- test/integration/helpers/Paths.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/integration/helpers/Paths.cs b/test/integration/helpers/Paths.cs index 96d0c57c..3c0dae71 100644 --- a/test/integration/helpers/Paths.cs +++ b/test/integration/helpers/Paths.cs @@ -31,8 +31,9 @@ private void GetAppiumJsPath() appiumJsPath = Encoding.UTF8.GetString(bytes); npmPath = Npm.GetNpmPrefixPath(); - _pathToAppiumPackageIndex = Path.Combine(npmPath, appiumJsPath); + string tempPath = Path.Combine(npmPath, appiumJsPath); + _pathToAppiumPackageIndex = tempPath.Replace("\\", "/"); } } } From abad288e0a70524838cefa76b42b34ab6edd0db9 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 15:07:27 +0300 Subject: [PATCH 12/32] chore: Switch from `npm list -g --depth=0` to `npm -g root` for better performance --- test/integration/helpers/Npm.cs | 8 ++++---- test/integration/helpers/PathToNode | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index ce45327c..4e4278d9 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -18,11 +18,11 @@ public static string GetNpmPrefixPath() npmPath = GetNpmExecutablePath(); } - string result = RunCommand(npmPath, "list -g --depth=0"); - string[] lines = result?.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); - string npmPrefixPath = lines.FirstOrDefault(); + string npmPrefixPath = RunCommand(npmPath, "-g root"); + //string[] lines = result?.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); + //string npmPrefixPath = lines.FirstOrDefault(); - return npmPrefixPath; + return npmPrefixPath.Trim(); } private static string RunCommand(string command, string arguments) diff --git a/test/integration/helpers/PathToNode b/test/integration/helpers/PathToNode index 96133d35..a5f158af 100644 --- a/test/integration/helpers/PathToNode +++ b/test/integration/helpers/PathToNode @@ -1 +1 @@ -node_modules/appium/index.js \ No newline at end of file +appium/index.js \ No newline at end of file From 7ac682aa387da48c6c0c3908fe692391e5add080 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 15:14:46 +0300 Subject: [PATCH 13/32] chore: Remove comment section --- test/integration/helpers/Npm.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 4e4278d9..f69239fb 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -19,8 +19,6 @@ public static string GetNpmPrefixPath() } string npmPrefixPath = RunCommand(npmPath, "-g root"); - //string[] lines = result?.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); - //string npmPrefixPath = lines.FirstOrDefault(); return npmPrefixPath.Trim(); } From ca7bff09465e86d57fbd3175eee1b30561138392 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 16:02:30 +0300 Subject: [PATCH 14/32] chore: Throw specific exception for npm commands --- test/integration/helpers/Npm.cs | 25 +++++++++---------- .../helpers/NpmNotFoundException.cs | 12 +++++++++ 2 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 test/integration/helpers/NpmNotFoundException.cs diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index f69239fb..01af236f 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -23,12 +23,11 @@ public static string GetNpmPrefixPath() return npmPrefixPath.Trim(); } - private static string RunCommand(string command, string arguments) + private static string RunCommand(string command, string arguments, int timeoutMilliseconds = 30000) { - int timeoutMilliseconds = 30000; try { - Process process = new Process + using (Process process = new Process { StartInfo = new ProcessStartInfo { @@ -38,22 +37,22 @@ private static string RunCommand(string command, string arguments) UseShellExecute = false, CreateNoWindow = true, } - }; - - process.Start(); - string output = process.StandardOutput.ReadToEnd(); - process.WaitForExit(timeoutMilliseconds); + }) + { + process.Start(); + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(timeoutMilliseconds); - return output; + return output; + } } - catch (Exception ex) + catch (Exception) { - Console.WriteLine($"Error executing command: {ex.Message}"); - return null; + throw new NpmNotFoundException(); } } - private static string GetNpmExecutablePath() + private static string GetNpmExecutablePath() { string result = RunCommand("where", "npm"); string npmPath; diff --git a/test/integration/helpers/NpmNotFoundException.cs b/test/integration/helpers/NpmNotFoundException.cs new file mode 100644 index 00000000..53c40e1d --- /dev/null +++ b/test/integration/helpers/NpmNotFoundException.cs @@ -0,0 +1,12 @@ +using System; + +namespace Appium.Net.Integration.Tests.helpers +{ + public class NpmNotFoundException : Exception + { + public NpmNotFoundException() + : base("Node Package Manager (npm) cannot be found. Make sure Node.js is installed and present in PATH.") + { + } + } +} From af3a35a2677f57ddac6041c3edaf7f9637bcf3d2 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 16:10:15 +0300 Subject: [PATCH 15/32] chore: Move declaration near reference --- test/integration/helpers/Paths.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/test/integration/helpers/Paths.cs b/test/integration/helpers/Paths.cs index 3c0dae71..85d66119 100644 --- a/test/integration/helpers/Paths.cs +++ b/test/integration/helpers/Paths.cs @@ -23,14 +23,10 @@ public string PathToAppiumPackageIndex private void GetAppiumJsPath() { - byte[] bytes; - string appiumJsPath; - string npmPath; - - bytes = Resources.PathToNode; - appiumJsPath = Encoding.UTF8.GetString(bytes); + byte[] bytes = Resources.PathToNode; + string appiumJsPath = Encoding.UTF8.GetString(bytes); - npmPath = Npm.GetNpmPrefixPath(); + string npmPath = Npm.GetNpmPrefixPath(); string tempPath = Path.Combine(npmPath, appiumJsPath); _pathToAppiumPackageIndex = tempPath.Replace("\\", "/"); From 1e66da43f2e363a771916efebfee808cf21a0da8 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 5 Oct 2023 16:11:02 +0300 Subject: [PATCH 16/32] chore: Rename GetAppiumJsPath --- test/integration/helpers/Paths.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/helpers/Paths.cs b/test/integration/helpers/Paths.cs index 85d66119..caf7d049 100644 --- a/test/integration/helpers/Paths.cs +++ b/test/integration/helpers/Paths.cs @@ -15,13 +15,13 @@ public string PathToAppiumPackageIndex { if (_pathToAppiumPackageIndex == null) { - GetAppiumJsPath(); + GetAppiumPackageIndexPath(); } return _pathToAppiumPackageIndex; } } - private void GetAppiumJsPath() + private void GetAppiumPackageIndexPath() { byte[] bytes = Resources.PathToNode; string appiumJsPath = Encoding.UTF8.GetString(bytes); From cedd7e017ca31c202122b3bb52c033df7e131178 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Fri, 6 Oct 2023 23:27:44 +0300 Subject: [PATCH 17/32] chore: Improve NPM path not found exception --- test/integration/helpers/Npm.cs | 2 +- test/integration/helpers/NpmNotFoundException.cs | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 01af236f..5537d5bb 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -63,7 +63,7 @@ private static string GetNpmExecutablePath() if (string.IsNullOrWhiteSpace(npmPath)) { - throw new ApplicationException("NPM path not found."); + throw new NpmNotFoundException($"NPM executable not found at path: {npmPath}. Please make sure the NPM executable is installed and check the configured path."); } return npmPath; diff --git a/test/integration/helpers/NpmNotFoundException.cs b/test/integration/helpers/NpmNotFoundException.cs index 53c40e1d..6fd3ba7f 100644 --- a/test/integration/helpers/NpmNotFoundException.cs +++ b/test/integration/helpers/NpmNotFoundException.cs @@ -8,5 +8,10 @@ public NpmNotFoundException() : base("Node Package Manager (npm) cannot be found. Make sure Node.js is installed and present in PATH.") { } + + public NpmNotFoundException(string message) + : base(message) + { + } } } From 187f6935b1cbfbfe7b5daa19d9c160e7cf980e8f Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Fri, 6 Oct 2023 23:32:47 +0300 Subject: [PATCH 18/32] chore: Rename resource file name --- test/integration/Properties/Resources.Designer.cs | 4 ++-- test/integration/Properties/Resources.resx | 4 ++-- test/integration/helpers/{PathToNode => PathToPackageIndex} | 0 test/integration/helpers/Paths.cs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) rename test/integration/helpers/{PathToNode => PathToPackageIndex} (100%) diff --git a/test/integration/Properties/Resources.Designer.cs b/test/integration/Properties/Resources.Designer.cs index c2f27df5..2d812b2e 100644 --- a/test/integration/Properties/Resources.Designer.cs +++ b/test/integration/Properties/Resources.Designer.cs @@ -73,9 +73,9 @@ public static byte[] ApiDemos_debug { /// /// Looks up a localized resource of type System.Byte[]. /// - public static byte[] PathToNode { + public static byte[] PathToPackageIndex { get { - object obj = ResourceManager.GetObject("PathToNode", resourceCulture); + object obj = ResourceManager.GetObject("PathToPackageIndex", resourceCulture); return ((byte[])(obj)); } } diff --git a/test/integration/Properties/Resources.resx b/test/integration/Properties/Resources.resx index a61993f2..bfaa9e81 100644 --- a/test/integration/Properties/Resources.resx +++ b/test/integration/Properties/Resources.resx @@ -133,7 +133,7 @@ ..\apps\archives\WebViewApp.app.zip;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - ..\helpers\PathToNode;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + ..\helpers\PathToPackageIndex;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 \ No newline at end of file diff --git a/test/integration/helpers/PathToNode b/test/integration/helpers/PathToPackageIndex similarity index 100% rename from test/integration/helpers/PathToNode rename to test/integration/helpers/PathToPackageIndex diff --git a/test/integration/helpers/Paths.cs b/test/integration/helpers/Paths.cs index caf7d049..9b255e9f 100644 --- a/test/integration/helpers/Paths.cs +++ b/test/integration/helpers/Paths.cs @@ -23,7 +23,7 @@ public string PathToAppiumPackageIndex private void GetAppiumPackageIndexPath() { - byte[] bytes = Resources.PathToNode; + byte[] bytes = Resources.PathToPackageIndex; string appiumJsPath = Encoding.UTF8.GetString(bytes); string npmPath = Npm.GetNpmPrefixPath(); From ace2dbbc7c8c97c164cae6257adc5b0d6f516ec3 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sat, 11 Nov 2023 21:48:19 +0200 Subject: [PATCH 19/32] chore: Remove path replacement and add XML DOC --- test/integration/helpers/PathToPackageIndex | 3 ++- test/integration/helpers/Paths.cs | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/test/integration/helpers/PathToPackageIndex b/test/integration/helpers/PathToPackageIndex index a5f158af..bf64f773 100644 --- a/test/integration/helpers/PathToPackageIndex +++ b/test/integration/helpers/PathToPackageIndex @@ -1 +1,2 @@ -appium/index.js \ No newline at end of file +appium +index.js \ No newline at end of file diff --git a/test/integration/helpers/Paths.cs b/test/integration/helpers/Paths.cs index 9b255e9f..8a2c0223 100644 --- a/test/integration/helpers/Paths.cs +++ b/test/integration/helpers/Paths.cs @@ -1,5 +1,6 @@ using Appium.Net.Integration.Tests.helpers; using Appium.Net.Integration.Tests.Properties; +using System; using System.IO; using System.Text; @@ -21,15 +22,23 @@ public string PathToAppiumPackageIndex } } + /// + /// Combines the path components from the appiumJsPath with the npmPath. + /// + /// + /// This function reads a byte array from the PathToPackageIndex resource, converts it to a string, + /// processes the relative path to remove '\r\n', and combines it with the npm prefix path. + /// The resulting combined path is assigned to the _pathToAppiumPackageIndex variable. + /// private void GetAppiumPackageIndexPath() { byte[] bytes = Resources.PathToPackageIndex; string appiumJsPath = Encoding.UTF8.GetString(bytes); + string[] appiumJsPathComponents = appiumJsPath.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); string npmPath = Npm.GetNpmPrefixPath(); - string tempPath = Path.Combine(npmPath, appiumJsPath); - _pathToAppiumPackageIndex = tempPath.Replace("\\", "/"); + _pathToAppiumPackageIndex = Path.Combine(npmPath, Path.Combine(appiumJsPathComponents)); } } } From 496c849a10bc409a62db4c8c7187cea18066f7ba Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sat, 11 Nov 2023 21:54:49 +0200 Subject: [PATCH 20/32] chore: Verify exit code on RunCommand() from Npm --- test/integration/helpers/Npm.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 5537d5bb..c82820bf 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -34,6 +34,7 @@ private static string RunCommand(string command, string arguments, int timeoutMi FileName = command, Arguments = arguments, RedirectStandardOutput = true, + RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, } @@ -41,8 +42,15 @@ private static string RunCommand(string command, string arguments, int timeoutMi { process.Start(); string output = process.StandardOutput.ReadToEnd(); + string errorOutput = process.StandardError.ReadToEnd(); process.WaitForExit(timeoutMilliseconds); + int exitCode = process.ExitCode; + if (exitCode != 0) + { + throw new ApplicationException($"Command exited with code {exitCode}. Error: {errorOutput}"); + } + return output; } } From 27988b61777e4c3f02dc106c75d929f905528585 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sat, 11 Nov 2023 22:38:51 +0200 Subject: [PATCH 21/32] chore: Use GetNpmExecutablePath() on all platforms --- test/integration/helpers/Npm.cs | 34 +++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index c82820bf..3ebba08a 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -1,5 +1,4 @@ -using OpenQA.Selenium; -using System; +using System; using System.Diagnostics; using System.Linq; @@ -10,14 +9,7 @@ internal class Npm public static string GetNpmPrefixPath() { - - string npmPath = "npm"; - - if (Platform.CurrentPlatform.IsPlatformType(PlatformType.Windows)) - { - npmPath = GetNpmExecutablePath(); - } - + string npmPath = GetNpmExecutablePath(); string npmPrefixPath = RunCommand(npmPath, "-g root"); return npmPrefixPath.Trim(); @@ -60,21 +52,35 @@ private static string RunCommand(string command, string arguments, int timeoutMi } } - private static string GetNpmExecutablePath() + private static string GetNpmExecutablePath() { - string result = RunCommand("where", "npm"); + string commandName = IsWindows() ? "where" : "which"; + string result = RunCommand(commandName, "npm"); + string npmPath; string[] lines = result?.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); - npmPath = lines?.FirstOrDefault(line => line.EndsWith("npm.cmd")); + if (IsWindows()) + { + npmPath = lines?.FirstOrDefault(line => !string.IsNullOrWhiteSpace(line) && line.EndsWith("npm.cmd")); + } + else + { + npmPath = lines?.FirstOrDefault(line => !string.IsNullOrWhiteSpace(line)); + } if (string.IsNullOrWhiteSpace(npmPath)) { - throw new NpmNotFoundException($"NPM executable not found at path: {npmPath}. Please make sure the NPM executable is installed and check the configured path."); + throw new NpmNotFoundException("NPM executable not found. Please make sure the NPM executable is installed and check the configured path."); } return npmPath; } + + private static bool IsWindows() + { + return Environment.OSVersion.Platform == PlatformID.Win32NT; + } } } From 70896e006661c142bf9c0cafbbfda5cfdfd70182 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sat, 11 Nov 2023 23:58:29 +0200 Subject: [PATCH 22/32] chore: log command upon errorin RunCommand() --- test/integration/helpers/Npm.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 3ebba08a..022dfe93 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -40,7 +40,7 @@ private static string RunCommand(string command, string arguments, int timeoutMi int exitCode = process.ExitCode; if (exitCode != 0) { - throw new ApplicationException($"Command exited with code {exitCode}. Error: {errorOutput}"); + throw new ApplicationException($"Command: {command} exited with code {exitCode}. Error: {errorOutput}"); } return output; From 76c1afa115b5d020187c2f61eb4107c88ed9c025 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sun, 12 Nov 2023 00:01:08 +0200 Subject: [PATCH 23/32] chore: Improve NpmNotFoundException log --- test/integration/helpers/Npm.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 022dfe93..2ce0e4b2 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -72,7 +72,7 @@ private static string GetNpmExecutablePath() if (string.IsNullOrWhiteSpace(npmPath)) { - throw new NpmNotFoundException("NPM executable not found. Please make sure the NPM executable is installed and check the configured path."); + throw new NpmNotFoundException("NPM executable not found. Please make sure the NPM executable is installed and check the configured PATH environment variable."); } return npmPath; From 5a0a7d2a3b41b9563e5948db59ceda9a4c8fe156 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sun, 12 Nov 2023 00:05:00 +0200 Subject: [PATCH 24/32] chore: Rename GetAppiumPackageIndexPath() --- test/integration/helpers/Paths.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/helpers/Paths.cs b/test/integration/helpers/Paths.cs index 8a2c0223..08addfe7 100644 --- a/test/integration/helpers/Paths.cs +++ b/test/integration/helpers/Paths.cs @@ -16,7 +16,7 @@ public string PathToAppiumPackageIndex { if (_pathToAppiumPackageIndex == null) { - GetAppiumPackageIndexPath(); + InitAppiumPackageIndexPath(); } return _pathToAppiumPackageIndex; } @@ -30,7 +30,7 @@ public string PathToAppiumPackageIndex /// processes the relative path to remove '\r\n', and combines it with the npm prefix path. /// The resulting combined path is assigned to the _pathToAppiumPackageIndex variable. /// - private void GetAppiumPackageIndexPath() + private void InitAppiumPackageIndexPath() { byte[] bytes = Resources.PathToPackageIndex; string appiumJsPath = Encoding.UTF8.GetString(bytes); From 587859c1a5d2085f4f88f765483192a545dae62f Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sun, 12 Nov 2023 00:37:53 +0200 Subject: [PATCH 25/32] chore: Simplify InitAppiumPackageIndexPath() --- test/integration/Properties/Resources.Designer.cs | 10 ---------- test/integration/Properties/Resources.resx | 3 --- test/integration/helpers/PathToPackageIndex | 2 -- test/integration/helpers/Paths.cs | 14 +++----------- 4 files changed, 3 insertions(+), 26 deletions(-) delete mode 100644 test/integration/helpers/PathToPackageIndex diff --git a/test/integration/Properties/Resources.Designer.cs b/test/integration/Properties/Resources.Designer.cs index 2d812b2e..02421c41 100644 --- a/test/integration/Properties/Resources.Designer.cs +++ b/test/integration/Properties/Resources.Designer.cs @@ -70,16 +70,6 @@ public static byte[] ApiDemos_debug { } } - /// - /// Looks up a localized resource of type System.Byte[]. - /// - public static byte[] PathToPackageIndex { - get { - object obj = ResourceManager.GetObject("PathToPackageIndex", resourceCulture); - return ((byte[])(obj)); - } - } - /// /// Looks up a localized resource of type System.Byte[]. /// diff --git a/test/integration/Properties/Resources.resx b/test/integration/Properties/Resources.resx index bfaa9e81..a1013a48 100644 --- a/test/integration/Properties/Resources.resx +++ b/test/integration/Properties/Resources.resx @@ -133,7 +133,4 @@ ..\apps\archives\WebViewApp.app.zip;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - ..\helpers\PathToPackageIndex;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - \ No newline at end of file diff --git a/test/integration/helpers/PathToPackageIndex b/test/integration/helpers/PathToPackageIndex deleted file mode 100644 index bf64f773..00000000 --- a/test/integration/helpers/PathToPackageIndex +++ /dev/null @@ -1,2 +0,0 @@ -appium -index.js \ No newline at end of file diff --git a/test/integration/helpers/Paths.cs b/test/integration/helpers/Paths.cs index 08addfe7..a61d2225 100644 --- a/test/integration/helpers/Paths.cs +++ b/test/integration/helpers/Paths.cs @@ -1,8 +1,5 @@ using Appium.Net.Integration.Tests.helpers; -using Appium.Net.Integration.Tests.Properties; -using System; using System.IO; -using System.Text; namespace Appium.Net.Integration.Tests.Helpers { @@ -23,19 +20,14 @@ public string PathToAppiumPackageIndex } /// - /// Combines the path components from the appiumJsPath with the npmPath. + /// Initializes the Appium package index path by combining the components "appium" and "index.js" with the npm prefix path. /// /// - /// This function reads a byte array from the PathToPackageIndex resource, converts it to a string, - /// processes the relative path to remove '\r\n', and combines it with the npm prefix path. - /// The resulting combined path is assigned to the _pathToAppiumPackageIndex variable. + /// This method sets the _pathToAppiumPackageIndex variable by combining the specified components with the npm prefix path. /// private void InitAppiumPackageIndexPath() { - byte[] bytes = Resources.PathToPackageIndex; - string appiumJsPath = Encoding.UTF8.GetString(bytes); - string[] appiumJsPathComponents = appiumJsPath.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); - + string[] appiumJsPathComponents = { "appium", "index.js" }; string npmPath = Npm.GetNpmPrefixPath(); _pathToAppiumPackageIndex = Path.Combine(npmPath, Path.Combine(appiumJsPathComponents)); From 86235678cc723a933230782d9b9085d67bf4fbb3 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sun, 12 Nov 2023 07:10:19 +0200 Subject: [PATCH 26/32] chore: Simplify paths creation --- test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs b/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs index bc736c3a..91d0aa38 100644 --- a/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs +++ b/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs @@ -37,8 +37,7 @@ public void BeforeAll() } Console.WriteLine(_testIp); - Paths paths = new Paths(); - _pathToAppiumPackageIndex = paths.PathToAppiumPackageIndex; + _pathToAppiumPackageIndex = new Paths().PathToAppiumPackageIndex; } [Test] From 4245ee37cd0336ca4d9d05ad5d92399b7078eb31 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sun, 12 Nov 2023 07:34:34 +0200 Subject: [PATCH 27/32] chore: More exceptions mapping for RunCommand --- test/integration/helpers/Npm.cs | 25 ++++++++++++++++--- .../helpers/NpmUnknownCommandException.cs | 17 +++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 test/integration/helpers/NpmUnknownCommandException.cs diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 2ce0e4b2..cb9e899e 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Diagnostics; using System.Linq; @@ -35,21 +36,39 @@ private static string RunCommand(string command, string arguments, int timeoutMi process.Start(); string output = process.StandardOutput.ReadToEnd(); string errorOutput = process.StandardError.ReadToEnd(); - process.WaitForExit(timeoutMilliseconds); + _ = process.WaitForExit(timeoutMilliseconds); int exitCode = process.ExitCode; - if (exitCode != 0) + if (exitCode == 1) { - throw new ApplicationException($"Command: {command} exited with code {exitCode}. Error: {errorOutput}"); + throw new NpmUnknownCommandException($"Command: `{arguments}` exited with code {exitCode}. Error: {output}"); + } + else if (exitCode != 0) + { + throw new ApplicationException($"Command: `{command}` exited with code {exitCode}. Error: {errorOutput}"); } return output; } } + catch (ApplicationException ex) + { + throw ex; + } + + catch (Win32Exception) + { + throw; + } + catch (NpmUnknownCommandException) + { + throw; + } catch (Exception) { throw new NpmNotFoundException(); } + } private static string GetNpmExecutablePath() diff --git a/test/integration/helpers/NpmUnknownCommandException.cs b/test/integration/helpers/NpmUnknownCommandException.cs new file mode 100644 index 00000000..492dbf98 --- /dev/null +++ b/test/integration/helpers/NpmUnknownCommandException.cs @@ -0,0 +1,17 @@ +using System; + +namespace Appium.Net.Integration.Tests.helpers +{ + public class NpmUnknownCommandException : Exception + { + public NpmUnknownCommandException() + : base("Unknown npm command encountered. ") + { + } + + public NpmUnknownCommandException(string message) + : base(message) + { + } + } +} From ab5ace64330746df1837dc12f58f1a8ce6170696 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Mon, 13 Nov 2023 20:08:08 +0200 Subject: [PATCH 28/32] fix: More exceptions improvements in RunCommand() --- test/integration/helpers/Npm.cs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index cb9e899e..ebcb023a 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -41,7 +41,14 @@ private static string RunCommand(string command, string arguments, int timeoutMi int exitCode = process.ExitCode; if (exitCode == 1) { - throw new NpmUnknownCommandException($"Command: `{arguments}` exited with code {exitCode}. Error: {output}"); + if (command.Contains("npm")) + { + throw new NpmUnknownCommandException($"Command: `{arguments}` exited with code {exitCode}. Error: {output}"); + } + else + { + throw new Exception($"Command: '{command} {arguments}` exited with code {exitCode}. Error: {errorOutput}"); + } } else if (exitCode != 0) { @@ -51,23 +58,26 @@ private static string RunCommand(string command, string arguments, int timeoutMi return output; } } - catch (ApplicationException ex) + catch (ApplicationException) { - throw ex; + throw; } catch (Win32Exception) { - throw; + if (command.Contains("npm")) + { + throw new NpmNotFoundException(); + } + else + { + throw; + } } catch (NpmUnknownCommandException) { throw; } - catch (Exception) - { - throw new NpmNotFoundException(); - } } From e1bb71ae3dc1b8a3f573aa7e92015142c06fe483 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Mon, 13 Nov 2023 23:06:23 +0200 Subject: [PATCH 29/32] fix: Exceptions makeover pt. 3 --- test/integration/helpers/Npm.cs | 40 ++++--------------- .../helpers/NpmNotFoundException.cs | 5 +++ 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index ebcb023a..23247c67 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -1,7 +1,9 @@ using System; using System.ComponentModel; using System.Diagnostics; +using System.IO; using System.Linq; +using System.Security.Cryptography; namespace Appium.Net.Integration.Tests.helpers { @@ -39,46 +41,20 @@ private static string RunCommand(string command, string arguments, int timeoutMi _ = process.WaitForExit(timeoutMilliseconds); int exitCode = process.ExitCode; - if (exitCode == 1) + if ((exitCode == 1) && command.Contains("npm")) { - if (command.Contains("npm")) - { - throw new NpmUnknownCommandException($"Command: `{arguments}` exited with code {exitCode}. Error: {output}"); - } - else - { - throw new Exception($"Command: '{command} {arguments}` exited with code {exitCode}. Error: {errorOutput}"); - } - } - else if (exitCode != 0) - { - throw new ApplicationException($"Command: `{command}` exited with code {exitCode}. Error: {errorOutput}"); + Console.WriteLine($"npm Error upon command: `{arguments}`. {output}"); + throw new NpmUnknownCommandException($"Command: `{arguments}` exited with code {exitCode}. Error: {output}"); } return output; } } - catch (ApplicationException) - { - throw; - } - - catch (Win32Exception) - { - if (command.Contains("npm")) - { - throw new NpmNotFoundException(); - } - else - { - throw; - } - } - catch (NpmUnknownCommandException) + catch (Win32Exception ex) when (command.Contains("npm")) { - throw; + Console.WriteLine(ex.Message); + throw new NpmNotFoundException($"mpm not found under {command}", ex); } - } private static string GetNpmExecutablePath() diff --git a/test/integration/helpers/NpmNotFoundException.cs b/test/integration/helpers/NpmNotFoundException.cs index 6fd3ba7f..71521797 100644 --- a/test/integration/helpers/NpmNotFoundException.cs +++ b/test/integration/helpers/NpmNotFoundException.cs @@ -13,5 +13,10 @@ public NpmNotFoundException(string message) : base(message) { } + + public NpmNotFoundException(string message, Exception innerException) + : base(message, innerException) + { + } } } From 981a645eb1ff3979bef231cb8a744f046ae4f282 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Mon, 13 Nov 2023 23:43:06 +0200 Subject: [PATCH 30/32] fix: npm typo --- test/integration/helpers/Npm.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 23247c67..001f3112 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -53,7 +53,7 @@ private static string RunCommand(string command, string arguments, int timeoutMi catch (Win32Exception ex) when (command.Contains("npm")) { Console.WriteLine(ex.Message); - throw new NpmNotFoundException($"mpm not found under {command}", ex); + throw new NpmNotFoundException($"npm not found under {command}", ex); } } From 7d4d7004cd260bd6aa508ad4c07bad85cb8d7d8a Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Mon, 13 Nov 2023 23:51:41 +0200 Subject: [PATCH 31/32] chore: Minimise try-catch --- test/integration/helpers/Npm.cs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 001f3112..8361da78 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -20,6 +20,8 @@ public static string GetNpmPrefixPath() private static string RunCommand(string command, string arguments, int timeoutMilliseconds = 30000) { + int exitCode; + string output; try { using (Process process = new Process @@ -36,20 +38,22 @@ private static string RunCommand(string command, string arguments, int timeoutMi }) { process.Start(); - string output = process.StandardOutput.ReadToEnd(); + + output = process.StandardOutput.ReadToEnd(); string errorOutput = process.StandardError.ReadToEnd(); _ = process.WaitForExit(timeoutMilliseconds); - int exitCode = process.ExitCode; - if ((exitCode == 1) && command.Contains("npm")) - { - Console.WriteLine($"npm Error upon command: `{arguments}`. {output}"); - throw new NpmUnknownCommandException($"Command: `{arguments}` exited with code {exitCode}. Error: {output}"); - } - - return output; + exitCode = process.ExitCode; } + if ((exitCode == 1) && command.Contains("npm")) + { + Console.WriteLine($"npm Error upon command: `{arguments}`. {output}"); + throw new NpmUnknownCommandException($"Command: `{arguments}` exited with code {exitCode}. Error: {output}"); + } + + return output; } + catch (Win32Exception ex) when (command.Contains("npm")) { Console.WriteLine(ex.Message); From 4eb939333ae4c6fe22872a9c42598a68c746f287 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Mon, 13 Nov 2023 23:53:41 +0200 Subject: [PATCH 32/32] chore: Remove unnecessary using --- test/integration/helpers/Npm.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/integration/helpers/Npm.cs b/test/integration/helpers/Npm.cs index 8361da78..d3605116 100644 --- a/test/integration/helpers/Npm.cs +++ b/test/integration/helpers/Npm.cs @@ -1,9 +1,7 @@ using System; using System.ComponentModel; using System.Diagnostics; -using System.IO; using System.Linq; -using System.Security.Cryptography; namespace Appium.Net.Integration.Tests.helpers {