Package: pytest-needle
pytest-needlepytest-needle is a pytest implementation ofneedle.It's fairly similar to needle and shares much of the same functionality,
except it usespytest-seleniumfor handling the webdriver
and implements needle as a fixture instead of having test cases inherit from needle's base test class.InstallationInstall through pip:pipinstallpytest-needleInstall from source:cd/path/to/source/pytest-needle
pythonsetup.pyinstallExampleExample needle pytest implementation:"""test_example.py"""fromselenium.webdriver.common.byimportByimportpytest@pytest.mark.elementdeftest_example_element(needle):"""Example for comparing individual elements:param NeedleDriver needle: NeedleDriver instance:return:"""# Navigate to web pageneedle.driver.get('https://www.google.com')# Take an element screen diffneedle.assert_screenshot('search_field',(By.ID,'tsf'))To create a baseline for all subsequent test run:pytest--driverChrome--needle-save-baselinetest_example.pyAfter we have a baseline, to run test use:pytest--driverChrometest_example.pySelecting a WebDriverTo control which browser to use, use--driver <BROWSER>from pytest-selenium. For example to change to browser to Firefox:pytest--driverFirefoxtest_example.pySetting the viewport's sizeYou may set the size of the browser's viewport using theset_viewport_size()on the needle fixturedeftest_example_viewport(needle):# Navigate to web pageneedle.set_viewport_size(width=1024,height=768)# Rest of the test ...You may also set the default viewport size for all your tests by using the command line argument--needle-viewport-size:pytest--driverChrome--needle-viewport-size"1024 x 768"test_example.pyExcluding areasSometimes areas on a web page may contain dynamic content and cause false negatives, or worse convince testers to raise
the threshold at which changes are acceptable. You can instead choose to mask these areas to avoid the issue of consistently
failing tests:"""test_example.py"""fromselenium.webdriver.common.byimportByimportpytest@pytest.mark.maskdeftest_example_page_with_mask(needle):"""Example for comparing page with a mask:param NeedleDriver needle: NeedleDriver instance:return:"""# Navigate to web pageneedle.driver.get('https://www.google.com')# Take a entire page screen diff, ignore the doodle bannerneedle.assert_screenshot('search_page',threshold=60,exclude=[(By.ID,'hplogo'),(By.ID,'prm')])In the case with Google's home page the doodle banner frequently changes, so to visually regress day-to-day requires
generating new baselines every time the banner is updated. Masking allows only the banner to be ignored while the rest
of the page can be evaluated.EnginesBy default Needle uses the PIL engine (needle.engines.pil_engine.Engine) to take screenshots. Instead of PIL, you may also use PerceptualDiff or ImageMagick.Example with PerceptualDiff:pytest--driverChrome--needle-engineperceptualdifftest_example.pyExample with ImageMagick:pytest--driverChrome--needle-engineimagemagicktest_example.pyBesides being much faster than PIL, PerceptualDiff and ImageMagick also generate a diff PNG file when a test fails, highlighting the differences between the baseline image and the new screenshot.Note that to use the PerceptualDiff engine you will first need todownloadthe perceptualdiff binary and place it in your PATH.To use the ImageMagick engine you will need to install a package on your machine (e.g. sudo apt-get install imagemagick on Ubuntu or brew install imagemagick on OSX).File cleanupEach time you run tests, Needle will create new screenshot images on disk, for comparison with the baseline screenshots.
It’s then up to you whether you want to delete them or archive them. To remove screenshots from successful test use:pytest--driverChrome--needle-cleanup-on-successtest_example.pyAny unsuccessful tests will remain on the file system.File outputTo specify a path for baseline image path use:pytest--driverChrome--needle-baseline-dir/path/to/baseline/imagesDefault path is ./screenshots/baselineTo specify a path for output image path use:pytest--driverChrome--needle-output-dir/path/to/output/imagesDefault path is ./screenshotsGenerating HTML reportsTo generate html reports use:pytest--driverChrome--html=report.html--self-contained-htmlSpecial ThanksSpecial thanks to BrowserStack for providing automated browser testing, at no charge, for this project and other open source projects like this. With over 1000+ device, browser and os versions combinations to choose from and integrations with Travis CI this project could not be successful without the hard work of the BrowserStack team and their continued support of the open source community.