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

Migrate browser_launcher to null-safety. #27

Merged
merged 1 commit into from
May 7, 2021
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 @@
## 1.0.0

- Migrate to null-safety.

## 0.1.10

- Support `webkit_inspection_protocol` version `^1.0.0`.
Expand Down
32 changes: 19 additions & 13 deletions lib/src/chrome.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const _windowsExecutable = r'Google\Chrome\Application\chrome.exe';
String get _executable {
for (var chromeEnv in _chromeEnvironments) {
if (Platform.environment.containsKey(chromeEnv)) {
return Platform.environment[chromeEnv];
return Platform.environment[chromeEnv]!;
}
}
if (Platform.isLinux) return _linuxExecutable;
Expand All @@ -34,7 +34,7 @@ String get _executable {
if (prefix == null) return false;
final path = p.join(prefix, _windowsExecutable);
return File(path).existsSync();
}, orElse: () => '.'),
}, orElse: () => '.')!,
_windowsExecutable,
);
}
Expand All @@ -43,15 +43,19 @@ String get _executable {

/// Manager for an instance of Chrome.
class Chrome {
Chrome._(this.debugPort, this.chromeConnection,
{Process process, Directory dataDir, this.deleteDataDir = false})
: _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 Process? _process;
final Directory? _dataDir;
final bool deleteDataDir;

/// Connects to an instance of Chrome with an open debug port.
Expand All @@ -61,17 +65,19 @@ 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, String userDataDir}) async {
static Future<Chrome> startWithDebugPort(
List<String> urls, {
int debugPort = 0,
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;
final port = debugPort == 0 ? await findUnusedPort() : debugPort;
final args = [
// Using a tmp directory ensures that a new instance of chrome launches
// allowing for the remote debug port to be enabled.
Expand Down Expand Up @@ -117,7 +123,7 @@ class Chrome {
ChromeConnection('localhost', port),
process: process,
dataDir: dataDir,
deleteDataDir: userDataDir = null,
deleteDataDir: userDataDir == null,
));
}

Expand Down
10 changes: 5 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name: browser_launcher
description: Provides a standardized way to launch web browsers for testing and tools.

version: 0.1.10
version: 1.0.0

homepage: https://github.com/dart-lang/browser_launcher

environment:
sdk: '>=2.2.0 <3.0.0'
sdk: '>=2.12.0 <3.0.0'

dependencies:
path: ^1.6.2
path: ^1.8.0
webkit_inspection_protocol: ^1.0.0

dev_dependencies:
pedantic: ^1.5.0
test: ^1.0.0
pedantic: ^1.11.0
test: ^1.17.3
10 changes: 5 additions & 5 deletions test/chrome_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import 'package:test/test.dart';
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';

void main() {
Chrome chrome;
Chrome? chrome;

Future<void> launchChromeWithDebugPort({int port}) async {
Future<void> launchChromeWithDebugPort({int port = 0}) async {
chrome = await Chrome.startWithDebugPort([_googleUrl], debugPort: port);
}

Expand All @@ -37,7 +37,7 @@ void main() {

test('debugger is working', () async {
await launchChromeWithDebugPort();
var tabs = await chrome.chromeConnection.getTabs();
var tabs = await chrome!.chromeConnection.getTabs();
expect(
tabs,
contains(const TypeMatcher<ChromeTab>()
Expand All @@ -46,13 +46,13 @@ void main() {

test('uses open debug port if provided port is 0', () async {
await launchChromeWithDebugPort(port: 0);
expect(chrome.debugPort, isNot(equals(0)));
expect(chrome!.debugPort, isNot(equals(0)));
});

test('can provide a specific debug port', () async {
var port = await findUnusedPort();
await launchChromeWithDebugPort(port: port);
expect(chrome.debugPort, port);
expect(chrome!.debugPort, port);
});
}

Expand Down