Best Python code snippet using selene_python
conftest.py
Source:conftest.py
1import pytest2import allure3from _pytest.nodes import Item4from _pytest.runner import CallInfo5from selene.core.exceptions import TimeoutException6from selene.support.shared import browser7@pytest.fixture(scope='function', autouse=True)8def browser_management():9 """10 Here, before yield,11 goes all "setup" code for each test case12 aka "before test function" hook13 """14 # def attach_snapshots_on_failure(error: TimeoutException) -> Exception:15 # """16 # An example of selene hook_wait_failure that attaches snapshots to failed test step.17 # It is actually not needed and optional,18 # because in the pytest_runtest_makereport hook below19 # we attach screenshots to the test body itself,20 # that is more handy during analysis of test report21 #22 # but if you need it, you can enable it by uncommenting23 # together with the following ``browser.config.hook_wait_failure =`` line;)24 #25 # otherwise, you can remove it26 # """27 # last_screenshot = browser.config.last_screenshot28 # if last_screenshot:29 # allure.attach.file(source=last_screenshot,30 # name='screenshot on failure',31 # attachment_type=allure.attachment_type.PNG)32 #33 # last_page_source = browser.config.last_page_source34 # if last_page_source:35 # allure.attach.file(source=last_page_source,36 # name='page source on failure',37 # attachment_type=allure.attachment_type.HTML)38 # return error39 # browser.config.hook_wait_failure = attach_snapshots_on_failure40 browser.config.timeout = 341 # todo: add your before setup here...42 yield43 """44 Here, after yield,45 goes all "tear down" code for each test case46 aka "after test function" hook47 """48 # todo: add your after setup here...49 browser.quit()50prev_test_screenshot = None51prev_test_page_source = None52@pytest.hookimpl(tryfirst=True, hookwrapper=True)53def pytest_runtest_setup(item):54 yield55 global prev_test_screenshot56 prev_test_screenshot = browser.config.last_screenshot57 global prev_test_page_source58 prev_test_page_source = browser.config.last_page_source59@pytest.hookimpl(tryfirst=True, hookwrapper=True)60def pytest_runtest_makereport(item: Item, call: CallInfo):61 """62 Attach snapshots on test failure63 """64 # All code prior to yield statement would be ran prior65 # to any other of the same fixtures defined66 outcome = yield # Run all other pytest_runtest_makereport non wrapped hooks67 result = outcome.get_result()68 if result.when == "call" and result.failed:69 last_screenshot = browser.config.last_screenshot70 if last_screenshot and not last_screenshot == prev_test_screenshot:71 allure.attach.file(source=last_screenshot,72 name='screenshot',73 attachment_type=allure.attachment_type.PNG)74 last_page_source = browser.config.last_page_source75 if last_page_source and not last_page_source == prev_test_page_source:76 allure.attach.file(source=last_page_source,77 name='page source',...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!