From 700d4096626b3273bd89fefbdc2167827efb6544 Mon Sep 17 00:00:00 2001 From: svatal Date: Wed, 25 Oct 2017 13:37:22 +0200 Subject: [PATCH] chrome - ability to set headless mode and path to chrome --- source/ChromeDevTools/ChromeProcessFactory.cs | 19 +++++++++++++++---- .../ChromeDevTools/IChromeProcessFactory.cs | 2 +- source/Sample/Program.cs | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/source/ChromeDevTools/ChromeProcessFactory.cs b/source/ChromeDevTools/ChromeProcessFactory.cs index 494568ef..85017bf1 100644 --- a/source/ChromeDevTools/ChromeProcessFactory.cs +++ b/source/ChromeDevTools/ChromeProcessFactory.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -7,21 +8,31 @@ namespace MasterDevs.ChromeDevTools public class ChromeProcessFactory : IChromeProcessFactory { public IDirectoryCleaner DirectoryCleaner { get; set; } + public string ChromePath { get; } - public ChromeProcessFactory(IDirectoryCleaner directoryCleaner) + public ChromeProcessFactory(IDirectoryCleaner directoryCleaner, string chromePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe") { DirectoryCleaner = directoryCleaner; + ChromePath = chromePath; } - public IChromeProcess Create(int port) + public IChromeProcess Create(int port, bool headless) { string path = Path.GetRandomFileName(); var directoryInfo = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), path)); var remoteDebuggingArg = $"--remote-debugging-port={port}"; var userDirectoryArg = $"--user-data-dir=\"{directoryInfo.FullName}\""; 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 chromeProcessArgs = new List + { + remoteDebuggingArg, + userDirectoryArg, + "--bwsi", + "--no-first-run" + }; + if (headless) + chromeProcessArgs.Add(headlessArg); + var processStartInfo = new ProcessStartInfo(ChromePath, string.Join(" ", chromeProcessArgs)); var chromeProcess = Process.Start(processStartInfo); string remoteDebuggingUrl = "http://localhost:" + port; diff --git a/source/ChromeDevTools/IChromeProcessFactory.cs b/source/ChromeDevTools/IChromeProcessFactory.cs index ddf57955..fcac1ce2 100644 --- a/source/ChromeDevTools/IChromeProcessFactory.cs +++ b/source/ChromeDevTools/IChromeProcessFactory.cs @@ -2,6 +2,6 @@ { public interface IChromeProcessFactory { - IChromeProcess Create(int port); + IChromeProcess Create(int port, bool headless); } } \ No newline at end of file diff --git a/source/Sample/Program.cs b/source/Sample/Program.cs index 2d42013a..7b3761d0 100644 --- a/source/Sample/Program.cs +++ b/source/Sample/Program.cs @@ -22,7 +22,7 @@ private static void Main(string[] args) // STEP 1 - Run Chrome var chromeProcessFactory = new ChromeProcessFactory(new StubbornDirectoryCleaner()); - using (var chromeProcess = chromeProcessFactory.Create(9222)) + using (var chromeProcess = chromeProcessFactory.Create(9222, true)) { // STEP 2 - Create a debugging session var sessionInfo = (await chromeProcess.GetSessionInfo()).LastOrDefault();