def test_assert_screenshot_element_and_save_baseline(driver_wrapper): # Create element mock driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0 web_element = get_mock_element(x=250, y=40, height=40, width=300) # Configure driver mock with open(file_v1, "rb") as f: image_data = f.read() driver_wrapper.driver.get_screenshot_as_png.return_value = image_data visual = VisualTest(driver_wrapper) # Assert screenshot visual.assert_screenshot(web_element, filename='screenshot_elem', file_suffix='screenshot_suffix') driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with() # Check cropped image expected_image = os.path.join(root_path, 'resources', 'register_cropped_element.png') output_file = os.path.join(visual.output_directory, '01_screenshot_elem__screenshot_suffix.png') MagickEngine().assertSameFiles(output_file, expected_image, 0.1) # Output image and new baseline image must be equals baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_elem.png') MagickEngine().assertSameFiles(output_file, baseline_file, 0.1)
def test_assert_screenshot_mobile_web_resize_and_exclude(driver_wrapper): # Create elements mock driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0 form_element = get_mock_element(x=0, y=0, height=559, width=375) exclude_elements = [get_mock_element(x=15, y=296.515625, height=32, width=345)] # Configure driver mock file_ios_web = os.path.join(root_path, 'resources', 'ios_web.png') with open(file_ios_web, "rb") as f: image_data = f.read() driver_wrapper.driver.get_screenshot_as_png.return_value = image_data driver_wrapper.driver.get_window_size.return_value = {'width': 375, 'height': 667} # Update conf and create a new VisualTest instance driver_wrapper.config.set('Driver', 'type', 'ios') driver_wrapper.config.set('AppiumCapabilities', 'browserName', 'safari') visual = VisualTest(driver_wrapper) # Assert screenshot visual.assert_screenshot(form_element, filename='screenshot_ios_web', file_suffix='screenshot_suffix', exclude_elements=exclude_elements) driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with() # Check cropped image expected_image = os.path.join(root_path, 'resources', 'ios_web_exclude.png') output_file = os.path.join(visual.output_directory, '01_screenshot_ios_web__screenshot_suffix.png') MagickEngine().assertSameFiles(output_file, expected_image, 0.1) # Output image and new baseline image must be equal baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_ios_web.png') MagickEngine().assertSameFiles(output_file, baseline_file, 0.1)
def assert_image(visual, img, img_name, expected_image_filename): """Save img in an image file and compare with the expected image :param img: image object :param img_name: temporary filename :param expected_image_filename: filename of the expected image """ # Save result image in output folder result_file = os.path.join(visual.output_directory, img_name + '.png') img.save(result_file) # Output image and expected image must be equal expected_image = os.path.join(root_path, 'resources', expected_image_filename + '.png') MagickEngine().assertSameFiles(result_file, expected_image, 0.1)
def test_assert_screenshot_full_and_save_baseline(driver_wrapper): # Configure driver mock with open(file_v1, "rb") as f: image_data = f.read() driver_wrapper.driver.get_screenshot_as_png.return_value = image_data visual = VisualTest(driver_wrapper) # Assert screenshot visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix') output_file = os.path.join(visual.output_directory, '01_screenshot_full__screenshot_suffix.png') driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with() # Output image and new baseline image must be equal baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png') MagickEngine().assertSameFiles(output_file, baseline_file, 0.1)
def test_assert_screenshot_full_and_save_baseline(driver_wrapper): # Configure driver mock def copy_file_side_effect(output_file): shutil.copyfile(file_v1, output_file) driver_wrapper.driver.save_screenshot.side_effect = copy_file_side_effect visual = VisualTest(driver_wrapper) # Assert screenshot visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix') output_file = os.path.join(visual.output_directory, '01_screenshot_full__screenshot_suffix.png') driver_wrapper.driver.save_screenshot.assert_called_once_with(output_file) # Output image and new baseline image must be equals baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png') MagickEngine().assertSameFiles(output_file, baseline_file, 0.1)
def _get_engine(self): """Get configured or default visual engine :returns: engine instance """ engine_type = self.driver_wrapper.config.get_optional('VisualTests', 'engine', 'pil') if engine_type == 'perceptualdiff': engine = PerceptualEngine() elif engine_type == 'imagemagick' and 'MagickEngine' not in globals(): self.logger.warning("Engine '%s' not found, using pil instead. You need needle 0.4+ to use this engine.", engine_type) engine = PilEngine() elif engine_type == 'imagemagick': engine = MagickEngine() elif engine_type == 'pil': engine = PilEngine() else: self.logger.warning("Engine '%s' not found, using pil instead. Review your properties.cfg file.", engine_type) engine = PilEngine() return engine
def __init__(self, driver_wrapper=None, force=False): self.driver_wrapper = driver_wrapper if driver_wrapper else DriverWrappersPool.get_default_wrapper( ) self.force = force if not self.driver_wrapper.config.getboolean_optional( 'VisualTests', 'enabled') and not self.force: return if 'PerceptualEngine' not in globals(): raise Exception( 'The visual tests are enabled, but needle is not installed') self.utils = self.driver_wrapper.utils self.logger = logging.getLogger(__name__) self.output_directory = DriverWrappersPool.visual_output_directory # Update baseline with real platformVersion value if '{platformVersion}' in self.driver_wrapper.baseline_name: platform_version = self.driver_wrapper.driver.desired_capabilities[ 'platformVersion'] baseline_name = self.driver_wrapper.baseline_name.replace( '{platformVersion}', platform_version) self.driver_wrapper.baseline_name = baseline_name self.driver_wrapper.visual_baseline_directory = os.path.join( DriverWrappersPool.visual_baseline_directory, baseline_name) self.baseline_directory = self.driver_wrapper.visual_baseline_directory engine_type = self.driver_wrapper.config.get_optional( 'VisualTests', 'engine', 'pil') if engine_type == 'perceptualdiff': self.engine = PerceptualEngine() elif engine_type == 'imagemagick' and 'MagickEngine' not in globals(): self.logger.warning( "Engine '%s' not found, using pil instead. You need needle 0.4+ to use this engine.", engine_type) self.engine = PilEngine() elif engine_type == 'imagemagick': self.engine = MagickEngine() elif engine_type == 'pil': self.engine = PilEngine() else: self.logger.warning( "Engine '%s' not found, using pil instead. Review your properties.cfg file.", engine_type) self.engine = PilEngine() self.save_baseline = self.driver_wrapper.config.getboolean_optional( 'VisualTests', 'save') # Create folders if not os.path.exists(self.baseline_directory): os.makedirs(self.baseline_directory) if not os.path.exists(self.output_directory): os.makedirs(self.output_directory) # Copy html template to output directory orig_template_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'resources', self.template_name) dst_template_path = os.path.join(self.output_directory, self.report_name) if not os.path.exists(dst_template_path): shutil.copyfile(orig_template_path, dst_template_path) self._add_summary_to_report()