Package pytest_auto :: Module selenium_plugin |
[hide private]
[frames] | no frames]

# Source Code for Module pytest_auto.selenium_plugin

      1  import pytest
      2  import os
      3  from selenium import webdriver as selenium_webdriver
      4  from tlib.base import AndroidHelper
      5  from tlib.base.TestHelper import webdriver_chrome_executable, webdriver_ie_executable
      6  from _pytest.python import FixtureRequest
      7  import logging
      8  from appium.webdriver.webdriver import WebDriver as appium_webdriver
      9  from selenium.common.exceptions import WebDriverException
     10
     11 -def pytest_addoption(parser):
     12      """
     13      Options supported by pytest's selenium plug-in
     14      """
     15      group = parser.getgroup("selenium", "Testing with the selenium framework")
     16
     17      group.addoption(
     18          "--mobile_base_url",
     19          action="store",
     20          dest="mobile_base_url",
     21          help="mobile base url of the mobile web server used for testing")
     22
     23      group.addoption(
     24          "--browser",
     25          action="store",
     26          dest="browser",
     27          choices=["chrome", "firefox", "firefox_no_js", "ie", "android", "chrome_android"],
     28          help="Browser used for running test cases")
     29
     30      group.addoption(
     31          "--chromedriver_version",
     32          action="store",
     33          dest="chromedriver_version",
     34          default="latest",
     35          help="Version of chromedriver to use")
     36
     37      group.addoption(
     38          "--serial_id",
     39          action="store",
     40          dest="serial_id",
     41          help="Serial of the android device or simulator where Android tests will be run")
     42
     43
     44  #noinspection PyShadowingNames,PyUnresolvedReferences
     45  @pytest.fixture(scope='class')
     46 -def driver(request, tlib_logger, adb_logger):
     47      """
     48      Fixture to creates a Webdriver object to interact with native apps specified by command line parameters
     49      @param request: Object that gives access to the requesting test context
     50      @type request: FixtureRequest
     51      @param tlib_logger: logger for tlib related actions
     52      @type tlib_logger: logging.Logger
     53      """
     54      serial_id = request.config.getoption("serial_id")
     55
     56      apk_path = request.config.getoption("apk_path")
     57      if apk_path is None:
     58          tlib_logger.error("parameter --apk_path was not specified")
     59          raise RuntimeError("parameter --apk_path was not specified")
     60
     61      if not os.path.exists(apk_path):
     62          tlib_logger.error("Selendroid coulnd't be found under '%s'" % apk_path)
     63          raise RuntimeError("Selendroid coulnd't be found under '%s'" % apk_path)
     64
     65      tlib_logger.debug("Creating selendroid driver")
     66
     67      #Start selendroid server
     68      process = AndroidHelper.start_selendroid_server(tlib_logger, adb_logger, apk_path)
     69      if process:
     70          request.addfinalizer(process.kill)
     71
     72      try:
     73          return selendroid_driver(tlib_logger, serial_id)
     74      except WebDriverException, e:
     75          raise RuntimeError("An error occurred while trying to look for device/emulator")
     76
     77
     78 -def selendroid_driver(tlib_logger, serial_id):
     79      """
     80      Function to create Webdriver object to interact with native apps
     81      @param tlib_logger: logger for tlib related actions
     82      @type tlib_logger: logging.Logger
     83      @param serial_id: Serial Id of android device. If not given default android device will be used
     84      @type serial_id: str
     85      """
     86      app_id = AndroidHelper.get_android_app_id()
     87      capabilities = {'aut': app_id}
     88      if serial_id is None:
     89          tlib_logger.warn("Parameter --serial_id was not given. Running test on default device")
     90      else:
     91          capabilities['serial'] = serial_id
     92      dr = selenium_webdriver.Remote(desired_capabilities=capabilities)
     93
     94      return dr
     95
     96
     97  #noinspection PyShadowingNames,PyUnresolvedReferences
     98  @pytest.fixture(scope='class')
     99 -def browser(request, tlib_logger, adb_logger, chromedriver_version):
    100      """
    101      Fixture to create a browser as specified by the command line parameter --browser\n
    102      """
    103      browser = request.config.getoption("browser")
    104      return get_browser(request, tlib_logger, adb_logger, browser, chromedriver_version)
    105
    106
    107  @pytest.fixture(scope='class')
    108 -def chromedriver_version(request, tlib_logger):
    109      """
    110      Fixture to create a browser as specified by the command line parameter --browser\n
    111      """
    112      version = request.config.getoption("chromedriver_version")
    113      tlib_logger.info("Using chromedriver version = '%s'" % version)
    114      return version
    115
    116
    117 -def get_browser(request, tlib_logger, adb_logger, browser_str, chromedriver="latest"):
    118      """
    119      Creates a Webdriver object to interact with browser specified in browser_str parameter
    120      @param request: Object that gives access to the requesting test context
    121      @type request: FixtureRequest
    122      @param tlib_logger: logger for tlib related actions
    123      @type tlib_logger: logging.Logger
    124      @param adb_logger: logger for adb related actions
    125      @type adb_logger: logging.Logger
    126      @param browser_str: browser to launch. It cna be firefox, chrome, ie or android
    127      @type browser_str: str
    128      @param chromedriver: Version of ChromeDriver to use. Pass 'latest' to use the latest version
    129      @type chromedriver: str
    130      """
    131      b = None
    132      process = None
    133
    134      #Don't create a browser object is --browser was not specified
    135      if browser_str is None:
    136          tlib_logger.error("Parameter browser was not specified")
    137          raise RuntimeError("Parameter --browser was not specified")
    138      else:
    139          if browser_str == "ie":
    140              tlib_logger.debug("Creating webdriver for IE")
    141              b = selenium_webdriver.Ie(webdriver_ie_executable())
    142              b.maximize_window()
    143          elif browser_str == "chrome":
    144              tlib_logger.debug("Creating webdriver for Chrome")
    145              b = selenium_webdriver.Chrome(webdriver_chrome_executable(chromedriver))
    146              b.maximize_window()
    147          elif browser_str == "firefox":
    148              tlib_logger.debug("Creating webdriver for Firefox")
    149              b = selenium_webdriver.Firefox()
    150              b.maximize_window()
    151          elif browser_str == "firefox_no_js":
    152              tlib_logger.debug("Creating webdriver for Firefox with JavaScript disabled")
    153              profile = selenium_webdriver.FirefoxProfile()
    154              profile.set_preference("javascript.enabled", False)
    155              b = selenium_webdriver.Firefox(profile)
    156              b.maximize_window()
    157          elif browser_str == "android":
    158              tlib_logger.debug("Creating webdriver for Android")
    159
    160              serial_id = request.config.getoption("serial_id")
    161              if serial_id is None:
    162                  tlib_logger.warn("Parameter --serial_id was not given. Running test on default device")
    163
    164              #Start android server
    165              AndroidHelper.setup_webdriver(tlib_logger, adb_logger, serial_id)
    166              b = selenium_webdriver.Remote(command_executor='http://localhost:8080/wd/hub',
    167                                            desired_capabilities=selenium_webdriver.DesiredCapabilities.ANDROID)
    168          elif browser_str == "chrome_android":
    169              tlib_logger.debug("Running Chrome on android")
    170
    171              desired_caps = {}
    172
    173              serial_id = request.config.getoption("serial_id")
    174              if serial_id is not None:
    175                  desired_caps['udid'] = serial_id
    176              else:
    177                  desired_caps['defaultDevice'] = True
    178
    179              desired_caps['deviceName']=''
    180              desired_caps['platformName'] = 'Android'
    181              desired_caps['browserName'] = 'Chrome'
    182              desired_caps['noReset'] = True
    183
    184
    185              #Start selendroid server
    186              process = AndroidHelper.start_appium_server(tlib_logger, adb_logger)
    187              b = appium_webdriver(desired_capabilities=desired_caps)
    188          else:
    189              #noinspection PyUnresolvedReferences
    190              raise RuntimeError("{browser}s is not a valid browser".format(browser=browser_str))
    191
    192          def fin():
    193              # noinspection PyStatementEffect
    194              b.quit()
    195              if browser_str == "android":
    196                  AndroidHelper.teardown_webdriver(tlib_logger, adb_logger, serial_id)
    197
    198              if process:
    199                  process.kill
    200
    201          request.addfinalizer(fin)
    202
    203      # set browser name
    204      b.browser_name = browser_str
    205      return b
    206
    207
    208  #noinspection PyUnresolvedReferences
    209  @pytest.fixture(scope="class")
    210 -def mobile_base_url(request, tlib_logger):
    211      """
    212      Returns value of --mobile_base_url command line parameter
    213      """
    214      if request.config.getoption("mobile_base_url") is None:
    215          #noinspection PyUnresolvedReferences
    216          tlib_logger.error("parameter --mobile_base_url was not specified")
    217          raise RuntimeError("parameter --mobile_base_url was not specified")
    218          return None
    219      else:
    220          return request.config.getoption("mobile_base_url")
    221