Skip to content

Commit

Permalink
chrome - ability to set headless mode and path to chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
svatal committed Oct 25, 2017
1 parent 81f12e8 commit 700d409
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
19 changes: 15 additions & 4 deletions source/ChromeDevTools/ChromeProcessFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

Expand All @@ -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<string>
{
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;
Expand Down
2 changes: 1 addition & 1 deletion source/ChromeDevTools/IChromeProcessFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface IChromeProcessFactory
{
IChromeProcess Create(int port);
IChromeProcess Create(int port, bool headless);
}
}
2 changes: 1 addition & 1 deletion source/Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 700d409

Please sign in to comment.