def assert_screenshot(self, screenshot_id=None, threshold=100, crop_threshold=100): assert_dir = self.global_settings.get('assert_screenshots_dir') screenshot_id += '.png' ok_file = os.path.join(assert_dir, screenshot_id) if not os.path.isfile(ok_file): if self.global_settings.get('assert_screenshots_learning'): self.log.d('Learning screenshot for %r' % screenshot_id) self.browser.save_screenshot(ok_file) else: raise LookupError('No reference screenshot file %r for ' 'screenshot id:%r' % (ok_file, screenshot_id)) else: if not self._tmp_screenshots_dir: self._tmp_screenshots_dir = tempfile.mkdtemp() test_file = os.path.join(self._tmp_screenshots_dir, screenshot_id) self.browser.save_screenshot(test_file) assert os.path.isfile(test_file), ( 'The screenshot %r was not saved' % screenshot_id) comp = ImagesComparator() equal = comp.compare(ok_file, test_file, threshold) if not equal: self._save_failed_screenshot(ok_file, test_file, screenshot_id, crop_threshold) self.assert_( equal, 'Reference(%r) != Screenshot(%r), threshold=%s' % (ok_file, test_file, threshold))
def test_comparator(self): ic = ImagesComparator() base = os.path.join(os.path.dirname(__file__), 'img') a_file = os.path.join(base, 'street.jpg') b_file = os.path.join(base, 'street_diff.jpg') diff = os.path.join(base, 'diff.jpg') self.assertFalse(ic.compare(a_file, b_file, treshold=100)) self.assertTrue(ic.compare(a_file, b_file, treshold=50)) ic.create_diff(a_file, b_file, diff, crop_threshold=100) os.remove(diff)
def _save_failed_screenshot(self, ok_file, test_file, screenshot_id, crop_threshold): save_dir = self.global_settings.get('assert_screenshots_failed_dir') assert os.path.isdir(save_dir), 'Inexistent directory %r' % save_dir failed = os.path.join(save_dir, 'failed_' + screenshot_id) # Create the diff diff = os.path.join(save_dir, 'failed_diff_' + screenshot_id) ImagesComparator().create_diff(ok_file, test_file, diff, crop_threshold) # Move the failing image shutil.move(test_file, failed) # Return the path to intercept them in reports return failed, diff
def assert_screenshot(self, screenshot_id=None, threshold=100, crop_threshold=100): assert_dir = self.global_settings.get('assert_screenshots_dir') screenshot_id += '.png' ok_file = os.path.join(assert_dir, screenshot_id) if not os.path.isfile(ok_file): if self.global_settings.get('assert_screenshots_learning'): self.log.d('Learning screenshot for %r' % screenshot_id) self.browser.save_screenshot(ok_file) else: raise LookupError('No reference screenshot file %r for ' 'screenshot id:%r' % (ok_file, screenshot_id)) else: if not self._tmp_screenshots_dir: self._tmp_screenshots_dir = tempfile.mkdtemp() test_file = os.path.join(self._tmp_screenshots_dir, screenshot_id) self.browser.save_screenshot(test_file) assert os.path.isfile(test_file), ('The screenshot %r was not saved' % screenshot_id) comp = ImagesComparator() equal = comp.compare(ok_file, test_file, threshold) if not equal: self._save_failed_screenshot(ok_file, test_file, screenshot_id, crop_threshold) self.assert_(equal, 'Reference(%r) != Screenshot(%r), threshold=%s' % (ok_file, test_file, threshold))