Skip to content

base_test

SeleniumHelper(browser, browser_version, splunk_web_url, splunk_mgmt_url, debug=False, cred=('admin', 'Chang3d!'), headless=False, test_case=None)

The helper class provides the Remote Browser

Source code in pytest_splunk_addon_ui_smartx/base_test.py
 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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def __init__(
    self,
    browser,
    browser_version,
    splunk_web_url,
    splunk_mgmt_url,
    debug=False,
    cred=("admin", "Chang3d!"),
    headless=False,
    test_case=None,
):
    self.splunk_web_url = splunk_web_url
    self.splunk_mgmt_url = splunk_mgmt_url
    self.cred = cred
    self.test_case = test_case

    selenium_host = os.environ.get("SELENIUM_HOST")

    try:
        if browser == "firefox":
            if debug:
                self.browser = webdriver.Firefox(
                    firefox_options=SeleniumHelper.get_local_firefox_opts(headless)
                )
            elif selenium_host:
                self.browser = webdriver.Remote(
                    command_executor=f"{selenium_host}:4444/wd/hub",
                    options=SeleniumHelper.get_local_firefox_opts(
                        headless_run=False
                    ),
                )
                self.browser.implicitly_wait(3)
            else:
                raise Exception(
                    f"Firefox tests have to be run either with --local or in CI environment with selenium host!"
                )

        elif browser == "chrome":
            if debug:
                self.browser = webdriver.Chrome(
                    chrome_options=SeleniumHelper.get_local_chrome_opts(headless),
                    service_args=["--verbose"],
                )
            elif selenium_host:
                self.browser = webdriver.Remote(
                    command_executor=f"{selenium_host}:4444/wd/hub",
                    options=SeleniumHelper.get_local_chrome_opts(
                        headless_run=False
                    ),
                )
                self.browser.implicitly_wait(3)
            else:
                raise Exception(
                    f"Chrome tests have to be run either with --local or in CI environment with selenium host!"
                )
        elif browser == "edge":
            if debug:
                self.browser = Edge(
                    executable_path="msedgedriver",
                    desired_capabilities=SeleniumHelper.get_local_edge_opts(
                        headless
                    ),
                    service_args=["--verbose"],
                )
            else:
                raise Exception(
                    f"Edge tests are available only with --local option"
                )
        elif browser == "IE":
            if debug:
                self.browser = webdriver.Ie(
                    capabilities=SeleniumHelper.get_local_ie_opts()
                )
            else:
                raise Exception(f"IE tests are available only with --local option")
        elif browser == "safari":
            if debug:
                self.browser = webdriver.Safari()
            else:
                raise Exception(
                    f"Safari tests are available only with --local option"
                )
        else:
            raise Exception(
                f"No valid browser found.! expected=[firefox, chrome, edge, IE, safari], got={browser}"
            )
    except Exception as e:
        raise e

    try:
        self.browser_session = self.browser.session_id
        self.login_to_splunk(*self.cred)
    except Exception as e:
        self.browser.quit()
        logger.error(
            f"An unexpected error occurred during SeleniumHelper initialization: {e})"
        )
        raise

UccTester

The default setup and teardown methods can be added here. Use in case if some additional configuration should be added to all the test cases

assert_util(left, right, operator='==', left_args={}, right_args={}, msg=None)

Try to check the condition for {WAIT_TIMEOUT} seconds. In UI Automation, it is not possible to expect things to work properly just milliseconds after an action. Even in manual testing, we try things after 4-5 seconds and 2-3 times. This utility method tries to achive the same assertion. To perform certain action multiple time, provide callable functoins with arguments.

Params: left (object or callable): LHS of the operator. right (object or callable): RHS of the operator operator: Operator. Possible values: (==, !=, <, >, <=, >=, in, not in, is, is not) left_args: If left is callable, pass the parameters of the callable function. right_args: If right is callable, pass the parameters of the callable function. msg: Error message if the condition was not matched even after trying for {WAIT_TIMEOUT} seconds.

Source code in pytest_splunk_addon_ui_smartx/base_test.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
def assert_util(
    self, left, right, operator="==", left_args={}, right_args={}, msg=None
):
    """
    Try to check the condition for {WAIT_TIMEOUT} seconds.
    In UI Automation, it is not possible to expect things to work properly just milliseconds after an action.
    Even in manual testing, we try things after 4-5 seconds and 2-3 times.
    This utility method tries to achive the same assertion.
    To perform certain action multiple time, provide callable functoins with arguments.

    Params:
        left (object or callable): LHS of the operator.
        right (object or callable): RHS of the operator
        operator: Operator. Possible values: (==, !=, <, >, <=, >=, in, not in, is, is not)
        left_args: If left is callable, pass the parameters of the callable function.
        right_args: If right is callable, pass the parameters of the callable function.
        msg: Error message if the condition was not matched even after trying for {WAIT_TIMEOUT} seconds.

    """
    args = {
        "left": left,
        "right": right,
        "operator": operator,
        "left_args": left_args,
        "right_args": right_args,
        "left_value": left,
        "right_value": right,
    }
    operator_map = {
        "==": lambda left, right: left == right,
        "!=": lambda left, right: left != right,
        "<": lambda left, right: left < right,
        "<=": lambda left, right: left <= right,
        ">": lambda left, right: left > right,
        ">=": lambda left, right: left >= right,
        "in": lambda left, right: left in right,
        "not in": lambda left, right: left not in right,
        "is": lambda left, right: left is right,
        "is not": lambda left, right: left is not right,
    }

    def _assert(browser):
        try:
            if callable(args["left"]):
                args["left_value"] = args["left"](**args["left_args"])
            if callable(args["right"]):
                args["right_value"] = args["right"](**args["right_args"])
        except TimeoutException as e:
            raise Exception("Timeout: {}".format(str(e)))
        except ElementNotInteractableException as e:
            raise Exception("Element not interactable: {}".format(str(e)))
        return operator_map[args["operator"]](
            args["left_value"], args["right_value"]
        )

    try:
        self.wait.until(_assert)
        condition_failed = False
    except (TimeoutException, ElementNotInteractableException) as e:
        logger.error("Exception raised: {}".format(str(e)))
        condition_failed = True
    if condition_failed:
        if not msg:
            msg = "Condition Failed. \nLeft-value: {}\nOperator: {}\nRight-value: {}".format(
                args["left_value"], args["operator"], args["right_value"]
            )
        assert operator_map[args["operator"]](
            args["left_value"], args["right_value"]
        ), msg