Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 34 additions & 38 deletions Appium Wizard/AppiumServerSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Newtonsoft.Json.Linq;
using NLog;
using RestSharp;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -30,7 +31,7 @@
{
Logger.Info("Appium server initial command : " + command);
string versionString = Common.InstalledAppiumServerVersionFromPackageJson();
if (Version.TryParse(versionString, out Version installedVersion))

Check warning on line 34 in Appium Wizard/AppiumServerSetup.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Converting null literal or possible null value to non-nullable type.
{
Version minimumVersion = new Version(2, 12, 3);
if (installedVersion > minimumVersion)
Expand Down Expand Up @@ -61,7 +62,7 @@
command = command + " --port " + appiumPort;
}
var pluginList = Common.GetListOfInstalledPlugins();
if (pluginList.TryGetValue("inspector", out string value) && !value.Equals("NotInstalled"))

Check warning on line 65 in Appium Wizard/AppiumServerSetup.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Converting null literal or possible null value to non-nullable type.
{
if (!command.Contains("--use-plugins=inspector"))
{
Expand Down Expand Up @@ -135,7 +136,7 @@
appiumServerProcess.StartInfo = startInfo;
appiumServerProcess.OutputDataReceived += (sender, e) => AppiumServer_OutputDataReceived(sender, e, serverNumber, webDriverAgentProxyPort);
appiumServerProcess.ErrorDataReceived += (sender, e) => AppiumServer_OutputDataReceived(sender, e, serverNumber, webDriverAgentProxyPort);
appiumServerProcess.Exited += (sender, e) => AppiumServer_ProcessExited(sender, e, serverNumber);

Check warning on line 139 in Appium Wizard/AppiumServerSetup.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference argument for parameter 'sender' in 'void AppiumServerSetup.AppiumServer_ProcessExited(object sender, EventArgs e, int serverNumber)'.
appiumServerProcess.EnableRaisingEvents = true;

appiumServerProcess.Start();
Expand Down Expand Up @@ -170,18 +171,27 @@
}
}

string deviceUDID = "none"; string currentSessionId = "none"; string currentUDID = "none";
string currentPlatformName = "none";
//int proxyPort = 0;
int screenDensity = 0;
Dictionary<string, string> sessionIdUDID = new Dictionary<string, string>();
public static ConcurrentDictionary<string, string> sessionIdUDID = new ConcurrentDictionary<string, string>();
// Per-serverNumber state, since a single AppiumServerSetup instance's OutputDataReceived
// handler can be invoked concurrently for multiple devices/processes.
private static readonly ConcurrentDictionary<int, string> deviceUDIDByServer = new ConcurrentDictionary<int, string>();
private static readonly ConcurrentDictionary<int, string> currentSessionIdByServer = new ConcurrentDictionary<int, string>();
private static readonly ConcurrentDictionary<int, string> currentUDIDByServer = new ConcurrentDictionary<int, string>();
private static readonly ConcurrentDictionary<int, string> currentPlatformNameByServer = new ConcurrentDictionary<int, string>();
private static readonly ConcurrentDictionary<int, string> proxiedUDIDByServer = new ConcurrentDictionary<int, string>();
ExecutionStatus executionStatus = new ExecutionStatus();
string proxiedUDID = "";
private DateTime lastExecutionTime = DateTime.MinValue;
bool isWelcomeDisplayed;
string appiumWarning;
public void AppiumServer_OutputDataReceived(object sender, DataReceivedEventArgs e, int serverNumber, int webDriverAgentProxyPort)
{
string deviceUDID = deviceUDIDByServer.GetOrAdd(serverNumber, "none");
string currentSessionId = currentSessionIdByServer.GetOrAdd(serverNumber, "none");
string currentUDID = currentUDIDByServer.GetOrAdd(serverNumber, "none");
string currentPlatformName = currentPlatformNameByServer.GetOrAdd(serverNumber, "none");
string proxiedUDID = proxiedUDIDByServer.GetOrAdd(serverNumber, "");
try
{
if (!string.IsNullOrEmpty(e.Data))
Expand Down Expand Up @@ -304,7 +314,7 @@
if (match1.Success)
{
currentSessionId = match1.Groups[1].Value;
sessionIdUDID.Add(currentSessionId, currentUDID);
sessionIdUDID[currentSessionId] = currentUDID;
if (MainScreen.udidProxyPort.ContainsKey(deviceUDID))
{
MainScreen.udidProxyPort[currentUDID] = webDriverAgentProxyPort;
Expand Down Expand Up @@ -381,7 +391,7 @@
if (sessionIdUDID.ContainsKey(sessionId))
{
udid = sessionIdUDID[sessionId];
sessionIdUDID.Remove(sessionId);
sessionIdUDID.TryRemove(sessionId, out _);
if (MainScreen.DeviceInfo.ContainsKey(udid))
{
string name = MainScreen.DeviceInfo[udid].Item1;
Expand Down Expand Up @@ -454,6 +464,14 @@
catch (Exception)
{
}
finally
{
deviceUDIDByServer[serverNumber] = deviceUDID;
currentSessionIdByServer[serverNumber] = currentSessionId;
currentUDIDByServer[serverNumber] = currentUDID;
currentPlatformNameByServer[serverNumber] = currentPlatformName;
proxiedUDIDByServer[serverNumber] = proxiedUDID;
}
}

public bool processExited;
Expand Down Expand Up @@ -555,8 +573,9 @@
}
}

public static bool isExpectedDataAvailableInSessionDetails(string data)
public static bool isExpectedDataAvailableInSessionDetails(string udid, string data)
{
string sessionId = sessionIdUDID.FirstOrDefault(x => x.Value == udid).Key;
Dictionary<string, string> readPortData = Database.QueryDataFromPortNumberTable();
int port = 0;
for (int i = 1; i <= 5; i++)
Expand All @@ -574,38 +593,15 @@
{
continue;
}
var options = new RestClientOptions("http://localhost:" + port)
{
Timeout = TimeSpan.FromSeconds(5),
};
var client = new RestClient(options);
var request = new RestRequest("/sessions", Method.Get);
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
if (response.StatusCode == HttpStatusCode.OK && response.Content != null)

string androidId = GetAndroidIdFromAppiumServer(port, sessionId);
if (androidId.Equals(data))
{
if (response.Content.Equals("{\"value\":[]}"))
{
continue;
}
else
{
JObject responseObj = JObject.Parse(response.Content);
string sessionId = responseObj["value"]?[0]?["id"]?.ToString() ?? string.Empty;
string androidId = GetAndroidIdFromAppiumServer(port, sessionId);
if (androidId.Equals(data))
{
return true;
}
else
{
continue;
}
}
return true;
}
else
{
return false;
continue;
}

}
Expand Down Expand Up @@ -635,14 +631,14 @@
{
try
{
var options = new RestClientOptions("http://localhost:" + appiumPort)
var options = new RestClientOptions("http://127.0.0.1:"+appiumPort)
{
Timeout = TimeSpan.FromSeconds(5),
};
var client = new RestClient(options);
var request = new RestRequest("/session/" + appiumSessionId + "/execute", Method.Post);
var request = new RestRequest("/session/"+appiumSessionId+"/execute/sync", Method.Post);
request.AddHeader("Content-Type", "application/json");
var body = @"{""script"":""mobile:deviceInfo"",""args"":[]}";
var body = @"{""script"":""mobile:deviceInfo"",""args"": []}";
request.AddStringBody(body, DataFormat.Json);
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Expand Down
17 changes: 10 additions & 7 deletions Appium Wizard/OpenDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,15 @@ await Task.Run(() =>
try
{
commonProgress.UpdateStepLabel(title, "Getting screen density...", 5);
try
{
MainScreen.udidScreenDensity[udid] = AndroidMethods.GetInstance().GetScreenDensity(udid);
}
catch (Exception)
if (!MainScreen.useScrcpy)
{
try
{
MainScreen.udidScreenDensity[udid] = AndroidMethods.GetInstance().GetScreenDensity(udid);
}
catch (Exception)
{
}
}
commonProgress.UpdateStepLabel(title, "Checking UIAutomator installation...", 10);
AndroidMethods.GetInstance().UninstallOtherInstrumentationApps(udid);
Expand Down Expand Up @@ -609,13 +612,13 @@ await Task.Run(() =>
if (isSessionCreated)
{
string androidId = AppiumServerSetup.GetAndroidId(proxyPort, sessionIdAvailableForAutomation);
isItValidSession = AppiumServerSetup.isExpectedDataAvailableInSessionDetails(androidId);
isItValidSession = AppiumServerSetup.isExpectedDataAvailableInSessionDetails(udid,androidId);
if (isItValidSession == false)
{
isSessionCreated = false;
}
}
if (isSessionCreated && !isItValidSession)
if (!isItValidSession)
{
commonProgress.UpdateStepLabel(title, "Restarting UIAutomator...", 80);
AndroidMethods.GetInstance().StopUIAutomator(udid);
Expand Down
Loading