Exemplo n.º 1
0
 def test_distance_all_channels(self):
     diff = ImageDiff(self.get_white_image(), self.get_black_image())
     self.assertAlmostEqual(diff.get_distance(), 100 * 100, delta=0.001)
Exemplo n.º 2
0
 def test_nrmsd_one_channel(self):
     diff = ImageDiff(self.get_image((255, 0, 0)), self.get_black_image())
     self.assertEqual(diff.get_nrmsd(), math.sqrt(1.0 / 3))
Exemplo n.º 3
0
 def test_nrmsd_half_filled(self):
     diff = ImageDiff(self.get_black_image(), self.get_half_filled_image())
     self.assertEqual(diff.get_nrmsd(), math.sqrt(0.5))
Exemplo n.º 4
0
    def compareScreenshot(self, element_or_selector, filename, threshold=0.1):
        """
        Assert that a screenshot of an element is the same as a screenshot on disk,
        within a given threshold.

        :param element_or_selector:
            Either a CSS selector as a string or a
            :py:class:`~needle.driver.NeedleWebElement` object that represents
            the element to capture.
        :param name:
            A name for the screenshot, which will be appended with ``.png``.
        :param threshold:
            The threshold for triggering a test failure.
        """

        if not isinstance(filename, basestring):
            raise NotImplementedError

        baseline_file = os.path.join(self.baseline_directory,
                                     '%s.png' % filename)
        output_file = os.path.join(self.output_directory, '%s.png' % filename)

        if self.capture and os.path.exists(baseline_file):
            self.skipTest('Not capturing %s, image already exists. If you '
                          'want to capture this element again, delete %s' %
                          (filename, baseline_file))

        yield

        if not isinstance(element_or_selector, NeedleWebElement):
            element = self.driver.find_element_by_css_selector(
                element_or_selector)
        else:
            element = element_or_selector

        if self.capture:
            element.get_screenshot().save(baseline_file)
            return
        else:
            screenshot = element.get_screenshot()
            screenshot.save(output_file)

            if self.use_perceptualdiff:
                diff_filename = output_file.replace(".png", ".diff.ppm")
                try:
                    # BUG: figure out how best to convert threshold distances to pixel counts
                    subprocess.check_call([
                        "perceptualdiff", "-output", diff_filename,
                        baseline_file, output_file
                    ])
                except subprocess.CalledProcessError:
                    raise AssertionError(
                        "The saved screenshot for '%s' did not match "
                        "the screenshot captured: see %s" %
                        (filename, diff_filename))
            else:
                baseline_image = Image.open(baseline_file)

                diff = ImageDiff(screenshot, baseline_image)
                distance = abs(diff.get_distance())
                if distance > threshold:
                    raise AssertionError(
                        "The saved screenshot for '%s' did not match "
                        "the screenshot captured (by a distance of %.2f)" %
                        (filename, distance))