Skip to content

Commit 419a3a8

Browse files
kblokMeir017
authored andcommitted
Launch with Environment Variables (#664)
1 parent e2b9191 commit 419a3a8

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/PuppeteerSharp/Launcher.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public async Task<Browser> ConnectAsync(ConnectOptions options)
108108
/// </summary>
109109
/// <returns>The executable path.</returns>
110110
public static string GetExecutablePath()
111-
=> new BrowserFetcher().RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath;
111+
=> ResolveExecutablePath();
112112

113113
#endregion
114114

@@ -129,8 +129,7 @@ private static string GetOrFetchChromeExecutable(LaunchOptions options)
129129
var chromeExecutable = options.ExecutablePath;
130130
if (string.IsNullOrEmpty(chromeExecutable))
131131
{
132-
var browserFetcher = new BrowserFetcher();
133-
chromeExecutable = browserFetcher.RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath;
132+
chromeExecutable = ResolveExecutablePath();
134133
}
135134

136135
if (!File.Exists(chromeExecutable))
@@ -141,6 +140,38 @@ private static string GetOrFetchChromeExecutable(LaunchOptions options)
141140
return chromeExecutable;
142141
}
143142

143+
private static string ResolveExecutablePath()
144+
{
145+
var executablePath = Environment.GetEnvironmentVariable("PUPPETEER_EXECUTABLE_PATH");
146+
if (!string.IsNullOrEmpty(executablePath))
147+
{
148+
if (!File.Exists(executablePath))
149+
{
150+
throw new FileNotFoundException("Tried to use PUPPETEER_EXECUTABLE_PATH env variable to launch browser but did not find any executable", executablePath);
151+
}
152+
return executablePath;
153+
}
154+
155+
var browserFetcher = new BrowserFetcher();
156+
var revision = Environment.GetEnvironmentVariable("PUPPETEER_CHROMIUM_REVISION");
157+
RevisionInfo revisionInfo;
158+
if (!string.IsNullOrEmpty(revision) && int.TryParse(revision, out var revisionNumber))
159+
{
160+
revisionInfo = browserFetcher.RevisionInfo(revisionNumber);
161+
if (!revisionInfo.Local)
162+
{
163+
throw new FileNotFoundException("Tried to use PUPPETEER_CHROMIUM_REVISION env variable to launch browser but did not find executable", revisionInfo.ExecutablePath);
164+
}
165+
return revisionInfo.ExecutablePath;
166+
}
167+
revisionInfo = browserFetcher.RevisionInfo(BrowserFetcher.DefaultRevision);
168+
if (!revisionInfo.Local)
169+
{
170+
throw new FileNotFoundException("Chromium revision is not downloaded. Run BrowserFetcher.DownloadAsync or download Chromium manually", revisionInfo.ExecutablePath);
171+
}
172+
return revisionInfo.ExecutablePath;
173+
}
174+
144175
private static Task EnsureInitialPageAsync(Browser browser)
145176
{
146177
// Wait for initial page target to be created.

lib/PuppeteerSharp/Puppeteer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public static string[] GetDefaultArgs(LaunchOptions options = null)
4646
/// See <a href="https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/">this article</a>
4747
/// for a description of the differences between Chromium and Chrome.
4848
/// <a href="https://chromium.googlesource.com/chromium/src/+/lkcr/docs/chromium_browser_vs_google_chrome.md">This article</a> describes some differences for Linux users.
49+
///
50+
/// Environment Variables
51+
/// Puppeteer looks for certain <see href="https://en.wikipedia.org/wiki/Environment_variable">environment variables</see>() to aid its operations.
52+
/// - <c>PUPPETEER_CHROMIUM_REVISION</c> - specify a certain version of Chromium you'd like Puppeteer to use. See <see cref="Puppeteer.LaunchAsync(LaunchOptions, ILoggerFactory)"/> on how executable path is inferred.
53+
/// **BEWARE**: Puppeteer is only <see href="https://github.com/GoogleChrome/puppeteer/#q-why-doesnt-puppeteer-vxxx-work-with-chromium-vyyy">guaranteed to work</see> with the bundled Chromium, use at your own risk.
54+
/// - <c>PUPPETEER_EXECUTABLE_PATH</c> - specify an executable path to be used in <see cref="Puppeteer.LaunchAsync(LaunchOptions, ILoggerFactory)"/>.
55+
/// **BEWARE**: Puppeteer is only <see href="https://github.com/GoogleChrome/puppeteer/#q-why-doesnt-puppeteer-vxxx-work-with-chromium-vyyy">guaranteed to work</see> with the bundled Chromium, use at your own risk.
4956
/// </remarks>
5057
public static Task<Browser> LaunchAsync(LaunchOptions options, ILoggerFactory loggerFactory = null)
5158
=> new Launcher(loggerFactory).LaunchAsync(options);

0 commit comments

Comments
 (0)