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;
namespace MasterDevs.ChromeDevTools
......@@ -14,12 +15,9 @@ namespace MasterDevs.ChromeDevTools
var chromeProcessArgs = remoteDebuggingArg + " " + userDirectoryArg + " --bwsi --no-first-run";
var processStartInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", chromeProcessArgs);
var chromeProcess = Process.Start(processStartInfo);
return new ChromeProcess
{
Process = chromeProcess,
UserDirectory = directoryInfo,
RemoteDebuggingUri = "http://localhost:" + port
};
string remoteDebuggingUrl = "http://localhost:" + port;
return new LocalChromeProcess(new Uri(remoteDebuggingUrl), directoryInfo, chromeProcess);
}
}
}
\ 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.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace MasterDevs.ChromeDevTools
{
public interface IChromeProcess : IDisposable
{
Task<string[]> GetSessions();
Task<ChromeSessionInfo[]> GetSessions();
DirectoryInfo UserDirectory { get; }
Process Process { get; }
string RemoteDebuggingUri { get; }
Uri 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 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ChromeProcessFactory.cs" />
<Compile Include="ChromeProcess.cs" />
<Compile Include="LocalChromeProcess.cs" />
<Compile Include="ChromeSession.cs" />
<Compile Include="ChromeSessionExtensions.cs" />
<Compile Include="ChromeSessionFactory.cs" />
<Compile Include="ChromeSessionInfo.cs" />
<Compile Include="Command.cs" />
<Compile Include="CommandAttribute.cs" />
<Compile Include="Extensions\JTokenExtensions.cs" />
......@@ -90,6 +91,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ProtocolNameAttribute.cs" />
<Compile Include="Protocol\**\*.cs" />
<Compile Include="RemoteChromeProcess.cs" />
<Compile Include="Serialization\MessageContractResolver.cs" />
<Compile Include="SupportedByAttribute.cs" />
</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 System;
using System.Linq;
using Task = System.Threading.Tasks.Task;
namespace MasterDevs.ChromeDevTools.Sample
{
......@@ -8,43 +9,46 @@ namespace MasterDevs.ChromeDevTools.Sample
{
private static void Main(string[] args)
{
// STEP 1 - Run Chrome
var chromeProcessFactory = new ChromeProcessFactory();
using (var chromeProcess = chromeProcessFactory.Create(9222))
Task.Run(async () =>
{
// STEP 2 - Create a debugging session
var endpointUrl = chromeProcess.GetSessions().Result.LastOrDefault();
var chromeSessionFactory = new ChromeSessionFactory();
var chromeSession = chromeSessionFactory.Create(endpointUrl);
// STEP 1 - Run Chrome
var chromeProcessFactory = new ChromeProcessFactory();
using (var chromeProcess = chromeProcessFactory.Create(9222))
{
// STEP 2 - Create a debugging session
var session = (await chromeProcess.GetSessions()).LastOrDefault();
var chromeSessionFactory = new ChromeSessionFactory();
var chromeSession = chromeSessionFactory.Create(session.WebSocketDebuggerUrl);
// STEP 3 - Send a command
//
// Here we are sending a command to tell chrome to navigate to
// the specified URL
var navigateResponse = chromeSession.SendAsync(new NavigateCommand
// STEP 3 - Send a command
//
// Here we are sending a command to tell chrome to navigate to
// the specified URL
var navigateResponse = chromeSession.SendAsync(new NavigateCommand
{
Url = "http://www.google.com"
})
.Result;
Console.WriteLine("NavigateResponse: " + navigateResponse.Id);
.Result;
Console.WriteLine("NavigateResponse: " + navigateResponse.Id);
// STEP 4 - Register for events (in this case, "Page" domain events)
// send an event to tell chrome to send us all Page events
// but we only subscribe to certain events in this session
var pageEnableResult = chromeSession.SendAsync<ChromeDevTools.Protocol.Chrome.Page.EnableCommand>().Result;
Console.WriteLine("PageEnable: " + pageEnableResult.Id);
chromeSession.Subscribe<Protocol.Chrome.Page.DomContentEventFiredEvent>(domContentEvent =>
// STEP 4 - Register for events (in this case, "Page" domain events)
// send an event to tell chrome to send us all Page events
// but we only subscribe to certain events in this session
var pageEnableResult = chromeSession.SendAsync<ChromeDevTools.Protocol.Chrome.Page.EnableCommand>().Result;
Console.WriteLine("PageEnable: " + pageEnableResult.Id);
chromeSession.Subscribe<Protocol.Chrome.Page.DomContentEventFiredEvent>(domContentEvent =>
{
Console.WriteLine("DomContentEvent: " + domContentEvent.Timestamp);
});
// you might never see this, but that's what an event is ... right?
chromeSession.Subscribe<Protocol.Chrome.Page.FrameStartedLoadingEvent>(frameStartedLoadingEvent =>
// you might never see this, but that's what an event is ... right?
chromeSession.Subscribe<Protocol.Chrome.Page.FrameStartedLoadingEvent>(frameStartedLoadingEvent =>
{
Console.WriteLine("FrameStartedLoading: " + frameStartedLoadingEvent.FrameId);
});
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