Skip to content

base_component

ActionChains(browser)

Bases: ActionChains

Purpose: It is a workaround by wrapping ActionChains class so that key_action.pause is not used in Safari browser.

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
36
37
38
39
def __init__(self, browser):
    super().__init__(browser)
    if browser.name in ("Safari", "Safari Technology Preview"):
        self.w3c_actions.key_action.pause = lambda *a, **k: None

BaseComponent(browser, container)

Purpose: The base class for the component. A component is an UI component with which a user interacts with. The component class will have all the interaction method which can be done to the component.

Implementation: - The component will have set of locators. Locators can be of type (ID, CSS_Selector, classname, Name, etc. whichever supported by selenium) - Each method will interact with theses locators directly. - The component should have a container, so that it does not have multiple confusing instances in a same page. - In a container, there should be only one component of the same type.

Parameters:

Name Type Description Default
browser

The instance of the selenium webdriver

required
container

The container in which the component is located at.

required
Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
55
56
57
58
59
60
61
62
63
def __init__(self, browser, container):
    """
    :param browser: The instance of the selenium webdriver
    :param container: The container in which the component is located at.
    """
    self.elements = dict()
    self.browser = browser
    self.wait = WebDriverWait(self.browser, DEFAULT_TIMEOUT)
    self.elements["container"] = container

__getattr__(key)

Makes the web-elements to be accessible directly. - For example self.elements = {“textbox”: Selector(by=…, select=…), Access the element by doing self.textbox directly. - It also has implicit wait while finding the element. :param key: The key of the element mentioned in self.elements :returns: The webelement we are accessing

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
212
213
214
215
216
217
218
219
220
221
222
223
224
def __getattr__(self, key):
    """
    Makes the web-elements to be accessible directly.
    - For example self.elements = {"textbox": Selector(by=..., select=...),
        Access the element by doing self.textbox directly.
    - It also has implicit wait while finding the element.
      :param key: The key of the element mentioned in self.elements
      :returns: The webelement we are accessing
    """
    try:
        return self.get_element(key)
    except KeyError:
        raise

get_child_element(key)

Get the web-element located inside the container. - It is more preferable to use get_child_element over get_element. - get_element should only be used if the element is out of the container for some reason. For example, in case of some pop-up.

Note: There is a wait in the method. :param key: The key of the element mentioned in self.elements :returns: The child element of the element searched by key

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
 99
100
101
102
103
104
105
106
107
108
109
110
def get_child_element(self, key):
    """
    Get the web-element located inside the container.
        - It is more preferable to use get_child_element over get_element.
        - get_element should only be used if the element is out of the container for some reason. For example, in case of some pop-up.

    Note: There is a wait in the method.
        :param key: The key of the element mentioned in self.elements
        :returns: The child element of the element searched by key
    """
    element = self.elements[key]
    return self._get_child_element(element.by, element.select)

get_child_elements(key)

Get the list of web-elements located inside the container. Returns empty list of no elements found. - It is more preferable to use get_child_elements over get_elements. - get_elements should only be used if the element is out of the container for some reason. For example, in case of some pop-up.

Note: There is a wait in the method. :param key: The key of the element mentioned in self.elements :returns: list The child elements of the element searched by key

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def get_child_elements(self, key):
    """
    Get the list of web-elements located inside the container. Returns empty list of no elements found.
        - It is more preferable to use get_child_elements over get_elements.
        - get_elements should only be used if the element is out of the container for some reason. For example, in case of some pop-up.

    Note: There is a wait in the method.
        :param key: The key of the element mentioned in self.elements
        :returns: list The child elements of the element searched by key
    """
    try:
        self.wait_for(key)
        element = self.elements[key]
        return self._get_child_elements(element.by, element.select)
    except:
        return list()

get_clear_text(web_element)

Gets the text of the web element :param web_element: The instance of the web element we are getting tect from. :returns: str the text of the web elements

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
65
66
67
68
69
70
71
def get_clear_text(self, web_element):
    """
    Gets the text of the web element
        :param web_element: The instance of the web element we are getting tect from.
        :returns: str the text of the web elements
    """
    return re.sub(r"\s+", " ", web_element.get_attribute("innerText")).strip()

get_element(key)

Get the web-element.

Note: There is a wait in get_element. :param key: The key of the element mentioned in self.elements :returns: element The element we are looking for by key

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
73
74
75
76
77
78
79
80
81
82
def get_element(self, key):
    """
    Get the web-element.

    Note: There is a wait in get_element.
        :param key: The key of the element mentioned in self.elements
        :returns: element The element we are looking for by key
    """
    element = self.elements[key]
    return self._get_element(element.by, element.select)

get_elements(key)

Get the list of web-elements.

Note: There is a wait in the method. :param key: The key of the element mentioned in self.elements :returns: list of elements we are searching for by key, or an empty list

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def get_elements(self, key):
    """
    Get the list of web-elements.

    Note: There is a wait in the method.
        :param key: The key of the element mentioned in self.elements
        :returns: list of elements we are searching for by key, or an empty list
    """
    try:
        self.wait_for(key)
        element = self.elements[key]
        return self._get_elements(element.by, element.select)
    except:
        return list()

get_tuple(key)

get the locator of the element in a tuple form. :param key: The key of the element mentioned in self.elements :returns: Tuple of the locator

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
129
130
131
132
133
134
135
def get_tuple(self, key):
    """
    get the locator of the element in a tuple form.
        :param key: The key of the element mentioned in self.elements
        :returns: Tuple of the locator
    """
    return self.elements[key].by, self.elements[key].select

hover_over_element(key)

Hover over an element, such as a tooltip, such that other items will appear :param key: The key of the element mentioned in self.elements

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
226
227
228
229
230
231
232
233
def hover_over_element(self, key):
    """
    Hover over an element, such as a tooltip, such that other items will appear
        :param key: The key of the element mentioned in self.elements
    """
    hover = (
        ActionChains(self.browser).move_to_element(self.get_element(key)).perform()
    )

wait_for(key, msg=None, timeout=None)

if key in element, Wait for an web element to be visible. Raises TimeoutException if the element not found. if key is a condition, wait for the condition to be true.

:param key: The key of the element mentioned in self.elements
:param msg: The error-msg which should be mentioned in the TimeoutException
:param timeout: The amount of time specified to wait for the wait function
Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def wait_for(self, key, msg=None, timeout=None):
    """
    if key in element, Wait for an web element to be visible. Raises TimeoutException if the element not found.
    if key is a condition, wait for the condition to be true.

        :param key: The key of the element mentioned in self.elements
        :param msg: The error-msg which should be mentioned in the TimeoutException
        :param timeout: The amount of time specified to wait for the wait function
    """
    if timeout:
        wait = WebDriverWait(self.browser, timeout)
    else:
        wait = self.wait
    if key in self.elements:
        if not msg:
            msg = "{} element is not present".format(key)
        return wait.until(EC.presence_of_element_located(self.get_tuple(key)), msg)
    else:
        if not msg:
            msg = "{}: Timeout while waiting for the condition to be true".format(
                key
            )
        return wait.until(key, msg)

wait_for_text(key, msg=None)

if key in element, Wait for an text in web element to be visible. Raises TimeoutException if the text not element found. :param key: The key of the element mentioned in self.elements :param msg: The error-msg which should be mentioned in the TimeoutException

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
161
162
163
164
165
166
167
168
169
170
171
172
173
def wait_for_text(self, key, msg=None):
    """
    if key in element, Wait for an text in web element to be visible. Raises TimeoutException if the text not element found.
        :param key: The key of the element mentioned in self.elements
        :param msg: The error-msg which should be mentioned in the TimeoutException
    """
    if not msg:
        msg = "Text not present in element {}".format(key)

    def _wait_for_text(browser):
        return len(self.get_element(key).text.strip()) > 0

    return self.wait_for(_wait_for_text, msg)

wait_to_be_clickable(key, msg=None)

Wait for an web element to be invisible. Raises TimeoutException if the element does not dissapear. :param key: The key of the element mentioned in self.elements :param msg: The error-msg which should be mentioned in the TimeoutException

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
201
202
203
204
205
206
207
208
209
210
def wait_to_be_clickable(self, key, msg=None):
    """
    Wait for an web element to be invisible. Raises TimeoutException if the element does not dissapear.
        :param key: The key of the element mentioned in self.elements
        :param msg: The error-msg which should be mentioned in the TimeoutException
    """
    if not msg:
        msg = "{} element is not clickable".format(key)
    self.wait.until(EC.element_to_be_clickable(self.get_tuple(key)), msg)
    sleep(1)

wait_to_display()

Wait for the component container to be displayed

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
185
186
187
188
189
def wait_to_display(self):
    """
    Wait for the component container to be displayed
    """
    self.wait_for("container")

wait_until(key, msg=None)

Wait for an web element to be invisible. Raises TimeoutException if the element does not dissapear. :param key: The key of the element mentioned in self.elements :param msg: The error-msg which should be mentioned in the TimeoutException

Source code in pytest_splunk_addon_ui_smartx/components/base_component.py
175
176
177
178
179
180
181
182
183
def wait_until(self, key, msg=None):
    """
    Wait for an web element to be invisible. Raises TimeoutException if the element does not dissapear.
        :param key: The key of the element mentioned in self.elements
        :param msg: The error-msg which should be mentioned in the TimeoutException
    """
    if not msg:
        msg = "{} element did not disappear".format(key)
    self.wait.until(EC.invisibility_of_element_located(self.get_tuple(key)), msg)