startWithDebugPort static method
Starts Chrome with the given arguments and a specific port.
Each url in urls
will be loaded in a separate tab.
If userDataDir
is null
, a new temp directory will be
passed to chrome as a user data directory. Chrome will
start without sign in and with extensions disabled.
If userDataDir
is not null
, it will be passed to chrome
as a user data directory. Chrome will start signed into
the default profile with extensions enabled if signIn
is also true.
Implementation
static Future<Chrome> startWithDebugPort(
List<String> urls, {
int debugPort = 0,
bool headless = false,
String? userDataDir,
bool signIn = false,
}) async {
Directory dataDir;
if (userDataDir == null) {
signIn = false;
dataDir = Directory.systemTemp.createTempSync();
} else {
dataDir = Directory(userDataDir);
}
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.
'--user-data-dir=${dataDir.path}',
'--remote-debugging-port=$port',
// When the DevTools has focus we don't want to slow down the application.
'--disable-background-timer-throttling',
// Since we are using a temp profile, disable features that slow the
// Chrome launch.
if (!signIn) '--disable-extensions',
'--disable-popup-blocking',
if (!signIn) '--bwsi',
'--no-first-run',
'--no-default-browser-check',
'--disable-default-apps',
'--disable-translate',
'--start-maximized',
];
if (headless) {
args.add('--headless');
}
final process = await _startProcess(urls, args: args);
// Wait until the DevTools are listening before trying to connect.
var _errorLines = <String>[];
try {
await process.stderr
.transform(utf8.decoder)
.transform(const LineSplitter())
.firstWhere((line) {
_errorLines.add(line);
return line.startsWith('DevTools listening');
}).timeout(Duration(seconds: 60));
} catch (_) {
throw Exception('Unable to connect to Chrome DevTools.\n\n'
'Chrome STDERR:\n' +
_errorLines.join('\n'));
}
return _connect(Chrome._(
port,
ChromeConnection('localhost', port),
process: process,
dataDir: dataDir,
deleteDataDir: userDataDir == null,
));
}