Best Python code snippet using toolium_python
test_visual_test.py
Source:test_visual_test.py
...295 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 mock...
visual_test.py
Source:visual_test.py
...128 # Get screenshot and modify it129 img = Image.open(BytesIO(self.driver_wrapper.driver.get_screenshot_as_png()))130 img = self.remove_scrolls(img)131 img = self.mobile_resize(img)132 img = self.desktop_resize(img)133 img = self.exclude_elements(img, exclude_web_elements)134 img = self.crop_element(img, web_element)135 img.save(output_file)136 DriverWrappersPool.visual_number += 1137 # Determine whether we should save the baseline image138 if self.save_baseline:139 # Copy screenshot to baseline140 shutil.copyfile(output_file, baseline_file)141 if self.driver_wrapper.config.getboolean_optional('VisualTests', 'complete_report'):142 self._add_result_to_report('baseline', report_name, output_file, None, 'Screenshot added to baseline')143 self.logger.debug("Visual screenshot '%s' saved in visualtests/baseline folder", filename)144 elif not os.path.exists(baseline_file):145 # Baseline should exist if save mode is not enabled146 error_message = "Baseline file not found: %s" % baseline_file147 self.logger.warning(error_message)148 self._add_result_to_report('diff', report_name, output_file, None, 'Baseline file not found')149 if self.driver_wrapper.config.getboolean_optional('VisualTests', 'fail') or self.force:150 raise AssertionError(error_message)151 else:152 # Compare the screenshots153 self.compare_files(report_name, output_file, baseline_file, threshold)154 def get_scrolls_size(self):155 """Return Chrome and Explorer scrolls sizes if they are visible156 Firefox screenshots don't contain scrolls157 :returns: dict with horizontal and vertical scrolls sizes158 """159 scroll_x = 0160 scroll_y = 0161 if self.utils.get_driver_name() in ['chrome', 'iexplore'] and not self.driver_wrapper.is_mobile_test():162 scroll_height = self.driver_wrapper.driver.execute_script("return document.body.scrollHeight")163 scroll_width = self.driver_wrapper.driver.execute_script("return document.body.scrollWidth")164 window_height = self.driver_wrapper.driver.execute_script("return window.innerHeight")165 window_width = self.driver_wrapper.driver.execute_script("return window.innerWidth")166 scroll_size = 21 if self.utils.get_driver_name() == 'iexplore' else 17167 scroll_x = scroll_size if scroll_width > window_width else 0168 scroll_y = scroll_size if scroll_height > window_height else 0169 return {'x': scroll_x, 'y': scroll_y}170 def remove_scrolls(self, img):171 """Remove browser scrolls from image if they are visible172 :param img: image object173 :returns: modified image object174 """175 scrolls_size = self.get_scrolls_size()176 if scrolls_size['x'] > 0 or scrolls_size['y'] > 0:177 new_image_width = img.size[0] - scrolls_size['y']178 new_image_height = img.size[1] - scrolls_size['x']179 img = img.crop((0, 0, new_image_width, new_image_height))180 return img181 def mobile_resize(self, img):182 """Resize image in iOS (native and web) and Android (web) to fit window size183 :param img: image object184 :returns: modified image object185 """186 if self.driver_wrapper.is_ios_test() or self.driver_wrapper.is_android_web_test():187 img = self.base_resize(img=img)188 return img189 def desktop_resize(self, img):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:...
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!!