Merge pull request #51 from google/tests

Re-Enable Tests
diff --git a/.travis.yml b/.travis.yml
index 35e0ab9..0dfdf99 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -3,8 +3,15 @@
 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
diff --git a/pubspec.yaml b/pubspec.yaml
index 1de55da..5676af6 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -19,3 +19,4 @@
   shelf_static: ^0.2.0
   shelf_web_socket: ^0.2.0
   test: ^1.0.0
+  webdriver: ^2.0.0
diff --git a/test/console_test.dart b/test/console_test.dart
index 9d6727d..f0375a9 100644
--- a/test/console_test.dart
+++ b/test/console_test.dart
@@ -23,8 +23,8 @@
       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');
         }
@@ -34,14 +34,11 @@
     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();
@@ -61,13 +58,6 @@
       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);
diff --git a/test/test_setup.dart b/test/test_setup.dart
index 23b23ed..940a5cb 100644
--- a/test/test_setup.dart
+++ b/test/test_setup.dart
@@ -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;
@@ -15,7 +17,8 @@
 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();
@@ -26,6 +29,66 @@
   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
@@ -55,7 +118,10 @@
 
 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;
   }
 }
diff --git a/tool/travis.sh b/tool/travis.sh
index 2ccf3e5..04cf687 100755
--- a/tool/travis.sh
+++ b/tool/travis.sh
@@ -11,6 +11,15 @@
 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