Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Allow passing in a user-data-dir #14

Merged
merged 3 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.5

- Add a parameter to use a specified user-data-dir instead of a system temp.

## 0.1.4

- Start Chrome maximized.
Expand Down
31 changes: 17 additions & 14 deletions lib/src/chrome.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ String get _executable {

/// Manager for an instance of Chrome.
class Chrome {
Chrome._(
this.debugPort,
this.chromeConnection, {
Process process,
Directory dataDir,
}) : _process = process,
Chrome._(this.debugPort, this.chromeConnection,
{Process process, Directory dataDir, this.deleteDataDir = false})
: _process = process,
_dataDir = dataDir;

final int debugPort;
final ChromeConnection chromeConnection;
final Process _process;
final Directory _dataDir;
final bool deleteDataDir;

/// Connects to an instance of Chrome with an open debug port.
static Future<Chrome> fromExisting(int port) async =>
Expand All @@ -61,12 +59,14 @@ class Chrome {
/// Starts Chrome with the given arguments and a specific port.
///
/// Each url in [urls] will be loaded in a separate tab.
static Future<Chrome> startWithDebugPort(
List<String> urls, {
int debugPort,
bool headless = false,
}) async {
final dataDir = Directory.systemTemp.createTempSync();
static Future<Chrome> startWithDebugPort(List<String> urls,
{int debugPort, bool headless = false, String userDataDir}) async {
Directory dataDir;
if (userDataDir == null) {
dataDir = Directory.systemTemp.createTempSync();
} else {
dataDir = Directory(userDataDir);
}
final port = debugPort == null || debugPort == 0
? await findUnusedPort()
: debugPort;
Expand Down Expand Up @@ -108,6 +108,7 @@ class Chrome {
ChromeConnection('localhost', port),
process: process,
dataDir: dataDir,
deleteDataDir: userDataDir = null,
));
}

Expand Down Expand Up @@ -150,8 +151,10 @@ class Chrome {
// Chrome starts another process as soon as it dies that modifies the
// profile information. Give it some time before attempting to delete
// the directory.
await Future.delayed(Duration(milliseconds: 500));
await _dataDir?.delete(recursive: true);
if (deleteDataDir) {
await Future.delayed(Duration(milliseconds: 500));
await _dataDir?.delete(recursive: true);
}
} catch (_) {
// Silently fail if we can't clean up the profile information.
// It is a system tmp directory so it should get cleaned up eventually.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: browser_launcher
description: Provides a standardized way to launch web browsers for testing and tools.

version: 0.1.4
version: 0.1.5

author: Dart Team <[email protected]>
homepage: https://github.com/dart-lang/browser_launcher
Expand Down