Skip to content

Commit

Permalink
Merge pull request #51 from google/tests
Browse files Browse the repository at this point in the history
Re-Enable Tests
  • Loading branch information
grouma authored Mar 4, 2020
2 parents 80d3fb6 + a79debe commit 41efa16
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 20 deletions.
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ language: dart
dart:
- dev

#before_install:
# - export DISPLAY=:99.0
# - sh -e /etc/init.d/xvfb start
addons:
chrome: stable

services:
- xvfb

script: ./tool/travis.sh

# Only building master means that we don't run two builds for each pull request.
branches:
only:
- master
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ dev_dependencies:
shelf_static: ^0.2.0
shelf_web_socket: ^0.2.0
test: ^1.0.0
webdriver: ^2.0.0
14 changes: 2 additions & 12 deletions test/console_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ void main() {
expect(events, hasLength(expectedCount));
for (int i = 0; i < expectedCount; i++) {
if (i == 0) {
// clear adds an empty message
expect(events[i].text, '');
// Clearing adds this message.
expect(events[i].text, 'console.clear');
} else {
expect(events[i].text, 'message $i');
}
Expand All @@ -34,14 +34,11 @@ void main() {
setUp(() async {
// ignore: deprecated_member_use
console = (await wipConnection).console;
await console.clearMessages();
events.clear();
subs.add(console.onMessage.listen(events.add));
subs.add(console.onCleared.listen((_) => events.clear()));
});

tearDown(() async {
await console.clearMessages();
await console.disable();
console = null;
await closeConnection();
Expand All @@ -61,13 +58,6 @@ void main() {
await checkMessages(4);
});

test('does not receive messages if cleared', () async {
await navigateToPage('console_test.html');
await console.clearMessages();
await console.enable();
await checkMessages(0);
});

test('does not receive messages if not enabled', () async {
await navigateToPage('console_test.html');
await checkMessages(0);
Expand Down
70 changes: 68 additions & 2 deletions test/test_setup.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
library wip.test.setup;

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';

import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_static/shelf_static.dart';
import 'package:webdriver/io.dart';
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';

Future<WipConnection> _wipConnection;
Expand All @@ -15,7 +17,8 @@ Future<WipConnection> _wipConnection;
Future<WipConnection> get wipConnection {
if (_wipConnection == null) {
_wipConnection = () async {
var chrome = new ChromeConnection('localhost');
var debugPort = await _startWebDriver(await _startChromeDriver());
var chrome = new ChromeConnection('localhost', debugPort);
var tab = await chrome
.getTab((tab) => !tab.isBackgroundPage && !tab.isChromeExtension);
var connection = await tab.connect();
Expand All @@ -26,6 +29,66 @@ Future<WipConnection> get wipConnection {
return _wipConnection;
}

Process _chromeDriver;

/// Starts ChromeDriver and returns the listening port.
Future<int> _startChromeDriver() async {
var chromeDriverPort = await findUnusedPort();
try {
var _exeExt = Platform.isWindows ? '.exe' : '';
_chromeDriver = await Process.start('chromedriver$_exeExt',
['--port=$chromeDriverPort', '--url-base=wd/hub']);
// On windows this takes a while to boot up, wait for the first line
// of stdout as a signal that it is ready.
await _chromeDriver.stdout
.transform(utf8.decoder)
.transform(const LineSplitter())
.first;
} catch (e) {
throw StateError(
'Could not start ChromeDriver. Is it installed?\nError: $e');
}
return chromeDriverPort;
}

WebDriver _webDriver;

/// Starts WebDriver and returns the listening debug port.
Future<int> _startWebDriver(int chromeDriverPort) async {
var debugPort = await findUnusedPort();
var capabilities = Capabilities.chrome
..addAll({
Capabilities.chromeOptions: {
'args': ['remote-debugging-port=$debugPort', '--headless']
}
});

await createDriver(
spec: WebDriverSpec.JsonWire,
desired: capabilities,
uri: Uri.parse('http://127.0.0.1:$chromeDriverPort/wd/hub/'));

return debugPort;
}

/// Returns a port that is probably, but not definitely, not in use.
///
/// This has a built-in race condition: another process may bind this port at
/// any time after this call has returned.
Future<int> findUnusedPort() async {
int port;
ServerSocket socket;
try {
socket =
await ServerSocket.bind(InternetAddress.loopbackIPv6, 0, v6Only: true);
} on SocketException {
socket = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
}
port = socket.port;
await socket.close();
return port;
}

var _testServerUri;

/// Ensures that an HTTP server serving files from 'test/data' has been
Expand Down Expand Up @@ -55,7 +118,10 @@ Future _startHttpServer(SendPort sendPort) async {

Future closeConnection() async {
if (_wipConnection != null) {
await (await navigateToPage('chrome://about')).close();
await _webDriver?.quit(closeSession: true);
_webDriver = null;
_chromeDriver?.kill();
_chromeDriver = null;
_wipConnection = null;
}
}
15 changes: 12 additions & 3 deletions tool/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ set -e
pub global activate tuneup
pub global run tuneup check

# Temporarily disabled due to issues/24
# /usr/bin/chromium-browser --no-sandbox --remote-debugging-port=9222 &
# pub run test -j 1
# Install CHROMEDRIVER
export CHROMEDRIVER_BINARY=/usr/bin/google-chrome
export CHROMEDRIVER_OS=linux64
export CHROME_LATEST_VERSION=$("$CHROMEDRIVER_BINARY" --version | cut -d' ' -f3 | cut -d'.' -f1)
export CHROME_DRIVER_VERSION=$(wget -qO- https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROME_LATEST_VERSION)
wget https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_$CHROMEDRIVER_OS.zip
unzip "chromedriver_${CHROMEDRIVER_OS}.zip"
export CHROMEDRIVER_ARGS=--no-sandbox
export PATH=$PATH:$PWD

# Run tests
pub run test -j 1

0 comments on commit 41efa16

Please sign in to comment.