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

# Source Code for Module tlib.base.SeleniumTester

      1  # noinspection PyPackageRequirements
      2  import pytest
      3  import _pytest
      4  # noinspection PyPackageRequirements
      5  from selenium import webdriver
      6  # noinspection PyPackageRequirements
      7  import time
      8  from WebDriverTester import WebDriverTester
      9  from TestHelper import Singleton
     10  from tlib.base import TLibHelper
     11  from selenium.common.exceptions import TimeoutException
     12  from selenium.webdriver.support.ui import WebDriverWait
     13
     14
     15  # noinspection PyMethodParameters
     16 -class SeleniumTester(WebDriverTester):
     17      __metaclass__ = Singleton
     18      _homepage = None
     19
     20 -    def _get_browser(self):
     21          """
     22          Returns Instance of the WebDriver browser
     23          @rtype: webdriver.Remote
     24          """
     25          return self._driver
     26
     27 -    def _set_browser(self, browser):
     28          """
     29          Instance of the WebDriver browser
     30          @type browser: webdriver.Remote
     31          """
     32          self._driver = browser
     33
     34      browser = property(_get_browser, _set_browser)
     35
     36 -    def _get_homepage(self):
     37          """
     38          Instance of the WebDriver homepage
     39          """
     40          return self._homepage
     41
     42 -    def _set_homepage(self, homepage):
     43          """
     44          Instance of the WebDriver homepage
     45          @type homepage: webdriver.Remote
     46          """
     47          self._homepage = homepage
     48
     49      homepage = property(_get_homepage, _set_homepage)
     50
     51      @pytest.fixture(scope='class', autouse=True)
     52 -    def initialize_selenium_class(self, browser, request, tlib_logger, initialize_webdriver_class):
     53          """
     54          @type browser: webdriver.Remote
     55          @type browser: webdriver.Remote
     56          @type request: FixtureRequest
     57          @type tlib_logger: logging
     58          """
     59          self.browser = browser
     60
     61          # Only initialize homepage if it wasn't already initialized
     62          if self.homepage is None:
     63              # noinspection PyUnresolvedReferences
     64              try:
     65                  self.homepage = TLibHelper.get_base_url(request, tlib_logger)
     66              except RuntimeError:
     67                  #If this exception is triggered, command line option --base_url was not specified
     68                  self.homepage = ''
     69
     70      @pytest.fixture(scope='function', autouse=True)
     71 -    def setup_selenium_test(self, request, browser, test_logger, setup_webdriver_test):
     72          """
     73          Goes to homepage before each test, unless marker skipsetup is given
     74
     75          @type request: FixtureRequest
     76          @type test_logger: logging
     77          @type browser: webdriver.Remote
     78          """
     79
     80          #Skip going to homepage if marker skipsetup is specified
     81          if "skipsetup" not in request.node.function.__dict__:
     82              #Before clearing cookies, need to go to home screen, to ensure it's the current domain
     83              self.go_home(save_screenshot=False)
     84              browser.delete_all_cookies()
     85              self.go_home()
     86          else:
     87              test_logger.info("Skipping setup")
     88
     89 -    def navigate(self, page, save_screenshot=True):
     90          """
     91          Navigate to "page"
     92          """
     93          self.test_logger.info(u"Navigating to %s" % page)
     94          self.browser.get(page)
     95          if save_screenshot:
     96              self.save_screenshot(u"Navigate to %s" % page)
     97
     98 -    def go_home(self, save_screenshot=True):
     99          """
    100          Navigate to home page
    101          """
    102          if self.homepage:
    103              self.navigate(self.homepage, save_screenshot=False)
    104              if save_screenshot:
    105                  self.save_screenshot("Homepage")
    106
    107 -    def wait_for_page_loaded(self, timeout=10):
    108          """
    109          Waist until document.readyState is equal to complete
    110          @type timeout: Integer
    111          @param timeout: Number of seconds before timing out
    112          """
    113          if self._request.config.getoption('browser') != 'firefox_no_js':
    114              condition = lambda *args: self._driver.execute_script("return document.readyState") == "complete"
    115              try:
    116                  WebDriverWait(self.browser, timeout).until(condition)
    117              except TimeoutException:
    118                  self.test_logger.error('Timeout while waiting for page to load')
    119                  # noinspection PyUnresolvedReferences
    120                  raise RuntimeError('Timeout while waiting for page to load')
    121          else:
    122              time.sleep(2.0)
    123          self.tlib_logger.debug("Page '%s' finished loading" % self._driver.current_url)
    124