Skip to content
Snippets Groups Projects
Commit d29593e2 authored by Georgios Diamantopoulos's avatar Georgios Diamantopoulos
Browse files

split process object to allow connecting to a remote instance instead of having to instantiate it

parent c61610a1
No related branches found
No related tags found
No related merge requests found
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace MasterDevs.ChromeDevTools
{
public class ChromeProcess : IChromeProcess
{
public DirectoryInfo UserDirectory { get; set; }
public Process Process { get; set; }
public string RemoteDebuggingUri { get; set; }
public void Dispose()
{
Process.Kill();
try
{
UserDirectory.Delete(true);
}
catch
{
Thread.Sleep(500); // i'm being lazy because i'm tired
UserDirectory.Delete(true);
}
}
public async Task<string[]> GetSessions()
{
var remoteSessionUrls = new List<string>();
var webClient = new HttpClient();
var uriBuilder = new UriBuilder(RemoteDebuggingUri);
uriBuilder.Path = "/json";
var remoteSessions = await webClient.GetStringAsync(uriBuilder.Uri);
using (var stringReader = new StringReader(remoteSessions))
using (var jsonReader = new JsonTextReader(stringReader))
{
var sessionsObject = JToken.ReadFrom(jsonReader) as JArray;
foreach (var sessionObject in sessionsObject)
{
var sessionUrl = sessionObject["webSocketDebuggerUrl"].GetSafeString();
if (!String.IsNullOrEmpty(sessionUrl))
{
remoteSessionUrls.Add(sessionUrl);
}
}
}
return remoteSessionUrls.ToArray();
}
}
}
\ No newline at end of file
using System.Diagnostics; using System;
using System.Diagnostics;
using System.IO; using System.IO;
namespace MasterDevs.ChromeDevTools namespace MasterDevs.ChromeDevTools
...@@ -14,12 +15,9 @@ namespace MasterDevs.ChromeDevTools ...@@ -14,12 +15,9 @@ namespace MasterDevs.ChromeDevTools
var chromeProcessArgs = remoteDebuggingArg + " " + userDirectoryArg + " --bwsi --no-first-run"; var chromeProcessArgs = remoteDebuggingArg + " " + userDirectoryArg + " --bwsi --no-first-run";
var processStartInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", chromeProcessArgs); var processStartInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", chromeProcessArgs);
var chromeProcess = Process.Start(processStartInfo); var chromeProcess = Process.Start(processStartInfo);
return new ChromeProcess
{ string remoteDebuggingUrl = "http://localhost:" + port;
Process = chromeProcess, return new LocalChromeProcess(new Uri(remoteDebuggingUrl), directoryInfo, chromeProcess);
UserDirectory = directoryInfo,
RemoteDebuggingUri = "http://localhost:" + port
};
} }
} }
} }
\ No newline at end of file
namespace MasterDevs.ChromeDevTools
{
public class ChromeSessionInfo
{
public string Description { get; set; }
public string DevtoolsFrontendUrl { get; set; }
public string Id { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public string Url { get; set; }
public string WebSocketDebuggerUrl { get; set; }
}
}
\ No newline at end of file
using System; using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MasterDevs.ChromeDevTools namespace MasterDevs.ChromeDevTools
{ {
public interface IChromeProcess : IDisposable public interface IChromeProcess : IDisposable
{ {
Task<string[]> GetSessions(); Task<ChromeSessionInfo[]> GetSessions();
DirectoryInfo UserDirectory { get; } Uri RemoteDebuggingUri { get; }
Process Process { get; }
string RemoteDebuggingUri { get; }
} }
} }
\ No newline at end of file
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace MasterDevs.ChromeDevTools
{
public class LocalChromeProcess : RemoteChromeProcess
{
public LocalChromeProcess(Uri remoteDebuggingUri, DirectoryInfo userDirectory, Process process)
: base(remoteDebuggingUri)
{
UserDirectory = userDirectory;
Process = process;
}
public DirectoryInfo UserDirectory { get; set; }
public Process Process { get; set; }
public override void Dispose()
{
Process.Kill();
try
{
UserDirectory.Delete(true);
}
catch
{
Thread.Sleep(500); // i'm being lazy because i'm tired
UserDirectory.Delete(true);
}
}
}
}
\ No newline at end of file
...@@ -60,10 +60,11 @@ ...@@ -60,10 +60,11 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ChromeProcessFactory.cs" /> <Compile Include="ChromeProcessFactory.cs" />
<Compile Include="ChromeProcess.cs" /> <Compile Include="LocalChromeProcess.cs" />
<Compile Include="ChromeSession.cs" /> <Compile Include="ChromeSession.cs" />
<Compile Include="ChromeSessionExtensions.cs" /> <Compile Include="ChromeSessionExtensions.cs" />
<Compile Include="ChromeSessionFactory.cs" /> <Compile Include="ChromeSessionFactory.cs" />
<Compile Include="ChromeSessionInfo.cs" />
<Compile Include="Command.cs" /> <Compile Include="Command.cs" />
<Compile Include="CommandAttribute.cs" /> <Compile Include="CommandAttribute.cs" />
<Compile Include="Extensions\JTokenExtensions.cs" /> <Compile Include="Extensions\JTokenExtensions.cs" />
...@@ -90,6 +91,7 @@ ...@@ -90,6 +91,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ProtocolNameAttribute.cs" /> <Compile Include="ProtocolNameAttribute.cs" />
<Compile Include="Protocol\**\*.cs" /> <Compile Include="Protocol\**\*.cs" />
<Compile Include="RemoteChromeProcess.cs" />
<Compile Include="Serialization\MessageContractResolver.cs" /> <Compile Include="Serialization\MessageContractResolver.cs" />
<Compile Include="SupportedByAttribute.cs" /> <Compile Include="SupportedByAttribute.cs" />
</ItemGroup> </ItemGroup>
......
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace MasterDevs.ChromeDevTools
{
public class RemoteChromeProcess : IChromeProcess
{
public RemoteChromeProcess(Uri remoteDebuggingUri)
{
RemoteDebuggingUri = remoteDebuggingUri;
}
public Uri RemoteDebuggingUri { get; }
public virtual void Dispose()
{
}
public async Task<ChromeSessionInfo[]> GetSessions()
{
using (var http = new HttpClient
{
BaseAddress = RemoteDebuggingUri
})
{
string json = await http.GetStringAsync("/json");
return JsonConvert.DeserializeObject<ChromeSessionInfo[]>(json);
}
}
}
}
\ No newline at end of file
using MasterDevs.ChromeDevTools.Protocol.Chrome.Page; using MasterDevs.ChromeDevTools.Protocol.Chrome.Page;
using System; using System;
using System.Linq; using System.Linq;
using Task = System.Threading.Tasks.Task;
namespace MasterDevs.ChromeDevTools.Sample namespace MasterDevs.ChromeDevTools.Sample
{ {
internal class Program internal class Program
{ {
private static void Main(string[] args) private static void Main(string[] args)
{
Task.Run(async () =>
{ {
// STEP 1 - Run Chrome // STEP 1 - Run Chrome
var chromeProcessFactory = new ChromeProcessFactory(); var chromeProcessFactory = new ChromeProcessFactory();
using (var chromeProcess = chromeProcessFactory.Create(9222)) using (var chromeProcess = chromeProcessFactory.Create(9222))
{ {
// STEP 2 - Create a debugging session // STEP 2 - Create a debugging session
var endpointUrl = chromeProcess.GetSessions().Result.LastOrDefault(); var session = (await chromeProcess.GetSessions()).LastOrDefault();
var chromeSessionFactory = new ChromeSessionFactory(); var chromeSessionFactory = new ChromeSessionFactory();
var chromeSession = chromeSessionFactory.Create(endpointUrl); var chromeSession = chromeSessionFactory.Create(session.WebSocketDebuggerUrl);
// STEP 3 - Send a command // STEP 3 - Send a command
// //
...@@ -45,6 +48,7 @@ namespace MasterDevs.ChromeDevTools.Sample ...@@ -45,6 +48,7 @@ namespace MasterDevs.ChromeDevTools.Sample
Console.ReadLine(); Console.ReadLine();
} }
}).Wait();
} }
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment