Skip to content

Commit d9fa359

Browse files
Meir017kblok
authored andcommitted
New LaunchOptions config class (#71)
* added LaunchOptions file * use new launch options * cleaned launcher options
1 parent 112c6d2 commit d9fa359

File tree

7 files changed

+66
-45
lines changed

7 files changed

+66
-45
lines changed

lib/PuppeteerSharp.Tests/Puppeteer/PuppeteerLaunchTests.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics.Contracts;
4-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
52
using Xunit;
6-
using PuppeteerSharp;
7-
using PuppeteerSharp.Helpers;
83

94
namespace PuppeteerSharp.Tests.Puppeteer
105
{
@@ -18,8 +13,8 @@ public PuppeteerLaunchTests()
1813
[Fact]
1914
public async Task ShouldSupportIgnoreHTTPSErrorsOption()
2015
{
21-
var options = TestConstants.DefaultBrowserOptions.Clone();
22-
options.Add("ignoreHTTPSErrors", true);
16+
var options = TestConstants.DefaultBrowserOptions();
17+
options.IgnoreHTTPSErrors = true;
2318

2419
var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(options, TestConstants.ChromiumRevision);
2520
var page = await browser.NewPageAsync();

lib/PuppeteerSharp.Tests/PuppeteerBaseTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public PuppeteerBaseTest()
1818
dirInfo.Create();
1919
}
2020

21-
Browser = PuppeteerSharp.Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions,
21+
Browser = PuppeteerSharp.Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions(),
2222
TestConstants.ChromiumRevision).GetAwaiter().GetResult();
2323
}
2424

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32

43
namespace PuppeteerSharp.Tests
54
{
@@ -11,13 +10,13 @@ public static class TestConstants
1110
public const int ChromiumRevision = 526987;
1211
public static readonly string EmptyPage = $"{ServerUrl}/empty.html";
1312

14-
public static readonly Dictionary<string, object> DefaultBrowserOptions = new Dictionary<string, object>()
13+
public static LaunchOptions DefaultBrowserOptions() => new LaunchOptions
1514
{
16-
{ "slowMo", Convert.ToInt32(Environment.GetEnvironmentVariable("SLOW_MO") ?? "0") },
17-
{ "headless", Convert.ToBoolean(Environment.GetEnvironmentVariable("HEADLESS") ?? "true") },
18-
{ "args", new[] { "--no-sandbox" }},
19-
{ "timeout", 0},
20-
{ "keepAliveInterval", 120}
15+
SlowMo = Convert.ToInt32(Environment.GetEnvironmentVariable("SLOW_MO")),
16+
Headless = Convert.ToBoolean(Environment.GetEnvironmentVariable("HEADLESS") ?? "true"),
17+
Args = new[] { "--no-sandbox" },
18+
Timeout = 0,
19+
KeepAliveInterval = 120
2120
};
2221
}
2322
}

lib/PuppeteerSharp/Browser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ namespace PuppeteerSharp
77
{
88
public class Browser
99
{
10-
public Browser(Connection connection, Dictionary<string, object> options, Func<Task> closeCallBack)
10+
public Browser(Connection connection, LaunchOptions options, Func<Task> closeCallBack)
1111
{
1212
Connection = connection;
13-
IgnoreHTTPSErrors = options.ContainsKey("ignoreHTTPSErrors") && (bool)options["ignoreHTTPSErrors"];
14-
AppMode = options.ContainsKey("appMode") && (bool)options["appMode"];
13+
IgnoreHTTPSErrors = options.IgnoreHTTPSErrors;
14+
AppMode = options.AppMode;
1515
_targets = new Dictionary<string, Target>();
1616
ScreenshotTaskQueue = new TaskQueue();
1717

@@ -157,7 +157,7 @@ private async Task CreateTarget(MessageEventArgs args)
157157

158158
}
159159

160-
internal static async Task<Browser> CreateAsync(Connection connection, Dictionary<string, object> options,
160+
internal static async Task<Browser> CreateAsync(Connection connection, LaunchOptions options,
161161
Func<Task> closeCallBack)
162162
{
163163
var browser = new Browser(connection, options, closeCallBack);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace PuppeteerSharp
5+
{
6+
public class LaunchOptions
7+
{
8+
public bool AppMode { get; set; }
9+
10+
public bool IgnoreHTTPSErrors { get; set; }
11+
12+
public bool Headless { get; set; } = true;
13+
14+
public string ExecutablePath { get; set; }
15+
16+
public int SlowMo { get; set; }
17+
18+
public string[] Args { get; set; } = Array.Empty<string>();
19+
20+
public int Timeout { get; set; } = 30_000;
21+
22+
public bool DumpIO { get; set; }
23+
24+
public string UserDataDir { get; set; }
25+
26+
public IDictionary<string, string> Env { get; } = new Dictionary<string, string>();
27+
28+
public bool Devtools { get; set; }
29+
30+
public int KeepAliveInterval { get; set; } = 30;
31+
}
32+
}

lib/PuppeteerSharp/Launcher.cs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace PuppeteerSharp
1313
{
1414
public class Launcher
1515
{
16-
private static string[] _defaultArgs = {
16+
private static readonly string[] _defaultArgs = {
1717
"--disable-background-networking",
1818
"--disable-background-timer-throttling",
1919
"--disable-client-side-phishing-detection",
@@ -30,7 +30,7 @@ public class Launcher
3030
"--safebrowsing-disable-auto-update",
3131
};
3232

33-
public static string[] _automationArgs = {
33+
public static readonly string[] _automationArgs = {
3434
"--enable-automation",
3535
"--password-store=basic",
3636
"--use-mock-keychain"
@@ -42,40 +42,39 @@ public class Launcher
4242
private Connection _connection = null;
4343
private Timer _timer = null;
4444

45-
internal async Task<Browser> LaunchAsync(Dictionary<string, object> options, int chromiumRevision)
45+
internal async Task<Browser> LaunchAsync(LaunchOptions options, int chromiumRevision)
4646
{
4747
var chromeArguments = new List<string>(_defaultArgs);
4848

49-
if (options.ContainsKey("appMode"))
49+
if (options.AppMode)
5050
{
51-
options["headless"] = false;
51+
options.Headless = false;
5252
}
5353
else
5454
{
5555
chromeArguments.AddRange(_automationArgs);
5656
}
5757

58-
if (options.ContainsKey("args") &&
59-
((string[])options["args"]).Any(i => i.StartsWith("--user-data-dir", StringComparison.Ordinal)))
58+
if (options.Args.Any(i => i.StartsWith("--user-data-dir", StringComparison.Ordinal)))
6059
{
61-
if (!options.ContainsKey("userDataDir"))
60+
if (string.IsNullOrEmpty(options.UserDataDir))
6261
{
6362
_temporaryUserDataDir = GetTemporaryDirectory();
6463
chromeArguments.Add($"--user-data-dir=${_temporaryUserDataDir}");
6564
}
6665
else
6766
{
68-
chromeArguments.Add($"--user-data-dir=${options["userDataDir"]}");
67+
chromeArguments.Add($"--user-data-dir=${options.UserDataDir}");
6968
}
7069
}
7170

72-
if (options.TryGetValue("devtools", out var hasDevTools) && (bool)hasDevTools)
71+
if (options.Devtools)
7372
{
7473
chromeArguments.Add("--auto-open-devtools-for-tabs");
75-
options["headless"] = false;
74+
options.Headless = false;
7675
}
7776

78-
if (options.TryGetValue("headless", out var isHeadless) && (bool)isHeadless)
77+
if (options.Headless)
7978
{
8079
chromeArguments.AddRange(new[]{
8180
"--headless",
@@ -85,7 +84,7 @@ internal async Task<Browser> LaunchAsync(Dictionary<string, object> options, int
8584
});
8685
}
8786

88-
var chromeExecutable = (options.GetValueOrDefault("executablePath") ?? "").ToString();
87+
var chromeExecutable = options.ExecutablePath;
8988

9089
if (string.IsNullOrEmpty(chromeExecutable))
9190
{
@@ -94,20 +93,18 @@ internal async Task<Browser> LaunchAsync(Dictionary<string, object> options, int
9493
chromeExecutable = revisionInfo.ExecutablePath;
9594
}
9695

97-
if (options.ContainsKey("args"))
96+
if (options.Args.Any())
9897
{
99-
chromeArguments.AddRange((string[])options["args"]);
98+
chromeArguments.AddRange(options.Args);
10099
}
101100

102101
_chromeProcess = new Process();
103102
_chromeProcess.StartInfo.FileName = chromeExecutable;
104103
_chromeProcess.StartInfo.Arguments = string.Join(" ", chromeArguments);
105104

106-
SetEnvVariables(_chromeProcess.StartInfo.Environment,
107-
options.ContainsKey("env") ? (IDictionary<string, string>)options["env"] : null,
108-
(IDictionary)Environment.GetEnvironmentVariables());
105+
SetEnvVariables(_chromeProcess.StartInfo.Environment, options.Env, Environment.GetEnvironmentVariables());
109106

110-
if (!options.ContainsKey("dumpio"))
107+
if(!options.DumpIO)
111108
{
112109
_chromeProcess.StartInfo.RedirectStandardOutput = false;
113110
_chromeProcess.StartInfo.RedirectStandardError = false;
@@ -121,11 +118,9 @@ internal async Task<Browser> LaunchAsync(Dictionary<string, object> options, int
121118

122119
try
123120
{
124-
var connectionDelay = (int)(options.GetValueOrDefault("slowMo") ?? 0);
125-
var browserWSEndpoint = await WaitForEndpoint(_chromeProcess,
126-
(int)(options.GetValueOrDefault("timeout") ?? 30 * 100),
127-
options.ContainsKey("dumpio"));
128-
var keepAliveInterval = options.ContainsKey("keepAliveInterval") ? Convert.ToInt32(options["keepAliveInterval"]) : 30;
121+
var connectionDelay = options.SlowMo;
122+
var browserWSEndpoint = await WaitForEndpoint(_chromeProcess, options.Timeout, options.DumpIO);
123+
var keepAliveInterval = options.KeepAliveInterval;
129124

130125
_connection = await Connection.Create(browserWSEndpoint, connectionDelay, keepAliveInterval);
131126
return await Browser.CreateAsync(_connection, options, KillChrome);

lib/PuppeteerSharp/Puppeteer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace PuppeteerSharp
66
{
77
public class Puppeteer
88
{
9-
public static async Task<Browser> LaunchAsync(Dictionary<string, object> options, int chromiumRevision)
9+
public static async Task<Browser> LaunchAsync(LaunchOptions options, int chromiumRevision)
1010
{
1111
return await new Launcher().LaunchAsync(options, chromiumRevision);
1212
}

0 commit comments

Comments
 (0)