diff --git a/lib/web_ui/dev/chrome_installer.dart b/lib/web_ui/dev/chrome_installer.dart index e761446545e29..95a3892581b38 100644 --- a/lib/web_ui/dev/chrome_installer.dart +++ b/lib/web_ui/dev/chrome_installer.dart @@ -227,3 +227,64 @@ Future fetchLatestChromeVersion() async { client.close(); } } + +/// Get the Chrome Driver version for the system Chrome. +// TODO(nurhan): https://github.com/flutter/flutter/issues/53179 +Future queryChromeDriverVersion() async { + final int chromeVersion = await _querySystemChromeMajorVersion(); + final io.File lockFile = io.File( + path.join(environment.webUiRootDir.path, 'dev', 'driver_version.yaml')); + YamlMap _configuration = loadYaml(lockFile.readAsStringSync()) as YamlMap; + final String chromeDriverVersion = + _configuration['chrome'][chromeVersion] as String; + return chromeDriverVersion; +} + +Future _querySystemChromeMajorVersion() async { + String chromeExecutable = ''; + if (io.Platform.isLinux) { + chromeExecutable = 'google-chrome'; + } else if (io.Platform.isMacOS) { + chromeExecutable = await _findChromeExecutableOnMac(); + } else { + throw UnimplementedError('Web installers only work on Linux and Mac.'); + } + + final io.ProcessResult versionResult = + await io.Process.run('$chromeExecutable', ['--version']); + + if (versionResult.exitCode != 0) { + throw Exception('Failed to locate system Chrome.'); + } + // The output looks like: Google Chrome 79.0.3945.36. + final String output = versionResult.stdout as String; + + print('INFO: chrome version in use $output'); + + // Version number such as 79.0.3945.36. + try { + final String versionAsString = output.trim().split(' ').last; + final String majorVersion = versionAsString.split('.')[0]; + return int.parse(majorVersion); + } catch (e) { + throw Exception( + 'Was expecting a version of the form Google Chrome 79.0.3945.36., ' + 'received $output'); + } +} + +/// Find Google Chrome App on Mac. +Future _findChromeExecutableOnMac() async { + io.Directory chromeDirectory = io.Directory('/Applications') + .listSync() + .whereType() + .firstWhere( + (d) => path.basename(d.path).endsWith('Chrome.app'), + orElse: () => throw Exception('Failed to locate system Chrome'), + ); + + final io.File chromeExecutableDir = io.File( + path.join(chromeDirectory.path, 'Contents', 'MacOS', 'Google Chrome')); + + return chromeExecutableDir.path; +} diff --git a/lib/web_ui/dev/driver_version.yaml b/lib/web_ui/dev/driver_version.yaml new file mode 100644 index 0000000000000..5515e1fbca7b0 --- /dev/null +++ b/lib/web_ui/dev/driver_version.yaml @@ -0,0 +1,12 @@ + +## Map for driver versions to use for each browser version. +## See: https://chromedriver.chromium.org/downloads +chrome: + 81: '81.0.4044.69' + 80: '80.0.3987.106' + 79: '79.0.3945.36' + 78: '78.0.3904.105' + 77: '77.0.3865.40' + 76: '76.0.3809.126' + 75: '75.0.3770.140' + 74: '74.0.3729.6' diff --git a/lib/web_ui/dev/integration_tests_manager.dart b/lib/web_ui/dev/integration_tests_manager.dart index bd97ec878a7ef..6e5457da4fe0b 100644 --- a/lib/web_ui/dev/integration_tests_manager.dart +++ b/lib/web_ui/dev/integration_tests_manager.dart @@ -5,7 +5,9 @@ import 'dart:io' as io; import 'package:path/path.dart' as pathlib; import 'package:web_driver_installer/chrome_driver_installer.dart'; +import 'package:yaml/yaml.dart'; +import 'chrome_installer.dart'; import 'common.dart'; import 'environment.dart'; import 'utils.dart'; @@ -46,24 +48,6 @@ class IntegrationTestsManager { } } - void _cloneWebInstallers() async { - final int exitCode = await runProcess( - 'git', - [ - 'clone', - 'https://github.com/flutter/web_installers.git', - ], - workingDirectory: _browserDriverDir.path, - ); - - if (exitCode != 0) { - io.stderr.writeln('ERROR: ' - 'Failed to clone web installers. Exited with exit code $exitCode'); - throw DriverException('ERROR: ' - 'Failed to clone web installers. Exited with exit code $exitCode'); - } - } - Future _runPubGet(String workingDirectory) async { final String executable = isCirrus ? environment.pubExecutable : 'flutter'; final List arguments = isCirrus @@ -90,34 +74,14 @@ class IntegrationTestsManager { } void _runDriver() async { - final int exitCode = await runProcess( - environment.dartExecutable, - [ - 'lib/web_driver_installer.dart', - '${_browser}driver', - '--install-only', - ], - workingDirectory: pathlib.join( - _browserDriverDir.path, 'web_installers', 'packages', 'web_drivers'), - ); - - if (exitCode != 0) { - io.stderr.writeln( - 'ERROR: Failed to run driver. Exited with exit code $exitCode'); - throw DriverException( - 'ERROR: Failed to run driver. Exited with exit code $exitCode'); - } startProcess( './chromedriver/chromedriver', ['--port=4444'], - workingDirectory: pathlib.join( - _browserDriverDir.path, 'web_installers', 'packages', 'web_drivers'), ); print('INFO: Driver started'); } void prepareDriver() async { - final io.Directory priorCurrentDirectory = io.Directory.current; if (_browserDriverDir.existsSync()) { _browserDriverDir.deleteSync(recursive: true); } @@ -125,22 +89,12 @@ class IntegrationTestsManager { _browserDriverDir.createSync(recursive: true); temporaryDirectories.add(_drivers); - // TODO(nurhan): We currently need git clone for getting the driver lock - // file. Remove this after making changes in web_installers. - await _cloneWebInstallers(); - // Change the directory to the driver_lock.yaml file's directory. - io.Directory.current = pathlib.join( - _browserDriverDir.path, 'web_installers', 'packages', 'web_drivers'); - // Chrome is the only browser supporting integration tests for now. - ChromeDriverInstaller chromeDriverInstaller = ChromeDriverInstaller(); - bool installation = await chromeDriverInstaller.install(); - - if (installation) { - io.Directory.current = priorCurrentDirectory; - await _runDriver(); - } else { - throw DriverException('ERROR: Installing driver failed'); - } + // TODO(nurhan): https://github.com/flutter/flutter/issues/53179 + final String chromeDriverVersion = await queryChromeDriverVersion(); + ChromeDriverInstaller chromeDriverInstaller = + ChromeDriverInstaller.withVersion(chromeDriverVersion); + await chromeDriverInstaller.install(alwaysInstall: true); + await _runDriver(); } /// Runs all the web tests under e2e_tests/web. diff --git a/lib/web_ui/pubspec.yaml b/lib/web_ui/pubspec.yaml index 997e119224c72..75743250398be 100644 --- a/lib/web_ui/pubspec.yaml +++ b/lib/web_ui/pubspec.yaml @@ -25,4 +25,4 @@ dev_dependencies: git: url: git://github.com/flutter/web_installers.git path: packages/web_drivers/ - ref: dae38d8839cc39f997fb4229f1382680b8758b4f + ref: 90c69a79b2764c93875dc8ed4f4932c27a6f3a86