Best Python code snippet using ATX
conftest.py
Source: conftest.py
1import pytest2import allure3from _pytest.nodes import Item4from _pytest.runner import CallInfo5from selene.support.shared import browser6import project7import web_test.helpers.allure.gherkin8def pytest_addoption(parser):9 project.Config.register(parser)10@pytest.fixture11def config(request):12 if not project.config:13 project.config = project.Config(request)14 return project.config15@pytest.fixture(scope='function', autouse=True)16def browser_management(config):17 """18 Here, before yield,19 goes all "setup" code for each test case20 aka "before test function" hook21 """22 # def attach_snapshots_on_failure(error: TimeoutException) -> Exception:23 # """24 # An example of selene hook_wait_failure that attaches snapshots to failed test step.25 # It is actually not needed and optional,26 # because in the pytest_runtest_makereport hook below27 # we attach screenshots to the test body itself,28 # that is more handy during analysis of test report29 #30 # but if you need it, you can enable it by uncommenting31 # together with the following ``browser.config.hook_wait_failure =`` line;)32 #33 # otherwise, you can remove it34 # """35 # last_screenshot = browser.config.last_screenshot36 # if last_screenshot:37 # allure.attach.file(source=last_screenshot,38 # name='screenshot on failure',39 # attachment_type=allure.attachment_type.PNG)40 #41 # last_page_source = browser.config.last_page_source42 # if last_page_source:43 # allure.attach.file(source=last_page_source,44 # name='page source on failure',45 # attachment_type=allure.attachment_type.HTML)46 # return error47 # browser.config.hook_wait_failure = attach_snapshots_on_failure48 browser.config.timeout = config.timeout49 browser.config.save_page_source_on_failure \50 = config.save_page_source_on_failure51 # todo: add your before setup here...52 yield53 """54 Here, after yield,55 goes all "tear down" code for each test case56 aka "after test function" hook57 """58 # todo: add your after setup here...59 browser.quit()60prev_test_screenshot = None61prev_test_page_source = None62@pytest.hookimpl(tryfirst=True, hookwrapper=True)63def pytest_runtest_setup(item):64 yield65 global prev_test_screenshot66 prev_test_screenshot = browser.config.last_screenshot67 global prev_test_page_source68 prev_test_page_source = browser.config.last_page_source69@pytest.hookimpl(tryfirst=True, hookwrapper=True)70def pytest_runtest_makereport(item: Item, call: CallInfo):71 """72 Attach snapshots on test failure73 """74 # All code prior to yield statement would be ran prior75 # to any other of the same fixtures defined76 outcome = yield # Run all other pytest_runtest_makereport non wrapped hooks77 result = outcome.get_result()78 if web_test.helpers.allure.gherkin.when == 'call' and result.failed:79 last_screenshot = browser.config.last_screenshot80 if last_screenshot and not last_screenshot == prev_test_screenshot:81 allure.attach.file(source=last_screenshot,82 name='screenshot',83 attachment_type=allure.attachment_type.PNG)84 last_page_source = browser.config.last_page_source85 if last_page_source and not last_page_source == prev_test_page_source:86 allure.attach.file(source=last_page_source,87 name='page source',...
mvscreenshot.py
Source: mvscreenshot.py
1import typer2from pathlib import Path3from datetime import datetime, timedelta4app = typer.Typer(add_completion=False)5def pretty_delta(delta: timedelta):6 if delta.seconds < 0:7 return "in the future"8 elif delta.seconds < 120:9 return f"{delta.seconds} seconds ago"10 elif delta.seconds < 60 * 120:11 return f"{delta.seconds // 60} minutes ago"12 else:13 return f"{delta.seconds // (60 * 60)} hours ago"14@app.command()15def main(16 destination: Path,17 markdown: bool = typer.Option(18 False,19 "-m",20 help="Print markdown to use image",21 ),22):23 pictures_path = Path.home() / "Pictures"24 screenshots = list(pictures_path.glob("Screenshot from *.png"))25 screenshots.sort()26 last_screenshot = screenshots[-1]27 last_date = datetime.strptime(28 last_screenshot.name, "Screenshot from %Y-%m-%d %H-%M-%S.png"29 )30 now = datetime.now()31 delta = now - last_date32 delta_text = pretty_delta(delta)33 if destination.suffix != last_screenshot.suffix:34 destination = destination.with_suffix(last_screenshot.suffix)35 print(f"Screenshot from {delta_text}")36 print(f"Move to: {destination}")37 confirm = input("Confirm [Y/n]: ")38 if confirm.lower() in {"y", "yes", ""}:39 last_screenshot.rename(destination)40 if markdown:41 if "docs" in destination.parts:42 docs_index = destination.parts.index("docs")43 docs_path = Path(*destination.parts[: docs_index + 1])44 relative_path = destination.relative_to(docs_path)45 else:46 relative_path = destination47 print("=" * 10)48 print(f'')49if __name__ == "__main__":...
Check out the latest blogs from LambdaTest on this topic:
As part of one of my consulting efforts, I worked with a mid-sized company that was looking to move toward a more agile manner of developing software. As with any shift in work style, there is some bewilderment and, for some, considerable anxiety. People are being challenged to leave their comfort zones and embrace a continuously changing, dynamic working environment. And, dare I say it, testing may be the most ‘disturbed’ of the software roles in agile development.
QA testers have a unique role and responsibility to serve the customer. Serving the customer in software testing means protecting customers from application defects, failures, and perceived failures from missing or misunderstood requirements. Testing for known requirements based on documentation or discussion is the core of the testing profession. One unique way QA testers can both differentiate themselves and be innovative occurs when senseshaping is used to improve the application user experience.
Manual cross browser testing is neither efficient nor scalable as it will take ages to test on all permutations & combinations of browsers, operating systems, and their versions. Like every developer, I have also gone through that ‘I can do it all phase’. But if you are stuck validating your code changes over hundreds of browsers and OS combinations then your release window is going to look even shorter than it already is. This is why automated browser testing can be pivotal for modern-day release cycles as it speeds up the entire process of cross browser compatibility.
Unit testing is typically software testing within the developer domain. As the QA role expands in DevOps, QAOps, DesignOps, or within an Agile team, QA testers often find themselves creating unit tests. QA testers may create unit tests within the code using a specified unit testing tool, or independently using a variety of methods.
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!!