Skip to content

plugin

get_browser_scope(fixture_name, config)

Set the scope of the browser dynamically.

Source code in pytest_splunk_addon_ui_smartx/plugin.py
157
158
159
160
161
162
163
164
def get_browser_scope(fixture_name, config):
    """
    Set the scope of the browser dynamically.
    """
    if config.getoption("--local") and config.getoption("--persist-browser"):
        return "session"
    else:
        return "function"

pytest_addoption(parser)

Register argparse-style options and ini-style config values, called once at the beginning of a test run.

Source code in pytest_splunk_addon_ui_smartx/plugin.py
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
def pytest_addoption(parser):
    """
    Register argparse-style options and ini-style config values, called once at the beginning of a test run.
    """
    parser.conflict_handler = "resolve"
    group = parser.getgroup("splunk-ucc-smartx")

    group.addoption(
        "--browser",
        action="store",
        help=(
            "The browser on which the test should run. supported_values: (firefox, chrome, edge, IE, safari)."
        ),
    )

    group.addoption(
        "--local", action="store_true", help="The test will be run on local browsers"
    )

    group.addoption(
        "--persist-browser",
        action="store_true",
        help=(
            "For local execution, keep a single browser to executed all tests."
            " (Only supported with --local)"
        ),
    )

    group.addoption(
        "--setup-retry-count",
        action="store_true",
        default="3",
        help="The number of times the browser should try to connect to the SeleniumBrowser",
    )

    group.addoption(
        "--headless", action="store_true", help="Run the test case on headless mode"
    )

pytest_collection_modifyitems(config, items)

Add browser name as prefix in test class name

Source code in pytest_splunk_addon_ui_smartx/plugin.py
42
43
44
45
46
47
48
49
50
51
52
53
54
def pytest_collection_modifyitems(config, items):
    """
    Add browser name as prefix in test class name
    """
    browser = config.getoption("--browser")
    if browser:
        if len(browser.split(":")) == 2:
            browser, _ = browser.split(":")
        for item in items:
            class_name = item.nodeid.split("::")[-2]
            item._nodeid = item.nodeid.replace(
                class_name, "{}_{}".format(browser, class_name)
            )

pytest_configure(config)

Setup configuration after command-line options are parsed

Source code in pytest_splunk_addon_ui_smartx/plugin.py
29
30
31
32
33
34
35
36
37
38
39
def pytest_configure(config):
    """
    Setup configuration after command-line options are parsed
    """
    config.addinivalue_line("markers", "ucc: UCC Tests")
    pytest_html = config.pluginmanager.getplugin("html")
    if pytest_html:
        try:
            os.mkdir(PNG_PATH)
        except OSError:
            pass

pytest_runtest_makereport(item, call)

pytest_runtest_makereport will be called after each test case is executed. Capture a screenshot if the test case is failed. item.selenium_helper has been added by the fixture “test_helper”. The scope of the fixture must be function.

:param item: the method of the test case.
Source code in pytest_splunk_addon_ui_smartx/plugin.py
245
246
247
248
249
250
251
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
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    """
    pytest_runtest_makereport will be called after each test case is executed.
    Capture a screenshot if the test case is failed.
    item.selenium_helper has been added by the fixture "test_helper". The scope of the fixture must be function.

        :param item: the method of the test case.
    """
    LOGGER.debug("pytest_runtest_makereport: Start making report")
    try:
        pytest_html = item.config.pluginmanager.getplugin("html")
        if pytest_html:
            outcome = yield
            report = outcome.get_result()
            if report.when == "call" or report.when == "setup":
                setattr(item, "report", report)
                if report.failed:
                    try:
                        if report.when == "setup":
                            # Possible - Login failed
                            # the item will not have selenium_helper
                            screenshot_path = os.path.join(PNG_PATH, "login_error.png")
                        else:
                            # Test Failed
                            screenshot_path = os.path.join(
                                PNG_PATH, item.nodeid.split("::")[-1] + ".png"
                            )
                            item.selenium_helper.browser.save_screenshot(
                                screenshot_path
                            )

                        report.extra = [pytest_html.extras.image(screenshot_path)]
                    except:
                        LOGGER.warning(
                            "Screenshot can not be captured. Scope of the fixture test_helper must be 'function' to capture the screenshot. "
                        )
        else:
            LOGGER.warning(
                "pytest-html is not installed. Install by using: pip install pytest-html"
            )
    except Exception as e:
        LOGGER.warning(
            "Got exception while making test report. Exception  {}".format(e)
        )
        LOGGER.debug("test_report, Exception: {}".format(traceback.format_exc()))

ucc_smartx_configs(request)

Configure pytest parameters, if provided

Source code in pytest_splunk_addon_ui_smartx/plugin.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
@pytest.fixture(scope="session")
def ucc_smartx_configs(request):
    """
    Configure pytest parameters, if provided
    """

    if not request.config.getoption("--browser"):
        raise ValueError(
            "--browser parameter was not provided while running the test cases."
        )
    driver = request.config.getoption("--browser")
    LOGGER.debug("--browser={}".format(driver))
    if len(driver.split(":")) == 2:
        driver, driver_version = driver.split(":")
    else:
        driver_version = "latest"

    if request.config.getoption("--local"):
        local_run = True
        LOGGER.debug("--debug")
    else:
        local_run = False

    if request.config.getoption("--setup-retry-count"):
        retry_count = int(request.config.getoption("--setup-retry-count"))
        LOGGER.debug("--setup-retry-count={}".format(retry_count))
    else:
        retry_count = 3

    if request.config.getoption("--headless"):
        headless_run = True
        LOGGER.debug("--headless")
    else:
        headless_run = False

    LOGGER.info(
        f"Calling SeleniumHelper with:: browser={driver}, local-run={local_run}, headless={headless_run})"
    )
    smartx_configs = SmartConfigs(
        driver=driver,
        driver_version=driver_version,
        local_run=local_run,
        retry_count=retry_count,
        headless_run=headless_run,
    )
    return smartx_configs

ucc_smartx_selenium_wrapper(request)

Calls ucc_smartx_selenium_helper fixture

Source code in pytest_splunk_addon_ui_smartx/plugin.py
234
235
236
237
238
239
240
241
242
@pytest.fixture(scope="function", autouse=True)
def ucc_smartx_selenium_wrapper(request):
    """
    Calls ucc_smartx_selenium_helper fixture
    """
    if "ucc_smartx_selenium_helper" in request.fixturenames:
        request.node.selenium_helper = request.getfixturevalue(
            "ucc_smartx_selenium_helper"
        )