Best Python code snippet using selene_python
conftest.py
Source:conftest.py
1import pytest2import allure3from _pytest.nodes import Item4from _pytest.runner import CallInfo5from selene.support.shared import browser, SharedConfig, SharedBrowser6import config7import web_test.help.allure.gherkin8from web_test.help.allure import report9from web_test.help.python import monkey10from web_test.help.selene.report.wait import ReportedWait11@pytest.fixture(scope='session', autouse=True)12def add_reporting_to_selene_steps():13 original_open = browser.open14 @monkey.patch_method_in(SharedBrowser)15 def open(self, relative_or_absolute_url: str):16 return report.step(original_open)(relative_or_absolute_url)17 @monkey.patch_method_in(SharedConfig) # todo: consider patching Wait explicitly18 def wait(self, entity):19 hook = self._inject_screenshot_and_page_source_pre_hooks(self.hook_wait_failure)20 return ReportedWait(entity, at_most=self.timeout, or_fail_with=hook)21@pytest.fixture(scope='function', autouse=True)22def browser_management():23 """24 Here, before yield,25 goes all "setup" code for each test case26 aka "before test function" hook27 """28 # def attach_snapshots_on_failure(error: TimeoutException) -> Exception:29 # """30 # An example of selene hook_wait_failure that attaches snapshots to failed test step.31 # It is actually not needed and optional,32 # because in the pytest_runtest_makereport hook below33 # we attach screenshots to the test body itself,34 # that is more handy during analysis of test report35 #36 # but if you need it, you can enable it by uncommenting37 # together with the following ``browser.config.hook_wait_failure =`` line;)38 #39 # otherwise, you can remove it40 # """41 # last_screenshot = browser.config.last_screenshot42 # if last_screenshot:43 # allure.attach.file(source=last_screenshot,44 # name='screenshot on failure',45 # attachment_type=allure.attachment_type.PNG)46 #47 # last_page_source = browser.config.last_page_source48 # if last_page_source:49 # allure.attach.file(source=last_page_source,50 # name='page source on failure',51 # attachment_type=allure.attachment_type.HTML)52 # return error53 # browser.config.hook_wait_failure = attach_snapshots_on_failure54 browser.config.timeout = config.settings.timeout55 browser.config.save_page_source_on_failure \56 = config.settings.save_page_source_on_failure57 yield58 """59 Here, after yield,60 goes all "tear down" code for each test case61 aka "after test function" hook62 """63 browser.config.hold_browser_open = config.settings.hold_browser_open64 if not config.settings.hold_browser_open:65 browser.quit()66prev_test_screenshot = None67prev_test_page_source = None68@pytest.hookimpl(tryfirst=True, hookwrapper=True)69def pytest_runtest_setup(item):70 yield71 global prev_test_screenshot72 prev_test_screenshot = browser.config.last_screenshot73 global prev_test_page_source74 prev_test_page_source = browser.config.last_page_source75@pytest.hookimpl(tryfirst=True, hookwrapper=True)76def pytest_runtest_makereport(item: Item, call: CallInfo):77 """78 Attach snapshots on test failure79 """80 # All code prior to yield statement would be ran prior81 # to any other of the same fixtures defined82 outcome = yield # Run all other pytest_runtest_makereport non wrapped hooks83 result = outcome.get_result()84 if web_test.help.allure.gherkin.when == 'call' and result.failed:85 last_screenshot = browser.config.last_screenshot86 if last_screenshot and not last_screenshot == prev_test_screenshot:87 allure.attach.file(source=last_screenshot,88 name='screenshot',89 attachment_type=allure.attachment_type.PNG)90 last_page_source = browser.config.last_page_source91 if last_page_source and not last_page_source == prev_test_page_source:92 allure.attach.file(source=last_page_source,93 name='page source',...
configuration.py
Source:configuration.py
...62 @property63 def timeout(self) -> int:64 return self._timeout65 @property66 def hook_wait_failure(self) -> Callable[[TimeoutException], Exception]:67 return self._hook_wait_failure68 def wait(self, entity):69 return Wait(70 entity, at_most=self.timeout, or_fail_with=self.hook_wait_failure71 )72 @property73 def base_url(self) -> str:74 return self._base_url75 @property76 def set_value_by_js(self) -> bool:77 return self._set_value_by_js78 @property79 def type_by_js(self) -> bool:80 return self._type_by_js...
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!!