Package tlib :: Package base :: Module TestHelper |
[hide private]
[frames] | no frames]

# Source Code for Module tlib.base.TestHelper

      1  import os
      2  import socket
      3  import subprocess
      4  import pytest
      5  import logging
      6  import tempfile
      7
      8 -def tlib_folder():
      9      """
     10      Returns tlib's absolute path
     11      """
     12      file_folder = os.path.dirname(__file__)
     13      return os.path.abspath(os.path.join(file_folder, os.pardir))
     14
     15
     16 -def tlib_asset_folder():
     17      """
     18      Returns absolute path of tlib's asset folder
     19      """
     20      return os.path.abspath(os.path.join(tlib_folder(), "asset"))
     21
     22
     23 -def tlib_config_folder():
     24      """
     25      Returns absolute path of tlib's config folder
     26      """
     27      return os.path.abspath(os.path.join(tlib_folder(), "config"))
     28
     29
     30 -def tlib_template_folder():
     31      """
     32      Returns absolute path of tlib's template folder\n
     33      Template folder contains jinja templates used for generation of reports\n
     34      like screenshots
     35      """
     36      return os.path.abspath(os.path.join(tlib_folder(), "templates"))
     37
     38
     39 -def webdriver_chrome_executable():
     40      """
     41      Returns path to Chrome WebDriver executable
     42      """
     43      return os.path.join(tlib_asset_folder(), "selenium", "chromedriver.exe")
     44
     45
     46 -def webdriver_ie_executable():
     47      """
     48      Alias for webdriver_ie32_executable
     49      """
     50      return webdriver_ie32_executable();
     51
     52
     53 -def webdriver_ie32_executable():
     54      """
     55      Returns path to IE WebDriver executable
     56      """
     57      return os.path.join(tlib_asset_folder(), "selenium", "IE", "Win32_2.39.0", "IEDriverServer.exe")
     58
     59
     60 -def webdriver_ie64_executable():
     61      """
     62      Returns path to IE WebDriver executable
     63      """
     64      return os.path.join(tlib_asset_folder(), "selenium", "IE", "x64_2.39.0", "IEDriverServer.exe")
     65
     66
     67 -def android_server_apk():
     68      """
     69      Returns path to Webdriver for android
     70      """
     71      return os.path.join(tlib_asset_folder(), "selenium", "android-server-2.21.0.apk")
     72
     73
     74 -def is_valid_ip(ip):
     75      """
     76      Returns true if IP parameter is a valid IP address.\n
     77      Currently IPs in this format are valid:\n
     78      X.X.X.X\n
     79      X.X.X.X:PORT
     80
     81      @type ip: str
     82      @return: bool
     83      """
     84
     85      if ip is None:
     86          return False
     87
     88      try:
     89          (ip, port) = ip.split(":", 1)
     90      except ValueError:
     91          #There is no ':' in the string
     92          port = None
     93
     94      try:
     95          socket.inet_aton(ip)
     96      except socket.error:
     97          return False
     98
     99      if port is not None:
    100          try:
    101              return (int(port) >= 1) and (int(port) <= 65535)
    102          except ValueError:
    103              #Not a valid integer
    104              return False
    105
    106      return True
    107
    108 -def run_command(logger, command, fail_on_error=True):
    109      """
    110      Run a command and skip test if exit code is not 0
    111
    112      Example:
    113      Run 'adb devices' and don't skip test if commands returns non-zero status
    114      run_command(logger, ["adb", "devices"], fail_on_error=False)
    115
    116      @param logger: logger for debugging purposes
    117      @type logger: logging.logger
    118      @param command: Command to run
    119      @type command: list
    120      @param fail_on_error: When True, skip test if command returned a non-zero exit code
    121      @type fail_on_error: bool
    122      @rtype: list
    123      @return: Returns a list with stdout and stderr output
    124      """
    125
    126      fd_out = tempfile.NamedTemporaryFile(delete=True)
    127      #With delete = True option, the files will be automatically removed after close
    128      fd_err = tempfile.NamedTemporaryFile(delete=True)
    129      try:
    130          process = subprocess.Popen(command, stdout=fd_out, stderr=fd_err)
    131      except WindowsError as e:
    132          logger.error(r"Problem running command, skipping test.\n%s" % e)
    133          # noinspection PyUnresolvedReferences
    134          if fail_on_error:
    135              pytest.fail(r"Problem running command, skipping test.\n%s" % e)
    136
    137      # noinspection PyUnboundLocalVariable
    138      process.communicate()
    139      fd_out.seek(0)
    140      fd_err.seek(0)
    141      out = (fd_out.read(), fd_err.read())
    142      fd_out.close()
    143      fd_err.close()
    144      errcode = process.returncode
    145
    146      if (errcode != 0) and fail_on_error:
    147          logger.error(r"Program  exited with code {errcode}, skipping test\n{out}".format(errcode=errcode, out=out))
    148          # noinspection PyUnresolvedReferences
    149          pytest.fail(r"Program exited with code {errcode}, skipping test\n{out}".format(errcode=errcode, out=out))
    150
    151      return out
    152