Skip to content
Snippets Groups Projects
Commit 4f74d04e authored by svatal's avatar svatal
Browse files

run chrome in headless mode & wait for user dir deletion

parent 0e1d4a7c
No related branches found
No related tags found
No related merge requests found
...@@ -6,18 +6,26 @@ namespace MasterDevs.ChromeDevTools ...@@ -6,18 +6,26 @@ namespace MasterDevs.ChromeDevTools
{ {
public class ChromeProcessFactory : IChromeProcessFactory public class ChromeProcessFactory : IChromeProcessFactory
{ {
public IDirectoryCleaner DirectoryCleaner { get; set; }
public ChromeProcessFactory(IDirectoryCleaner directoryCleaner)
{
DirectoryCleaner = directoryCleaner;
}
public IChromeProcess Create(int port) public IChromeProcess Create(int port)
{ {
string path = Path.GetRandomFileName(); string path = Path.GetRandomFileName();
var directoryInfo = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), path)); var directoryInfo = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), path));
var remoteDebuggingArg = "--remote-debugging-port=" + port; var remoteDebuggingArg = $"--remote-debugging-port={port}";
var userDirectoryArg = "--user-data-dir=\"" + directoryInfo.FullName + "\""; var userDirectoryArg = $"--user-data-dir=\"{directoryInfo.FullName}\"";
var chromeProcessArgs = remoteDebuggingArg + " " + userDirectoryArg + " --bwsi --no-first-run"; const string headlessArg = "--headless --disable-gpu";
var chromeProcessArgs = $"{headlessArg} {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);
string remoteDebuggingUrl = "http://localhost:" + port; string remoteDebuggingUrl = "http://localhost:" + port;
return new LocalChromeProcess(new Uri(remoteDebuggingUrl), directoryInfo, chromeProcess); return new LocalChromeProcess(new Uri(remoteDebuggingUrl), () => DirectoryCleaner.Delete(directoryInfo), chromeProcess);
} }
} }
} }
\ No newline at end of file
using System.IO;
namespace MasterDevs.ChromeDevTools
{
public interface IDirectoryCleaner
{
void Delete(DirectoryInfo dir);
}
}
\ No newline at end of file
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Threading;
namespace MasterDevs.ChromeDevTools namespace MasterDevs.ChromeDevTools
{ {
public class LocalChromeProcess : RemoteChromeProcess public class LocalChromeProcess : RemoteChromeProcess
{ {
public LocalChromeProcess(Uri remoteDebuggingUri, DirectoryInfo userDirectory, Process process) public LocalChromeProcess(Uri remoteDebuggingUri, Action disposeUserDirectory, Process process)
: base(remoteDebuggingUri) : base(remoteDebuggingUri)
{ {
UserDirectory = userDirectory; DisposeUserDirectory = disposeUserDirectory;
Process = process; Process = process;
} }
public DirectoryInfo UserDirectory { get; set; } public Action DisposeUserDirectory { get; set; }
public Process Process { get; set; } public Process Process { get; set; }
public override void Dispose() public override void Dispose()
...@@ -23,15 +20,7 @@ namespace MasterDevs.ChromeDevTools ...@@ -23,15 +20,7 @@ namespace MasterDevs.ChromeDevTools
base.Dispose(); base.Dispose();
Process.Kill(); Process.Kill();
try DisposeUserDirectory();
{
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,6 +60,7 @@ ...@@ -60,6 +60,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ChromeProcessFactory.cs" /> <Compile Include="ChromeProcessFactory.cs" />
<Compile Include="IDirectoryCleaner.cs" />
<Compile Include="LocalChromeProcess.cs" /> <Compile Include="LocalChromeProcess.cs" />
<Compile Include="ChromeSession.cs" /> <Compile Include="ChromeSession.cs" />
<Compile Include="ChromeSessionExtensions.cs" /> <Compile Include="ChromeSessionExtensions.cs" />
...@@ -93,6 +94,7 @@ ...@@ -93,6 +94,7 @@
<Compile Include="Protocol\**\*.cs" /> <Compile Include="Protocol\**\*.cs" />
<Compile Include="RemoteChromeProcess.cs" /> <Compile Include="RemoteChromeProcess.cs" />
<Compile Include="Serialization\MessageContractResolver.cs" /> <Compile Include="Serialization\MessageContractResolver.cs" />
<Compile Include="StubbornDirectoryCleaner.cs" />
<Compile Include="SupportedByAttribute.cs" /> <Compile Include="SupportedByAttribute.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
......
using System.IO;
using System.Threading;
namespace MasterDevs.ChromeDevTools
{
public class StubbornDirectoryCleaner : IDirectoryCleaner
{
public void Delete(DirectoryInfo dir)
{
while (true)
{
try
{
dir.Delete(true);
return;
}
catch
{
Thread.Sleep(500);
}
}
}
}
}
\ No newline at end of file
...@@ -12,7 +12,7 @@ namespace MasterDevs.ChromeDevTools.Sample ...@@ -12,7 +12,7 @@ namespace MasterDevs.ChromeDevTools.Sample
Task.Run(async () => Task.Run(async () =>
{ {
// STEP 1 - Run Chrome // STEP 1 - Run Chrome
var chromeProcessFactory = new ChromeProcessFactory(); var chromeProcessFactory = new ChromeProcessFactory(new StubbornDirectoryCleaner());
using (var chromeProcess = chromeProcessFactory.Create(9222)) using (var chromeProcess = chromeProcessFactory.Create(9222))
{ {
// STEP 2 - Create a debugging session // STEP 2 - Create a debugging session
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment