Adding a port parameter to Chromedriver server.

We are planning to use Chromedriver on ChromeOS vms. Any port on
the VM that is accessed by tests on the VM host must be decided
at VM startup time so that the port can be forwarded. So
chromedriver server should accept a port parameter rather than
always choosing a random port.

TEST=run locally
Change-Id: I7d8610cbc7518373feb03e60a30f7e146a008643
Reviewed-by: Dan Shi <>
Reviewed-by: David Abrahams <>
Commit-Queue: David Abrahams <>
Trybot-Ready: David Abrahams <>
Tested-by: David Abrahams <>
diff --git a/client/common_lib/cros/ b/client/common_lib/cros/
index 23ca0d2..404c444 100644
--- a/client/common_lib/cros/
+++ b/client/common_lib/cros/
@@ -28,7 +28,7 @@
     def __init__(self, extra_chrome_flags=[], subtract_extra_chrome_flags=[],
                  extension_paths=[], is_component=True, username=None,
-                 password=None, *args, **kwargs):
+                 password=None, server_port=None, *args, **kwargs):
         @param extra_chrome_flags: Extra chrome flags to pass to chrome, if any.
@@ -38,7 +38,9 @@
                                 that paths to crx files won't work.
         @param is_component: True if the manifest.json has a key.
         @param username: Log in using this username instead of the default.
-        @param username: Log in using this password instead of the default.
+        @param password: Log in using this password instead of the default.
+        @param server_port: Port number for the chromedriver server. If None,
+                            an available port is chosen at random.
         assert os.geteuid() == 0, 'Need superuser privileges'
@@ -54,7 +56,8 @@
         # Start ChromeDriver server
-        self._server = chromedriver_server(CHROMEDRIVER_EXE_PATH)
+        self._server = chromedriver_server(CHROMEDRIVER_EXE_PATH,
+                                           port=server_port)
         # Open a new tab using Chrome remote debugging. ChromeDriver expects
         # a tab opened for remote to work. Tabs opened using Telemetry will be
@@ -113,18 +116,20 @@
-    def __init__(self, exe_path):
+    def __init__(self, exe_path, port=None):
         """Starts the ChromeDriver server and waits for it to be ready.
             exe_path: path to the ChromeDriver executable
+            port: server port. If None, an available port is chosen at random.
             RuntimeError if ChromeDriver fails to start
         if not os.path.exists(exe_path):
             raise RuntimeError('ChromeDriver exe not found at: ' + exe_path)
-        port = utils.get_unused_port()
+        if not port:
+            port = utils.get_unused_port()
         chromedriver_args = [exe_path, '--port=%d' % port]
         # Chromedriver will look for an X server running on the display