def test_assert_screenshot_element_and_save_baseline(self): # Create element mock web_element = get_mock_element(x=250, y=40, height=40, width=300) # Configure driver mock with open(self.file_v1, "rb") as f: image_data = f.read() self.driver_wrapper.driver.get_screenshot_as_png.return_value = image_data # Assert screenshot self.visual.assert_screenshot(web_element, filename='screenshot_elem', file_suffix='screenshot_suffix') self.driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with( ) # Check cropped image expected_image = os.path.join(self.root_path, 'resources', 'register_cropped_element.png') output_file = os.path.join( self.visual.output_directory, '01_screenshot_elem__screenshot_suffix.png') PilEngine().assertSameFiles(output_file, expected_image, 0.1) # Output image and new baseline image must be equals baseline_file = os.path.join(self.root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_elem.png') PilEngine().assertSameFiles(output_file, baseline_file, 0.1)
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.output_directory, 'visualtests', 'baseline', 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': # self.engine = MagickEngine() elif engine_type == 'pil': self.engine = PilEngine() else: self.logger.warn( "Engine '{}' not found, using pil instead. Review your properties.cfg file." .format(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()
def test_assert_screenshot_mobile_web_resize_and_exclude(self): # Create elements mock 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(self.root_path, 'resources', 'ios_web.png') with open(file_ios_web, "rb") as f: image_data = f.read() self.driver_wrapper.driver.get_screenshot_as_png.return_value = image_data self.driver_wrapper.driver.get_window_size.return_value = { 'width': 375, 'height': 667 } # Update conf and create a new VisualTest instance self.driver_wrapper.config.set('Driver', 'type', 'ios') self.driver_wrapper.config.set('AppiumCapabilities', 'browserName', 'safari') self.visual = VisualTest(self.driver_wrapper) # Assert screenshot self.visual.assert_screenshot(form_element, filename='screenshot_ios_web', file_suffix='screenshot_suffix', exclude_elements=exclude_elements) self.driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with( ) # Check cropped image expected_image = os.path.join(self.root_path, 'resources', 'ios_web_exclude.png') output_file = os.path.join( self.visual.output_directory, '01_screenshot_ios_web__screenshot_suffix.png') PilEngine().assertSameFiles(output_file, expected_image, 0.1) # Output image and new baseline image must be equals baseline_file = os.path.join(self.root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_ios_web.png') PilEngine().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 compare_image_files(image, expected_image, threshold=0.1): """Compare two images :param image: file path image to compare :param expected_image: expected image :param threshold: allowed threshold """ # Pil needs a pixel number threshold instead of a percentage threshold img = Image.open(expected_image) width, height = img.size threshold = int(width * height * threshold) PilEngine().assertSameFiles(image, expected_image, threshold)
def assert_image(self, 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(self.visual.output_directory, img_name + '.png') img.save(result_file) # Output image and expected image must be equals expected_image = os.path.join(self.root_path, 'resources', expected_image_filename + '.png') PilEngine().assertSameFiles(result_file, expected_image, 0.1)
def test_assert_screenshot_full_and_save_baseline(self): # Configure driver mock def copy_file_side_effect(output_file): shutil.copyfile(self.file_v1, output_file) self.driver_wrapper.driver.save_screenshot.side_effect = copy_file_side_effect # Assert screenshot self.visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix') output_file = os.path.join( self.visual.output_directory, '01_screenshot_full__screenshot_suffix.png') self.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(self.root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png') PilEngine().assertSameFiles(output_file, baseline_file, 0.1)