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