Best Python code snippet using toolium_python
test_visual_test.py
Source:test_visual_test.py
1# -*- coding: utf-8 -*-2"""3Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.4This file is part of Toolium.5Licensed under the Apache License, Version 2.0 (the "License");6you may not use this file except in compliance with the License.7You may obtain a copy of the License at8 http://www.apache.org/licenses/LICENSE-2.09Unless required by applicable law or agreed to in writing, software10distributed under the License is distributed on an "AS IS" BASIS,11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12See the License for the specific language governing permissions and13limitations under the License.14"""15import mock16import os17import pkg_resources18import pytest19import re20import shutil21from PIL import Image22from needle.engines.imagemagick_engine import Engine as MagickEngine23from needle.engines.perceptualdiff_engine import Engine as PerceptualEngine24from needle.engines.pil_engine import Engine as PilEngine25from toolium.config_files import ConfigFiles26from toolium.driver_wrapper import DriverWrapper27from toolium.driver_wrapper import DriverWrappersPool28from toolium.test.utils.test_driver_utils import get_mock_element29from toolium.visual_test import VisualTest30# Get file paths31root_path = os.path.dirname(os.path.realpath(__file__))32file_v1 = os.path.join(root_path, 'resources', 'register.png')33file_v2 = os.path.join(root_path, 'resources', 'register_v2.png')34file_small = os.path.join(root_path, 'resources', 'register_small.png')35file_scroll = os.path.join(root_path, 'resources', 'register_chrome_scroll.png')36file_ios = os.path.join(root_path, 'resources', 'ios.png')37file_mac = os.path.join(root_path, 'resources', 'mac_os_retina.png')38@pytest.fixture39def driver_wrapper():40 # Remove previous visual path41 root_path = os.path.dirname(os.path.realpath(__file__))42 visual_path = os.path.join(root_path, 'output', 'visualtests')43 if os.path.exists(visual_path):44 shutil.rmtree(visual_path)45 # Reset wrappers pool values46 DriverWrappersPool._empty_pool()47 DriverWrapper.config_properties_filenames = None48 # Create a new wrapper49 driver_wrapper = DriverWrappersPool.get_default_wrapper()50 driver_wrapper.utils.get_window_size = mock.MagicMock()51 driver_wrapper.driver = mock.MagicMock()52 # Configure properties53 root_path = os.path.dirname(os.path.realpath(__file__))54 config_files = ConfigFiles()55 config_files.set_config_directory(os.path.join(root_path, 'conf'))56 config_files.set_config_properties_filenames('properties.cfg')57 config_files.set_output_directory(os.path.join(root_path, 'output'))58 driver_wrapper.configure(config_files)59 driver_wrapper.config.set('VisualTests', 'enabled', 'true')60 yield driver_wrapper61 # Remove visual path62 visual_path = os.path.join(root_path, 'output', 'visualtests')63 if os.path.exists(visual_path):64 shutil.rmtree(visual_path)65 # Reset wrappers pool values66 DriverWrappersPool._empty_pool()67 DriverWrapper.config_properties_filenames = None68def test_no_enabled(driver_wrapper):69 # Update conf and create a new VisualTest instance70 driver_wrapper.config.set('VisualTests', 'enabled', 'false')71 visual = VisualTest(driver_wrapper)72 visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')73 driver_wrapper.driver.save_screenshot.assert_not_called()74def test_engine_pil(driver_wrapper):75 visual = VisualTest(driver_wrapper)76 assert isinstance(visual.engine, PilEngine)77def test_engine_perceptual(driver_wrapper):78 # Update conf and create a new VisualTest instance79 driver_wrapper.config.set('VisualTests', 'engine', 'perceptualdiff')80 visual = VisualTest(driver_wrapper)81 assert isinstance(visual.engine, PerceptualEngine)82def test_engine_magick(driver_wrapper):83 # Update conf and create a new VisualTest instance84 driver_wrapper.config.set('VisualTests', 'engine', 'imagemagick')85 visual = VisualTest(driver_wrapper)86 assert isinstance(visual.engine, MagickEngine)87def test_engine_empty(driver_wrapper):88 # Update conf and create a new VisualTest instance89 driver_wrapper.config.set('VisualTests', 'engine', '')90 visual = VisualTest(driver_wrapper)91 assert isinstance(visual.engine, PilEngine)92def test_engine_unknown(driver_wrapper):93 # Update conf and create a new VisualTest instance94 driver_wrapper.config.set('VisualTests', 'engine', 'unknown')95 visual = VisualTest(driver_wrapper)96 assert isinstance(visual.engine, PilEngine)97def test_compare_files_equal(driver_wrapper):98 visual = VisualTest(driver_wrapper)99 message = visual.compare_files('report_name', file_v1, file_v1, 0)100 assert message is None101def test_compare_files_diff(driver_wrapper):102 visual = VisualTest(driver_wrapper)103 message = visual.compare_files('report_name', file_v1, file_v2, 0)104 assert 'Distance of 0.00080181' in message105def test_compare_files_diff_fail(driver_wrapper):106 # Update conf and create a new VisualTest instance107 driver_wrapper.config.set('VisualTests', 'fail', 'true')108 visual = VisualTest(driver_wrapper)109 with pytest.raises(AssertionError) as exc:110 visual.compare_files('report_name', file_v1, file_v2, 0)111 assert str(exc.value) == "The new screenshot '%s' did not match the baseline '%s' " \112 "(by a distance of 522.65)" % (file_v1, file_v2)113def test_compare_files_size(driver_wrapper):114 visual = VisualTest(driver_wrapper)115 message = visual.compare_files('report_name', file_v1, file_small, 0)116 if pkg_resources.get_distribution('pytest').version == '2.9.2':117 # PIL returns an empty error, but PyTest modifies AssertionError118 assert message.startswith('assert (1680, 388) == (1246, 388)')119 else:120 assert message == 'Image dimensions do not match'121def test_compare_files_size_fail(driver_wrapper):122 # Update conf and create a new VisualTest instance123 driver_wrapper.config.set('VisualTests', 'fail', 'true')124 visual = VisualTest(driver_wrapper)125 with pytest.raises(AssertionError) as exc:126 visual.compare_files('report_name', file_v1, file_small, 0)127 if pkg_resources.get_distribution('pytest').version == '2.9.2':128 # PIL returns an empty error, but PyTest modifies AssertionError129 assert str(exc.value).startswith('assert (1680, 388) == (1246, 388)')130 else:131 assert str(exc.value) == ''132def test_get_img_element(driver_wrapper):133 expected_img = r'<img src=".*register_v2.png" title="Baseline image"/>'134 visual = VisualTest(driver_wrapper)135 img = visual._get_img_element('register_v2.png', 'Baseline image')136 assert re.compile(expected_img).match(img) is not None137def test_get_html_row(driver_wrapper):138 expected_row = r'<tr class=diff><td>report_name</td><td><img src=".*register_v2.png" title="Baseline image"/>' \139 '</td><td><img src=".*register.png" title="Screenshot image"/></td><td></td></tr>'140 visual = VisualTest(driver_wrapper)141 row = visual._get_html_row('diff', 'report_name', file_v1, file_v2)142 assert re.compile(expected_row).match(row) is not None143def test_diff_message_equal():144 message = None145 expected_message = ''146 assert expected_message == VisualTest._get_diff_message(message, 1000000)147def test_diff_message_size_pil():148 message = ''149 expected_message = 'Image dimensions do not match'150 assert expected_message == VisualTest._get_diff_message(message, 1000000)151def test_diff_message_size_perceptualdiff():152 message = """The new screenshot 'C:\\...\\01_login_form__test_login.png' did not match the baseline 'C:\\...\\login_form.png':153 FAIL: Image dimensions do not match"""154 expected_message = 'Image dimensions do not match'155 assert expected_message == VisualTest._get_diff_message(message, 1000000)156def test_diff_message_size_imagemagick():157 message = 'Image dimensions do not match'158 expected_message = 'Image dimensions do not match'159 assert expected_message == VisualTest._get_diff_message(message, 1000000)160def test_diff_message_diff_pil():161 message = ("The new screenshot 'C:\\...\\01_login_form__test_login.png' did not match the baseline "162 "'C:\\...\\login_form.png' (by a distance of 14794.11)")163 expected_message = 'Distance of 0.01479411'164 assert expected_message == VisualTest._get_diff_message(message, 1000000)165def test_diff_message_diff_perceptualdiff():166 message = "The new screenshot 'C:\\...\\01_login_form__test_login.png' did not match the baseline "167 message += """'C:\\...\\login_form.png' (See C:\\...\\01_login_form__test_login.diff.png):168 FAIL: Images are visibly different169 15040 pixels are different170 """171 expected_message = 'Distance of 0.01504000'172 assert expected_message == VisualTest._get_diff_message(message, 1000000)173def test_diff_message_diff_imagemagick():174 message = ("The new screenshot 'C:\\...\\01_login_form__test_login.png' did not match the baseline "175 "'C:\\...\\login_form.png' (See C:\\...\\01_login_form__test_login.diff.png):\n"176 "5443.77 (0.0830666) @ 0,0")177 expected_message = 'Distance of 0.08306660'178 assert expected_message == VisualTest._get_diff_message(message, 1000000)179def assert_image(visual, img, img_name, expected_image_filename):180 """Save img in an image file and compare with the expected image181 :param img: image object182 :param img_name: temporary filename183 :param expected_image_filename: filename of the expected image184 """185 # Save result image in output folder186 result_file = os.path.join(visual.output_directory, img_name + '.png')187 img.save(result_file)188 # Output image and expected image must be equal189 expected_image = os.path.join(root_path, 'resources', expected_image_filename + '.png')190 compare_image_files(result_file, expected_image)191def compare_image_files(image, expected_image, threshold=0.1):192 """Compare two images193 :param image: file path image to compare194 :param expected_image: expected image195 :param threshold: allowed threshold196 """197 # Pil needs a pixel number threshold instead of a percentage threshold198 img = Image.open(expected_image)199 width, height = img.size200 threshold = int(width * height * threshold)201 PilEngine().assertSameFiles(image, expected_image, threshold)202def test_crop_element(driver_wrapper):203 # Create element mock204 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0205 web_element = get_mock_element(x=250, y=40, height=40, width=300)206 visual = VisualTest(driver_wrapper)207 # Resize image208 img = Image.open(file_v1)209 img = visual.crop_element(img, web_element)210 # Assert output image211 assert_image(visual, img, 'report_name', 'register_cropped_element')212def test_crop_element_oversize(driver_wrapper):213 # Create element mock214 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0215 web_element = get_mock_element(x=250, y=200, height=400, width=300)216 # y + width > img.size[1] --> 600 > 388217 visual = VisualTest(driver_wrapper)218 # Resize image219 img = Image.open(file_v1)220 img = visual.crop_element(img, web_element)221 # Assert output image222 assert_image(visual, img, 'report_name', 'register_cropped_element_oversize')223def test_get_scrolls_size(driver_wrapper):224 # Update conf and create a new VisualTest instance225 # Mock scrollHeight, scrollWidth, innerHeight, innerWidth226 driver_wrapper.driver.execute_script.side_effect = [600, 1200, 400, 900]227 driver_wrapper.config.set('Driver', 'type', 'chrome')228 visual = VisualTest(driver_wrapper)229 # Check scrolls230 assert visual.get_scrolls_size() == {'x': 17, 'y': 17}231def test_get_scrolls_size_y(driver_wrapper):232 # Update conf and create a new VisualTest instance233 # Mock scrollHeight, scrollWidth, innerHeight, innerWidth234 driver_wrapper.driver.execute_script.side_effect = [600, 1200, 400, 1200]235 driver_wrapper.config.set('Driver', 'type', 'chrome')236 visual = VisualTest(driver_wrapper)237 # Check scrolls238 assert visual.get_scrolls_size() == {'x': 0, 'y': 17}239def test_get_scrolls_size_without_scrolls(driver_wrapper):240 # Update conf and create a new VisualTest instance241 # Mock scrollHeight, scrollWidth, innerHeight, innerWidth242 driver_wrapper.driver.execute_script.side_effect = [600, 1200, 600, 1200]243 driver_wrapper.config.set('Driver', 'type', 'chrome')244 visual = VisualTest(driver_wrapper)245 # Check scrolls246 assert visual.get_scrolls_size() == {'x': 0, 'y': 0}247def test_get_scrolls_size_iexplore(driver_wrapper):248 # Update conf and create a new VisualTest instance249 # Mock scrollHeight, scrollWidth, innerHeight, innerWidth250 driver_wrapper.driver.execute_script.side_effect = [600, 1200, 400, 900]251 driver_wrapper.config.set('Driver', 'type', 'iexplore')252 visual = VisualTest(driver_wrapper)253 # Check scrolls254 assert visual.get_scrolls_size() == {'x': 21, 'y': 21}255def test_get_scrolls_size_firefox(driver_wrapper):256 # Update conf and create a new VisualTest instance257 driver_wrapper.config.set('Driver', 'type', 'firefox')258 visual = VisualTest(driver_wrapper)259 # Check scrolls260 assert visual.get_scrolls_size() == {'x': 0, 'y': 0}261def test_remove_scrolls(driver_wrapper):262 # Create a new VisualTest instance263 visual = VisualTest(driver_wrapper)264 visual.get_scrolls_size = lambda: {'x': 0, 'y': 17}265 # Remove scroll266 img = Image.open(file_scroll)267 img = visual.remove_scrolls(img)268 # Assert output image269 assert_image(visual, img, 'report_name', 'register_chrome_scroll_removed')270def test_remove_scrolls_without_scroll(driver_wrapper):271 # Create a new VisualTest instance272 visual = VisualTest(driver_wrapper)273 visual.get_scrolls_size = lambda: {'x': 0, 'y': 0}274 # Remove scroll275 img = Image.open(file_scroll)276 img = visual.remove_scrolls(img)277 # Assert output image278 assert_image(visual, img, 'report_name', 'register_chrome_scroll')279def test_mobile_resize(driver_wrapper):280 # Update conf and create a new VisualTest instance281 driver_wrapper.config.set('Driver', 'type', 'ios')282 driver_wrapper.utils.get_window_size.return_value = {'width': 375, 'height': 667}283 visual = VisualTest(driver_wrapper)284 # Resize image285 img = Image.open(file_ios)286 img = visual.mobile_resize(img)287 # Assert output image288 assert_image(visual, img, 'report_name', 'ios_resized')289def test_mobile_no_resize(driver_wrapper):290 # Update conf and create a new VisualTest instance291 driver_wrapper.config.set('Driver', 'type', 'ios')292 driver_wrapper.utils.get_window_size.return_value = {'width': 750, 'height': 1334}293 visual = VisualTest(driver_wrapper)294 # Resize image295 orig_img = Image.open(file_ios)296 img = visual.mobile_resize(orig_img)297 # Assert that image object has not been modified298 assert orig_img == img299def test_desktop_resize(driver_wrapper):300 # Update conf and create a new VisualTest instance301 driver_wrapper.is_mac_test = mock.MagicMock(return_value=True)302 driver_wrapper.utils.get_window_size.return_value = {'width': 1280, 'height': 1024}303 visual = VisualTest(driver_wrapper)304 # Resize image305 img = Image.open(file_mac)306 img = visual.desktop_resize(img)307 # Assert output image308 assert_image(visual, img, 'report_name', 'mac_os_retina_resized')309def test_desktop_no_resize(driver_wrapper):310 # Update conf and create a new VisualTest instance311 driver_wrapper.is_mac_test = mock.MagicMock(return_value=True)312 driver_wrapper.utils.get_window_size.return_value = {'width': 3840, 'height': 2102}313 visual = VisualTest(driver_wrapper)314 # Resize image315 orig_img = Image.open(file_ios)316 img = visual.mobile_resize(orig_img)317 # Assert that image object has not been modified318 assert orig_img == img319def test_exclude_elements(driver_wrapper):320 # Create elements mock321 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0322 visual = VisualTest(driver_wrapper)323 web_elements = [get_mock_element(x=250, y=40, height=40, width=300),324 get_mock_element(x=250, y=90, height=20, width=100)]325 img = Image.open(file_v1) # Exclude elements326 img = visual.exclude_elements(img, web_elements)327 # Assert output image328 assert_image(visual, img, 'report_name', 'register_exclude')329def test_exclude_element_outofimage(driver_wrapper):330 # Create elements mock331 visual = VisualTest(driver_wrapper)332 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0333 web_elements = [get_mock_element(x=250, y=40, height=40, width=1500)]334 img = Image.open(file_v1)335 # Exclude elements336 img = visual.exclude_elements(img, web_elements)337 # Assert output image338 assert_image(visual, img, 'report_name', 'register_exclude_outofimage')339def test_exclude_no_elements(driver_wrapper):340 # Exclude no elements341 visual = VisualTest(driver_wrapper)342 img = Image.open(file_v1)343 img = visual.exclude_elements(img, [])344 # Assert output image345 assert_image(visual, img, 'report_name', 'register')346def test_assert_screenshot_no_enabled_force(driver_wrapper):347 # Configure driver mock348 with open(file_v1, "rb") as f:349 image_data = f.read()350 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data351 # Update conf and create a new VisualTest instance352 driver_wrapper.config.set('VisualTests', 'enabled', 'false')353 visual = VisualTest(driver_wrapper, force=True)354 # Add v1 baseline image355 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')356 shutil.copyfile(file_v1, baseline_file)357 # Assert screenshot358 visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')359 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()360def test_assert_screenshot_no_enabled_force_fail(driver_wrapper):361 # Configure driver mock362 with open(file_v1, "rb") as f:363 image_data = f.read()364 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data365 # Update conf and create a new VisualTest instance366 driver_wrapper.config.set('VisualTests', 'fail', 'false')367 driver_wrapper.config.set('VisualTests', 'enabled', 'false')368 visual = VisualTest(driver_wrapper, force=True)369 # Add v2 baseline image370 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')371 shutil.copyfile(file_v2, baseline_file)372 # Assert screenshot373 with pytest.raises(AssertionError) as exc:374 visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')375 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()376 assert str(exc.value).endswith("did not match the baseline '%s' (by a distance of 522.65)" % baseline_file)377def test_assert_screenshot_full_and_save_baseline(driver_wrapper):378 # Configure driver mock379 with open(file_v1, "rb") as f:380 image_data = f.read()381 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data382 driver_wrapper.config.set('VisualTests', 'save', 'true')383 visual = VisualTest(driver_wrapper)384 # Assert screenshot385 visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')386 output_file = os.path.join(visual.output_directory, '01_screenshot_full__screenshot_suffix.png')387 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()388 # Output image and new baseline image must be equal389 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')390 compare_image_files(output_file, baseline_file)391def test_assert_screenshot_element_and_save_baseline(driver_wrapper):392 # Create element mock393 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0394 web_element = get_mock_element(x=250, y=40, height=40, width=300)395 # Configure driver mock396 with open(file_v1, "rb") as f:397 image_data = f.read()398 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data399 driver_wrapper.config.set('VisualTests', 'save', 'true')400 visual = VisualTest(driver_wrapper)401 # Assert screenshot402 visual.assert_screenshot(web_element, filename='screenshot_elem', file_suffix='screenshot_suffix')403 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()404 # Check cropped image405 expected_image = os.path.join(root_path, 'resources', 'register_cropped_element.png')406 output_file = os.path.join(visual.output_directory, '01_screenshot_elem__screenshot_suffix.png')407 compare_image_files(output_file, expected_image)408 # Output image and new baseline image must be equal409 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_elem.png')410 compare_image_files(output_file, baseline_file)411def test_assert_screenshot_full_and_compare(driver_wrapper):412 # Configure driver mock413 with open(file_v1, "rb") as f:414 image_data = f.read()415 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data416 visual = VisualTest(driver_wrapper)417 # Add baseline image418 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')419 shutil.copyfile(file_v1, baseline_file)420 # Assert screenshot421 visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')422 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()423def test_assert_screenshot_element_and_compare(driver_wrapper):424 # Add baseline image425 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0426 visual = VisualTest(driver_wrapper)427 expected_image = os.path.join(root_path, 'resources', 'register_cropped_element.png')428 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_elem.png')429 shutil.copyfile(expected_image, baseline_file)430 # Create element mock431 web_element = get_mock_element(x=250, y=40, height=40, width=300)432 # Configure driver mock433 with open(file_v1, "rb") as f:434 image_data = f.read()435 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data436 # Assert screenshot437 visual.assert_screenshot(web_element, filename='screenshot_elem', file_suffix='screenshot_suffix')438 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()439def test_assert_screenshot_full_without_baseline(driver_wrapper):440 # Configure driver mock441 with open(file_v1, "rb") as f:442 image_data = f.read()443 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data444 driver_wrapper.config.set('VisualTests', 'fail', 'true')445 visual = VisualTest(driver_wrapper)446 # Assert screenshot447 with pytest.raises(AssertionError) as exc:448 visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')449 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()450 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')451 assert str(exc.value) == 'Baseline file not found: %s' % baseline_file452def test_assert_screenshot_element_without_baseline(driver_wrapper):453 # Add baseline image454 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0455 driver_wrapper.config.set('VisualTests', 'fail', 'true')456 visual = VisualTest(driver_wrapper)457 # Create element mock458 web_element = get_mock_element(x=250, y=40, height=40, width=300)459 # Configure driver mock460 with open(file_v1, "rb") as f:461 image_data = f.read()462 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data463 # Assert screenshot464 with pytest.raises(AssertionError) as exc:465 visual.assert_screenshot(web_element, filename='screenshot_elem', file_suffix='screenshot_suffix')466 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()467 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_elem.png')468 assert str(exc.value) == 'Baseline file not found: %s' % baseline_file469def test_assert_screenshot_mobile_resize_and_exclude(driver_wrapper):470 # Create elements mock471 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0472 exclude_elements = [get_mock_element(x=0, y=0, height=24, width=375)]473 # Configure driver mock474 with open(file_ios, "rb") as f:475 image_data = f.read()476 driver_wrapper.utils.get_window_size.return_value = {'width': 375, 'height': 667}477 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data478 # Update conf and create a new VisualTest instance479 driver_wrapper.config.set('Driver', 'type', 'ios')480 driver_wrapper.config.set('VisualTests', 'save', 'true')481 visual = VisualTest(driver_wrapper)482 # Assert screenshot483 visual.assert_screenshot(None, filename='screenshot_ios', file_suffix='screenshot_suffix',484 exclude_elements=exclude_elements)485 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()486 # Check cropped image487 expected_image = os.path.join(root_path, 'resources', 'ios_excluded.png')488 output_file = os.path.join(visual.output_directory, '01_screenshot_ios__screenshot_suffix.png')489 compare_image_files(output_file, expected_image)490 # Output image and new baseline image must be equal491 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_ios.png')492 compare_image_files(output_file, baseline_file)493def test_assert_screenshot_mobile_web_resize_and_exclude(driver_wrapper):494 # Create elements mock495 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0496 form_element = get_mock_element(x=0, y=0, height=559, width=375)497 exclude_elements = [get_mock_element(x=15, y=296.515625, height=32, width=345)]498 # Configure driver mock499 file_ios_web = os.path.join(root_path, 'resources', 'ios_web.png')500 with open(file_ios_web, "rb") as f:501 image_data = f.read()502 driver_wrapper.utils.get_window_size.return_value = {'width': 375, 'height': 667}503 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data504 # Update conf and create a new VisualTest instance505 driver_wrapper.config.set('Driver', 'type', 'ios')506 driver_wrapper.config.set('AppiumCapabilities', 'browserName', 'safari')507 driver_wrapper.config.set('VisualTests', 'save', 'true')508 visual = VisualTest(driver_wrapper)509 # Assert screenshot510 visual.assert_screenshot(form_element, filename='screenshot_ios_web', file_suffix='screenshot_suffix',511 exclude_elements=exclude_elements)512 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()513 # Check cropped image514 expected_image = os.path.join(root_path, 'resources', 'ios_web_exclude.png')515 output_file = os.path.join(visual.output_directory, '01_screenshot_ios_web__screenshot_suffix.png')516 compare_image_files(output_file, expected_image)517 # Output image and new baseline image must be equal518 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_ios_web.png')519 compare_image_files(output_file, baseline_file)520def test_assert_screenshot_str_threshold(driver_wrapper):521 visual = VisualTest(driver_wrapper)522 with pytest.raises(TypeError) as exc:523 visual.assert_screenshot(None, 'screenshot_full', threshold='name')524 assert str(exc.value) == 'Threshold must be a number between 0 and 1: name'525def test_assert_screenshot_greater_threshold(driver_wrapper):526 visual = VisualTest(driver_wrapper)527 with pytest.raises(TypeError) as exc:528 visual.assert_screenshot(None, 'screenshot_full', threshold=2)...
visual_test.py
Source:visual_test.py
...190 """Resize image in Mac with Retina to fit window size191 :param img: image object192 :returns: modified image object193 """194 if self.driver_wrapper.is_mac_test():195 img = self.base_resize(img=img)196 return img197 def base_resize(self, img):198 """Base method for resize image199 :param img: image object200 :returns: modified image object201 """202 scale = img.size[0] / self.utils.get_window_size()['width']203 if scale != 1:204 new_image_size = (int(img.size[0] / scale), int(img.size[1] / scale))205 img = img.resize(new_image_size, Image.ANTIALIAS)206 return img207 def get_element_box(self, web_element):208 """Get element coordinates...
driver_wrapper.py
Source:driver_wrapper.py
...236 """Check if actual test must be executed in an iOS mobile237 :returns: True if test must be executed in an iOS mobile238 """239 return self.utils.get_driver_name() in ('ios', 'iphone')240 def is_mac_test(self):241 """Check if actual test must be executed in Mac desktop242 :returns: True if test must be executed in Mac desktop243 """244 return 'mac os' in self.get_driver_platform().lower()245 def is_mobile_test(self):246 """Check if actual test must be executed in a mobile247 :returns: True if test must be executed in a mobile248 """249 return self.is_android_test() or self.is_ios_test()250 def is_web_test(self):251 """Check if actual test must be executed in a browser252 :returns: True if test must be executed in a browser253 """254 appium_browser_name = self.config.get_optional('AppiumCapabilities', 'browserName')...
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!!