summaryrefslogtreecommitdiff
path: root/files/puppet/modules/pixelated/files/functional-tests/page_objects/base_page_object.py
blob: f809128be14b3519dede4d662608f69b32460d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#
# Copyright (c) 2015 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By


class BasePageObject(object):
    def __init__(self, context, timeout=10):
        self.context = context
        self.timeout = timeout

    def _find_element_containing_text(self, text, element_type='*'):
        return self._find_element_by_xpath("//%s[contains(.,'%s')]" % (element_type, text))

    def _find_element_by_css_locator(self, locator, timeout=None, dom_context=None):
        locator_tuple = (By.CSS_SELECTOR, locator)
        self._wait_until(EC.visibility_of_element_located(locator_tuple), timeout or self.timeout)
        context = dom_context or self.context.browser
        return context.find_element_by_css_selector(locator)

    def _find_element_by_id(self,id):
       return self.context.browser.find_element_by_id(id)

    def _find_elements_by_css_locator(self, locator, timeout=None):
        locator_tuple = (By.CSS_SELECTOR, locator)
        self._wait_until(EC.visibility_of_element_located(locator_tuple), timeout or self.timeout)
        return self.context.browser.find_elements_by_css_selector(locator)

    def _find_elements_by_xpath(self, xpath, timeout=None):
        locator_tuple = (By.XPATH, xpath)
        self._wait_until(EC.visibility_of_element_located(locator_tuple), timeout or self.timeout)
        return self.context.browser.find_elements_by_xpath(xpath)

    def _find_element_by_xpath(self, xpath, timeout=None, dom_context=None):
        locator_tuple = (By.XPATH, xpath)
        self._wait_until(EC.visibility_of_element_located(locator_tuple), timeout or self.timeout)
        context = dom_context or self.context.browser
        return self.context.browser.find_element_by_xpath(xpath)

    def _wait_element_to_be_removed(self, locator, timeout=None):
        locator_tuple = (By.CSS_SELECTOR, locator)
        self._wait_until(EC.invisibility_of_element_located(locator_tuple), timeout or self.timeout)

    def _wait_element_to_be_removed_by_xpath(self, xpath, timeout=None):
        locator_tuple = (By.XPATH, xpath)
        self._wait_until(EC.invisibility_of_element_located(locator_tuple), timeout or self.timeout)

    def _wait_until(self, condition_function, timeout=None):
        wait = WebDriverWait(self.context.browser, timeout or self.timeout)
        wait.until(condition_function)

    def wait_until_element_is_visible_by_css_locator(self, locator):
        wait = WebDriverWait(self.context.browser, 60)
        wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, locator)))
        return self.context.browser.find_element_by_selector(locator)

    def wait_until_elements_are_visible_by_css_locator(self, locator):
        wait = WebDriverWait(self.context.browser, 60)
        wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, locator)))

    def wait_until_element_is_visible_by_locator(self, locator):
        wait = WebDriverWait(self.context.browser, 60)
        by, locator = locator
        wait.until(EC.visibility_of_element_located((by, locator)))
        return self.context.browser.find_element(by, locator)

    def wait_until_element_is_invisible_by_locator(self, locator):
        wait = WebDriverWait(self.context.browser, 60)
        by, locator = locator
        wait.until(EC.invisibility_of_element_located((by, locator)))