From c3c69c8ab2cb17ff7977431507c015ccebba25fd Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 13 Oct 2022 17:56:31 +0300 Subject: [PATCH 1/6] Initial migration to appium2.0 [Removal of /wd/hub] --- src/Appium.Net/Appium/Service/AppiumLocalService.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Appium.Net/Appium/Service/AppiumLocalService.cs b/src/Appium.Net/Appium/Service/AppiumLocalService.cs index 5461fbefa..1cc90e7db 100644 --- a/src/Appium.Net/Appium/Service/AppiumLocalService.cs +++ b/src/Appium.Net/Appium/Service/AppiumLocalService.cs @@ -58,7 +58,7 @@ internal AppiumLocalService( /// /// The base URL for the managed appium server. /// - public Uri ServiceUrl => new Uri($"http://{IP.ToString()}:{Convert.ToString(Port)}/wd/hub"); + public Uri ServiceUrl => new Uri($"http://{IP}:{Convert.ToString(Port)}"); /// /// Event that can be used to capture the output of the service @@ -184,17 +184,17 @@ private bool Ping(TimeSpan span) Uri service = ServiceUrl; if (service.IsLoopback || IP.ToString().Equals(AppiumServiceConstants.DefaultLocalIPAddress)) { - status = new Uri("http://localhost:" + Convert.ToString(Port) + "/wd/hub/status"); + status = new Uri("http://localhost:" + Convert.ToString(Port) + "/status"); } else { - status = new Uri(service.ToString() + "/status"); + status = new Uri(service.ToString() + "status"); } DateTime endTime = DateTime.Now.Add(this.InitializationTimeout); while (!pinged & DateTime.Now < endTime) { - HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(status); + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(status); HttpWebResponse response = null; try { From 1c0d317cf8e0ac3fe66c5822fed49a603280c70e Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Thu, 13 Oct 2022 17:59:32 +0300 Subject: [PATCH 2/6] Removal of redundent using on Integration.Tests.Properties --- test/integration/Properties/Resources.Designer.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/integration/Properties/Resources.Designer.cs b/test/integration/Properties/Resources.Designer.cs index d3397d2da..400c7e3eb 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 { - using System; - - +namespace Appium.Net.Integration.Tests.Properties +{ + + /// /// A strongly-typed resource class, for looking up localized strings, etc. /// From 4669067c16c40f89e1212e1189584ad38fe5ab1a Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Mon, 17 Oct 2022 14:54:58 +0300 Subject: [PATCH 3/6] Add test for StartService using basePath flag Add Support for basePath argument Add BasePath to common Appium server arguments Add StatusUrl const to appiumService Add CreateStatusUrl function Update link to list of common Appium server arguments --- .../Appium/Service/AppiumLocalService.cs | 66 ++++++++++++++++--- .../Appium/Service/AppiumServiceConstants.cs | 4 +- .../Service/Options/GeneralOptionList.cs | 12 +++- .../AppiumLocalServerLaunchingTest.cs | 18 +++++ 4 files changed, 88 insertions(+), 12 deletions(-) diff --git a/src/Appium.Net/Appium/Service/AppiumLocalService.cs b/src/Appium.Net/Appium/Service/AppiumLocalService.cs index 1cc90e7db..886998c2d 100644 --- a/src/Appium.Net/Appium/Service/AppiumLocalService.cs +++ b/src/Appium.Net/Appium/Service/AppiumLocalService.cs @@ -18,6 +18,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Net; using System.Runtime.CompilerServices; @@ -40,11 +41,11 @@ public class AppiumLocalService : ICommandServer public static AppiumLocalService BuildDefaultService() => new AppiumServiceBuilder().Build(); internal AppiumLocalService( - FileInfo nodeJS, - string arguments, - IPAddress ip, + FileInfo nodeJS, + string arguments, + IPAddress ip, int port, - TimeSpan initializationTimeout, + TimeSpan initializationTimeout, IDictionary environmentForProcess) { NodeJS = nodeJS; @@ -174,22 +175,67 @@ public bool IsRunning return Ping(new TimeSpan(0, 0, 0, 0, 500)); } } - - private bool Ping(TimeSpan span) + private string GetArgsValue(string argStr) + { + int idx; + List args = Arguments.Split(' ').ToList(); + idx= args.IndexOf(argStr); + return args[idx + 1]; + } + private string ParseBasePath() + { + if (Arguments.Contains("--base-path")) + { + return GetArgsValue("--base-path"); + } + else if (Arguments.Contains("-pa")) + { + return GetArgsValue("-pa"); + } + return AppiumServiceConstants.DefaultBasePath; + } + private Uri CreateStatusUrl() { - bool pinged = false; - Uri status; Uri service = ServiceUrl; + + string basePath = ParseBasePath(); + bool defBasePath = basePath.Equals(AppiumServiceConstants.DefaultBasePath); + if (service.IsLoopback || IP.ToString().Equals(AppiumServiceConstants.DefaultLocalIPAddress)) { - status = new Uri("http://localhost:" + Convert.ToString(Port) + "/status"); + var tmpStatus = "http://localhost:" + Convert.ToString(Port); + if (defBasePath) + { + status = new Uri(tmpStatus + AppiumServiceConstants.StatusUrl); + } + else + { + status = new Uri(tmpStatus + basePath + AppiumServiceConstants.StatusUrl); + } } else { - status = new Uri(service.ToString() + "status"); + if (defBasePath) + { + status = new Uri(service, AppiumServiceConstants.StatusUrl); + } + else + { + status = new Uri(service, basePath + AppiumServiceConstants.StatusUrl); + } } + return status; + } + + private bool Ping(TimeSpan span) + { + bool pinged = false; + + Uri status; + + status = CreateStatusUrl(); DateTime endTime = DateTime.Now.Add(this.InitializationTimeout); while (!pinged & DateTime.Now < endTime) diff --git a/src/Appium.Net/Appium/Service/AppiumServiceConstants.cs b/src/Appium.Net/Appium/Service/AppiumServiceConstants.cs index 02a20d144..c64ab7d57 100644 --- a/src/Appium.Net/Appium/Service/AppiumServiceConstants.cs +++ b/src/Appium.Net/Appium/Service/AppiumServiceConstants.cs @@ -37,9 +37,11 @@ public sealed class AppiumServiceConstants internal readonly static string Bash = "/bin/bash"; internal readonly static string CmdExe = "cmd.exe"; internal readonly static string Node = "node"; - + internal static readonly string DefaultLocalIPAddress = "127.0.0.1"; internal static readonly int DefaultAppiumPort = 4723; + internal static readonly string StatusUrl = "/status"; + internal static readonly string DefaultBasePath = "/"; internal static readonly string AppiumFolder = "appium"; internal static readonly string BinFolder = "bin"; diff --git a/src/Appium.Net/Appium/Service/Options/GeneralOptionList.cs b/src/Appium.Net/Appium/Service/Options/GeneralOptionList.cs index 853ddab30..55502cb46 100644 --- a/src/Appium.Net/Appium/Service/Options/GeneralOptionList.cs +++ b/src/Appium.Net/Appium/Service/Options/GeneralOptionList.cs @@ -20,7 +20,7 @@ namespace OpenQA.Selenium.Appium.Service.Options /// /// Here is the list of common Appium server arguments. /// All flags are optional, but some are required in conjunction with certain others. - /// The full list is available here: http://appium.io/slate/en/master/?ruby#appium-server-arguments + /// The full list is available here: https://appium.io/docs/en/writing-running-appium/server-args/ /// public sealed class GeneralOptionList : BaseOptionList { @@ -30,6 +30,16 @@ public sealed class GeneralOptionList : BaseOptionList public static KeyValuePair Shell(string value) => GetKeyValuePair("--shell", value); + /// + /// Initial path segment where the Appium/WebDriver API will be hosted. Every endpoint will be behind this segment.
+ /// Default: for Appium 2.0: / ; for Appium 1: /wd/hub
+ /// Example: --base-path /my/path/prefix + ///
+ /// + /// + public static KeyValuePair BasePath(string value) => + GetKeyValuePair("--base-path", value); + /// /// callback IP Address (default: same as address)
/// Sample
diff --git a/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs b/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs index 2b23ec1d1..8662eef3a 100644 --- a/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs +++ b/test/integration/ServerTests/AppiumLocalServerLaunchingTest.cs @@ -184,6 +184,24 @@ public void CheckAbilityToStartServiceUsingFlags() } } + [Test] + public void CheckAbilityToStartServiceUsingBasePathFlag() + { + AppiumLocalService service = null; + var args = new OptionCollector().AddArguments(GeneralOptionList.BasePath("/wd/hub")); + try + { + service = new AppiumServiceBuilder().WithArguments(args).Build(); + service.Start(); + Assert.IsTrue(service.IsRunning); + } + finally + { + service?.Dispose(); + } + } + + [Test] public void CheckAbilityToStartServiceUsingCapabilities() { From cafba7878c62314090f70fa607a04140d3569d02 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sat, 22 Oct 2022 15:46:49 +0300 Subject: [PATCH 4/6] Work with ArgsList for cleaner code --- .../Appium/Service/AppiumLocalService.cs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Appium.Net/Appium/Service/AppiumLocalService.cs b/src/Appium.Net/Appium/Service/AppiumLocalService.cs index 886998c2d..6138941d3 100644 --- a/src/Appium.Net/Appium/Service/AppiumLocalService.cs +++ b/src/Appium.Net/Appium/Service/AppiumLocalService.cs @@ -33,6 +33,7 @@ public class AppiumLocalService : ICommandServer private readonly TimeSpan InitializationTimeout; private readonly IDictionary EnvironmentForProcess; private Process Service; + private List ArgsList; /// /// Creates an instance of AppiumLocalService without special settings @@ -175,31 +176,38 @@ public bool IsRunning return Ping(new TimeSpan(0, 0, 0, 0, 500)); } } + private string GetArgsValue(string argStr) { int idx; - List args = Arguments.Split(' ').ToList(); - idx= args.IndexOf(argStr); - return args[idx + 1]; + idx= ArgsList.IndexOf(argStr); + return ArgsList[idx + 1]; } + private string ParseBasePath() { - if (Arguments.Contains("--base-path")) + if (ArgsList.Contains("--base-path")) { return GetArgsValue("--base-path"); } - else if (Arguments.Contains("-pa")) + else if (ArgsList.Contains("-pa")) { return GetArgsValue("-pa"); } return AppiumServiceConstants.DefaultBasePath; } + + private void GenerateArgsList() + { + ArgsList = Arguments.Split(' ').ToList(); + } private Uri CreateStatusUrl() { Uri status; - Uri service = ServiceUrl; + GenerateArgsList(); + string basePath = ParseBasePath(); bool defBasePath = basePath.Equals(AppiumServiceConstants.DefaultBasePath); From 325e6404502d0dd2f099bcf0a4799c4adce4dcd0 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sat, 22 Oct 2022 18:37:58 +0300 Subject: [PATCH 5/6] Skip GenerateArgsList if it's not null --- src/Appium.Net/Appium/Service/AppiumLocalService.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Appium.Net/Appium/Service/AppiumLocalService.cs b/src/Appium.Net/Appium/Service/AppiumLocalService.cs index 6138941d3..877546a43 100644 --- a/src/Appium.Net/Appium/Service/AppiumLocalService.cs +++ b/src/Appium.Net/Appium/Service/AppiumLocalService.cs @@ -186,7 +186,7 @@ private string GetArgsValue(string argStr) private string ParseBasePath() { - if (ArgsList.Contains("--base-path")) + if (ArgsList.Contains("--base-path")) { return GetArgsValue("--base-path"); } @@ -206,8 +206,10 @@ private Uri CreateStatusUrl() Uri status; Uri service = ServiceUrl; - GenerateArgsList(); - + if (ArgsList == null) + { + GenerateArgsList(); + } string basePath = ParseBasePath(); bool defBasePath = basePath.Equals(AppiumServiceConstants.DefaultBasePath); From 35b569bb093aa845dcd4ad7797779a4a840d9492 Mon Sep 17 00:00:00 2001 From: Dor-bl Date: Sat, 22 Oct 2022 21:59:49 +0300 Subject: [PATCH 6/6] Minor code fixes Remove unused Exceptions variables Fix unused span parameter --- src/Appium.Net/Appium/Service/AppiumLocalService.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Appium.Net/Appium/Service/AppiumLocalService.cs b/src/Appium.Net/Appium/Service/AppiumLocalService.cs index 877546a43..b66ef2cff 100644 --- a/src/Appium.Net/Appium/Service/AppiumLocalService.cs +++ b/src/Appium.Net/Appium/Service/AppiumLocalService.cs @@ -133,7 +133,7 @@ private void DestroyProcess() { Service.Kill(); } - catch (Exception ignored) + catch { } finally @@ -247,7 +247,7 @@ private bool Ping(TimeSpan span) status = CreateStatusUrl(); - DateTime endTime = DateTime.Now.Add(this.InitializationTimeout); + DateTime endTime = DateTime.Now.Add(span); while (!pinged & DateTime.Now < endTime) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(status); @@ -259,7 +259,7 @@ private bool Ping(TimeSpan span) pinged = true; } } - catch (Exception e) + catch { pinged = false; }