Skip to content

Commit 3d1c6f6

Browse files
committed
Refactor AppiumLocalServiceTests namespace and setup
Changed the namespace of `AppiumLocalServiceTests` to `Appium.Net.Integration.Tests.ServerTests`. Updated the `GlobalSetup` method to dynamically determine the Appium path using an environment variable or by executing `npm root -g`, removing hardcoded paths. If the Appium path is invalid, the test is skipped with an appropriate message.
1 parent d7c8cc9 commit 3d1c6f6

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

test/integration/ServerTests/AppiumLocalServiceTests.cs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using OpenQA.Selenium.Appium.Service;
88
using OpenQA.Selenium.Appium.Service.Options;
99

10-
namespace Appium.Net.Unit.Tests.Appium.Service
10+
namespace Appium.Net.Integration.Tests.ServerTests
1111
{
1212
[TestFixture]
1313
public class AppiumLocalServiceTests
@@ -19,9 +19,52 @@ public void GlobalSetup()
1919
{
2020
// OptionCollector can be customized as needed
2121
var optionCollector = new OptionCollector();
22-
var appiumPath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
23-
? @"C:\Users\Dor-B\AppData\Roaming\npm\node_modules\appium\build\lib\main.js"
24-
: "/usr/local/lib/node_modules/appium/build/lib/main.js";
22+
23+
// Try to get Appium path from environment variable or npm root -g
24+
string appiumPath = Environment.GetEnvironmentVariable(AppiumServiceConstants.AppiumBinaryPath);
25+
26+
if (string.IsNullOrEmpty(appiumPath))
27+
{
28+
try
29+
{
30+
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
31+
32+
var startInfo = new ProcessStartInfo
33+
{
34+
// On Windows, using 'cmd /c' is often more reliable for resolving PATHs
35+
FileName = isWindows ? "cmd.exe" : "npm",
36+
Arguments = isWindows ? "/c npm root -g" : "root -g",
37+
RedirectStandardOutput = true,
38+
RedirectStandardError = true, // Capture errors too!
39+
UseShellExecute = false,
40+
CreateNoWindow = true
41+
};
42+
43+
using var process = Process.Start(startInfo);
44+
string output = process.StandardOutput.ReadToEnd()?.Trim();
45+
string error = process.StandardError.ReadToEnd()?.Trim();
46+
process.WaitForExit();
47+
48+
if (!string.IsNullOrEmpty(output))
49+
{
50+
appiumPath = Path.Combine(output, "appium", "build", "lib", "main.js");
51+
}
52+
else if (!string.IsNullOrEmpty(error))
53+
{
54+
Console.WriteLine($"NPM Error: {error}");
55+
}
56+
}
57+
catch (Exception ex)
58+
{
59+
Console.WriteLine($"Process failed: {ex.Message}");
60+
}
61+
}
62+
63+
if (string.IsNullOrEmpty(appiumPath) || !File.Exists(appiumPath))
64+
{
65+
Assert.Ignore("Appium is not installed or not found on this machine. Skipping AppiumLocalServiceTests.");
66+
}
67+
2568
appiumServer = new AppiumServiceBuilder()
2669
.WithAppiumJS(new FileInfo(appiumPath))
2770
.WithIPAddress("127.0.0.1")

0 commit comments

Comments
 (0)