Home | History | Annotate | Download | only in pyautolib
      1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """Factory that creates ChromeDriver instances for pyauto."""
      6 
      7 import os
      8 import random
      9 import tempfile
     10 
     11 import pyauto_paths
     12 from selenium import webdriver
     13 from selenium.webdriver.chrome import service
     14 
     15 
     16 class ChromeDriverFactory(object):
     17   """"Factory that creates ChromeDriver instances for pyauto.
     18 
     19   Starts a single ChromeDriver server when necessary. Users should call 'Stop'
     20   when no longer using the factory.
     21   """
     22 
     23   def __init__(self, port=0):
     24     """Initialize ChromeDriverFactory.
     25 
     26     Args:
     27       port: The port for WebDriver to use; by default the service will select a
     28             free port.
     29     """
     30     self._chromedriver_port = port
     31     self._chromedriver_server = None
     32 
     33   def NewChromeDriver(self, pyauto):
     34     """Creates a new remote WebDriver instance.
     35 
     36     This instance will connect to a new automation provider of an already
     37     running Chrome.
     38 
     39     Args:
     40       pyauto: pyauto.PyUITest instance
     41 
     42     Returns:
     43       selenium.webdriver.remote.webdriver.WebDriver instance.
     44     """
     45     if pyauto.IsChromeOS():
     46       os.putenv('DISPLAY', ':0.0')
     47       os.putenv('XAUTHORITY', '/home/chronos/.Xauthority')
     48     self._StartServerIfNecessary()
     49     channel_id = 'testing' + hex(random.getrandbits(20 * 4))[2:-1]
     50     if not pyauto.IsWin():
     51       channel_id = os.path.join(tempfile.gettempdir(), channel_id)
     52     pyauto.CreateNewAutomationProvider(channel_id)
     53     return webdriver.Remote(self._chromedriver_server.service_url,
     54                             {'chrome.channel': channel_id,
     55                              'chrome.noWebsiteTestingDefaults': True})
     56 
     57   def _StartServerIfNecessary(self):
     58     """Starts the ChromeDriver server, if not already started."""
     59     if self._chromedriver_server is None:
     60       exe = pyauto_paths.GetChromeDriverExe()
     61       assert exe, 'Cannot find chromedriver exe. Did you build it?'
     62       self._chromedriver_server = service.Service(exe, self._chromedriver_port)
     63       self._chromedriver_server.start()
     64 
     65   def Stop(self):
     66     """Stops the ChromeDriver server, if running."""
     67     if self._chromedriver_server is not None:
     68       self._chromedriver_server.stop()
     69       self._chromedriver_server = None
     70 
     71   def GetPort(self):
     72     """Gets the port ChromeDriver is set to use.
     73 
     74     Returns:
     75       The port all ChromeDriver instances returned from NewChromeDriver() will
     76       be listening on. A return value of 0 indicates the ChromeDriver service
     77       will select a free port.
     78     """
     79     return self._chromedriver_port
     80 
     81   def __del__(self):
     82     self.Stop()
     83