diff --git a/source/ChromeDevTools/ChromeProcess.cs b/source/ChromeDevTools/ChromeProcess.cs deleted file mode 100644 index 97181b30968a6fef3720d970bf6a217f33df84a3..0000000000000000000000000000000000000000 --- a/source/ChromeDevTools/ChromeProcess.cs +++ /dev/null @@ -1,59 +0,0 @@ -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 diff --git a/source/ChromeDevTools/ChromeProcessFactory.cs b/source/ChromeDevTools/ChromeProcessFactory.cs index ba30cdccb5c20eeb723c66ad0271dcd439f1a535..a0076286de8b3cf12957f4365bcdd345e24f3d81 100644 --- a/source/ChromeDevTools/ChromeProcessFactory.cs +++ b/source/ChromeDevTools/ChromeProcessFactory.cs @@ -1,4 +1,5 @@ -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 diff --git a/source/ChromeDevTools/ChromeSessionInfo.cs b/source/ChromeDevTools/ChromeSessionInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..c08553f5dbe25e07f3ae7af38a780985076691f8 --- /dev/null +++ b/source/ChromeDevTools/ChromeSessionInfo.cs @@ -0,0 +1,14 @@ +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 diff --git a/source/ChromeDevTools/IChromeProcess.cs b/source/ChromeDevTools/IChromeProcess.cs index a6ee6e57458bba29956fb16cb097520cfcafa5b9..5acfd30f46557f5845b8160990d3c24c5ea9ffbc 100644 --- a/source/ChromeDevTools/IChromeProcess.cs +++ b/source/ChromeDevTools/IChromeProcess.cs @@ -1,18 +1,12 @@ 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 diff --git a/source/ChromeDevTools/LocalChromeProcess.cs b/source/ChromeDevTools/LocalChromeProcess.cs new file mode 100644 index 0000000000000000000000000000000000000000..8c2e62999781410c98b410b8ccd048badc188f83 --- /dev/null +++ b/source/ChromeDevTools/LocalChromeProcess.cs @@ -0,0 +1,35 @@ +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 diff --git a/source/ChromeDevTools/MasterDevs.ChromeDevTools.csproj b/source/ChromeDevTools/MasterDevs.ChromeDevTools.csproj index 7d03ee8b2a942b03db17ac477a3bbc5ea05adb46..e3c29d8dab545f90675472330c0bfa497903c7e9 100644 --- a/source/ChromeDevTools/MasterDevs.ChromeDevTools.csproj +++ b/source/ChromeDevTools/MasterDevs.ChromeDevTools.csproj @@ -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> diff --git a/source/ChromeDevTools/RemoteChromeProcess.cs b/source/ChromeDevTools/RemoteChromeProcess.cs new file mode 100644 index 0000000000000000000000000000000000000000..0076d9baa7c9be60adb22e029ac4befc9d9cc053 --- /dev/null +++ b/source/ChromeDevTools/RemoteChromeProcess.cs @@ -0,0 +1,35 @@ +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 diff --git a/source/Sample/Program.cs b/source/Sample/Program.cs index 73f1e0813647df51bad1d6560ec04ec99134e34a..2d957c79bd129351b307fb1cd9eb2c5aacc6dc3a 100644 --- a/source/Sample/Program.cs +++ b/source/Sample/Program.cs @@ -1,6 +1,7 @@ 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