Exemplo n.º 1
0
def test_assert_screenshot_mobile_resize_and_exclude(driver_wrapper):
    # Create elements mock
    exclude_elements = [get_mock_element(x=0, y=0, height=24, width=375)]

    # Configure driver mock
    with open(file_ios, "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')
    visual = VisualTest(driver_wrapper)

    # Assert screenshot
    visual.assert_screenshot(None, filename='screenshot_ios', 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_excluded.png')
    output_file = os.path.join(visual.output_directory, '01_screenshot_ios__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(root_path, 'output', 'visualtests', 'baseline', 'firefox',
                                 'screenshot_ios.png')
    PilEngine().assertSameFiles(output_file, baseline_file, 0.1)
Exemplo n.º 2
0
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
    driver_wrapper.config.set('VisualTests', 'save', 'true')
    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')
    compare_image_files(output_file, expected_image)

    # Output image and new baseline image must be equal
    baseline_file = os.path.join(root_path, 'output', 'visualtests',
                                 'baseline', 'firefox', 'screenshot_elem.png')
    compare_image_files(output_file, baseline_file)
Exemplo n.º 3
0
def test_assert_screenshot_no_enabled_force_fail(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

    # Update conf and create a new VisualTest instance
    driver_wrapper.config.set('VisualTests', 'fail', 'false')
    driver_wrapper.config.set('VisualTests', 'enabled', 'false')
    visual = VisualTest(driver_wrapper, force=True)

    # Add v2 baseline image
    baseline_file = os.path.join(root_path, 'output', 'visualtests',
                                 'baseline', 'firefox', 'screenshot_full.png')
    shutil.copyfile(file_v2, baseline_file)

    # Assert screenshot
    with pytest.raises(AssertionError):
        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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
def test_assert_screenshot_no_enabled_force_fail(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

    # Update conf and create a new VisualTest instance
    driver_wrapper.config.set('VisualTests', 'fail', 'false')
    driver_wrapper.config.set('VisualTests', 'enabled', 'false')
    visual = VisualTest(driver_wrapper, force=True)

    # Add v2 baseline image
    baseline_file = os.path.join(root_path, 'output', 'visualtests',
                                 'baseline', 'firefox', 'screenshot_full.png')
    shutil.copyfile(file_v2, baseline_file)

    # Assert screenshot
    with pytest.raises(AssertionError) as exc:
        visual.assert_screenshot(None,
                                 filename='screenshot_full',
                                 file_suffix='screenshot_suffix')
    driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()
    assert str(exc.value).endswith(
        "did not match the baseline '%s' (by a distance of 522.65)" %
        baseline_file)
Exemplo n.º 6
0
def test_no_enabled(driver_wrapper):
    # Update conf and create a new VisualTest instance
    driver_wrapper.config.set('VisualTests', 'enabled', 'false')
    visual = VisualTest(driver_wrapper)

    visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')
    driver_wrapper.driver.save_screenshot.assert_not_called()
Exemplo n.º 7
0
def test_assert_screenshot_no_enabled_force(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

    # Update conf and create a new VisualTest instance
    driver_wrapper.config.set('VisualTests', 'enabled', 'false')
    visual = VisualTest(driver_wrapper, force=True)

    # Assert screenshot
    visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')
    driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()
Exemplo n.º 8
0
def test_assert_screenshot_full_and_compare(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)

    # Add baseline image
    baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')
    shutil.copyfile(file_v1, baseline_file)

    # Assert screenshot
    visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')
    driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()
Exemplo n.º 9
0
def test_assert_screenshot_no_enabled_force(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

    # Update conf and create a new VisualTest instance
    driver_wrapper.config.set('VisualTests', 'enabled', 'false')
    visual = VisualTest(driver_wrapper, force=True)

    # 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)
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
def test_assert_screenshot_full_and_compare(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)

    # Add baseline image
    baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox',
                                 'screenshot_full.png')
    shutil.copyfile(file_v1, baseline_file)

    # 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)
Exemplo n.º 12
0
def test_assert_screenshot_full_without_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
    driver_wrapper.config.set('VisualTests', 'fail', 'true')
    visual = VisualTest(driver_wrapper)

    # Assert screenshot
    with pytest.raises(AssertionError) as exc:
        visual.assert_screenshot(None,
                                 filename='screenshot_full',
                                 file_suffix='screenshot_suffix')
    driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()
    baseline_file = os.path.join(root_path, 'output', 'visualtests',
                                 'baseline', 'firefox', 'screenshot_full.png')
    assert str(exc.value) == 'Baseline file not found: %s' % baseline_file
Exemplo n.º 13
0
def test_assert_screenshot_no_enabled_force(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

    # Update conf and create a new VisualTest instance
    driver_wrapper.config.set('VisualTests', 'enabled', 'false')
    visual = VisualTest(driver_wrapper, force=True)

    # 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)
Exemplo n.º 14
0
def test_assert_screenshot_no_enabled_force_fail(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

    # Update conf and create a new VisualTest instance
    driver_wrapper.config.set('VisualTests', 'fail', 'false')
    driver_wrapper.config.set('VisualTests', 'enabled', 'false')
    visual = VisualTest(driver_wrapper, force=True)

    # Add v2 baseline image
    baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')
    shutil.copyfile(file_v2, baseline_file)

    # Assert screenshot
    with pytest.raises(AssertionError):
        visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')
    driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()
Exemplo n.º 15
0
def test_assert_screenshot_element_and_compare(driver_wrapper):
    # Add baseline image
    driver_wrapper.driver.execute_script.return_value = 0  # scrollX=0 and scrollY=0
    visual = VisualTest(driver_wrapper)
    expected_image = os.path.join(root_path, 'resources', 'register_cropped_element.png')
    baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_elem.png')
    shutil.copyfile(expected_image, baseline_file)

    # Create element mock
    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

    # 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()
Exemplo n.º 16
0
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)
Exemplo n.º 17
0
def test_assert_screenshot_full_and_compare(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)

    # Add baseline image
    baseline_file = os.path.join(root_path, 'output', 'visualtests',
                                 'baseline', 'firefox', 'screenshot_full.png')
    shutil.copyfile(file_v1, baseline_file)

    # 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)
Exemplo n.º 18
0
def test_assert_screenshot_no_enabled_force(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

    # Update conf and create a new VisualTest instance
    driver_wrapper.config.set('VisualTests', 'enabled', 'false')
    visual = VisualTest(driver_wrapper, force=True)

    # Add v1 baseline image
    baseline_file = os.path.join(root_path, 'output', 'visualtests',
                                 'baseline', 'firefox', 'screenshot_full.png')
    shutil.copyfile(file_v1, baseline_file)

    # Assert screenshot
    visual.assert_screenshot(None,
                             filename='screenshot_full',
                             file_suffix='screenshot_suffix')
    driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()
Exemplo n.º 19
0
def test_assert_screenshot_no_enabled_force_fail(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

    # Update conf and create a new VisualTest instance
    driver_wrapper.config.set('VisualTests', 'fail', 'false')
    driver_wrapper.config.set('VisualTests', 'enabled', 'false')
    visual = VisualTest(driver_wrapper, force=True)

    # Add v2 baseline image
    baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')
    shutil.copyfile(file_v2, baseline_file)

    # Assert screenshot
    with pytest.raises(AssertionError):
        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)
Exemplo n.º 20
0
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 equal
    baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_elem.png')
    MagickEngine().assertSameFiles(output_file, baseline_file, 0.1)
Exemplo n.º 21
0
def test_assert_screenshot_element_without_baseline(driver_wrapper):
    # Add baseline image
    driver_wrapper.driver.execute_script.return_value = 0  # scrollX=0 and scrollY=0
    driver_wrapper.config.set('VisualTests', 'fail', 'true')
    visual = VisualTest(driver_wrapper)

    # Create element mock
    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

    # Assert screenshot
    with pytest.raises(AssertionError) as exc:
        visual.assert_screenshot(web_element,
                                 filename='screenshot_elem',
                                 file_suffix='screenshot_suffix')
    driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()
    baseline_file = os.path.join(root_path, 'output', 'visualtests',
                                 'baseline', 'firefox', 'screenshot_elem.png')
    assert str(exc.value) == 'Baseline file not found: %s' % baseline_file
Exemplo n.º 22
0
def test_assert_screenshot_mobile_resize_and_exclude(driver_wrapper):
    # Create elements mock
    driver_wrapper.driver.execute_script.return_value = 0  # scrollX=0 and scrollY=0
    exclude_elements = [get_mock_element(x=0, y=0, height=24, width=375)]

    # Configure driver mock
    with open(file_ios, "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('VisualTests', 'save', 'true')
    visual = VisualTest(driver_wrapper)

    # Assert screenshot
    visual.assert_screenshot(None,
                             filename='screenshot_ios',
                             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_excluded.png')
    output_file = os.path.join(visual.output_directory,
                               '01_screenshot_ios__screenshot_suffix.png')
    compare_image_files(output_file, expected_image)

    # Output image and new baseline image must be equal
    baseline_file = os.path.join(root_path, 'output', 'visualtests',
                                 'baseline', 'firefox', 'screenshot_ios.png')
    compare_image_files(output_file, baseline_file)
Exemplo n.º 23
0
class VisualTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # Get file paths
        cls.root_path = os.path.dirname(os.path.realpath(__file__))
        cls.file_v1 = os.path.join(cls.root_path, 'resources', 'register.png')
        cls.file_v2 = os.path.join(cls.root_path, 'resources', 'register_v2.png')
        cls.file_small = os.path.join(cls.root_path, 'resources', 'register_small.png')
        cls.file_ios = os.path.join(cls.root_path, 'resources', 'ios.png')

    def setUp(self):
        # Remove previous visual path
        visual_path = os.path.join(self.root_path, 'output', 'visualtests')
        if os.path.exists(visual_path):
            shutil.rmtree(visual_path)

        # Reset wrappers pool values
        DriverWrappersPool._empty_pool()
        DriverWrapper.config_properties_filenames = None

        # Create a new wrapper
        self.driver_wrapper = DriverWrappersPool.get_default_wrapper()
        self.driver_wrapper.driver = mock.MagicMock()

        # Configure properties
        config_files = ConfigFiles()
        config_files.set_config_directory(os.path.join(self.root_path, 'conf'))
        config_files.set_config_properties_filenames('properties.cfg')
        config_files.set_output_directory(os.path.join(self.root_path, 'output'))
        self.driver_wrapper.configure(tc_config_files=config_files)
        self.driver_wrapper.config.set('VisualTests', 'enabled', 'true')

        # Create a new VisualTest instance
        self.visual = VisualTest(self.driver_wrapper)

    @classmethod
    def tearDownClass(cls):
        # Remove visual path
        visual_path = os.path.join(cls.root_path, 'output', 'visualtests')
        if os.path.exists(visual_path):
            shutil.rmtree(visual_path)

        # Reset wrappers pool values
        DriverWrappersPool._empty_pool()
        DriverWrapper.config_properties_filenames = None

    def test_no_enabled(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'enabled', 'false')
        self.visual = VisualTest(self.driver_wrapper)

        self.visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')
        self.driver_wrapper.driver.save_screenshot.assert_not_called()

    def test_engine_pil(self):
        assert_is_instance(self.visual.engine, PilEngine)

    def test_engine_perceptual(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'engine', 'perceptualdiff')
        self.visual = VisualTest(self.driver_wrapper)

        assert_is_instance(self.visual.engine, PerceptualEngine)

    # def test_engine_magick(self):
    #    self.driver_wrapper.config.set('VisualTests', 'engine', 'imagemagick')
    #    visual = VisualTest()
    #    assert_is_instance(visual.engine, MagickEngine)

    def test_engine_empty(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'engine', '')
        self.visual = VisualTest(self.driver_wrapper)

        assert_is_instance(self.visual.engine, PilEngine)

    def test_engine_unknown(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'engine', 'unknown')
        self.visual = VisualTest(self.driver_wrapper)

        assert_is_instance(self.visual.engine, PilEngine)

    def test_compare_files_equals(self):
        message = self.visual.compare_files(self._testMethodName, self.file_v1, self.file_v1, 0)
        assert_is_none(message)

    def test_compare_files_diff(self):
        message = self.visual.compare_files(self._testMethodName, self.file_v1, self.file_v2, 0)
        assert_in('by a distance of 522.65', message)

    def test_compare_files_diff_fail(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'fail', 'true')
        self.visual = VisualTest(self.driver_wrapper)

        with assert_raises(AssertionError):
            self.visual.compare_files(self._testMethodName, self.file_v1, self.file_v2, 0)

    def test_compare_files_size(self):
        message = self.visual.compare_files(self._testMethodName, self.file_v1, self.file_small, 0)
        # PIL returns an empty error
        assert_equal('', message)

    def test_compare_files_size_fail(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'fail', 'true')
        self.visual = VisualTest(self.driver_wrapper)

        with assert_raises(AssertionError):
            self.visual.compare_files(self._testMethodName, self.file_v1, self.file_small, 0)

    def test_get_html_row(self):
        expected_row = '<tr class=diff><td>test_get_html_row</td><td><img style="width: 100%" onclick=' \
                       '"launchModal\(this.src\)" src=".*register_v2.png"/></td></td><td><img style="width: 100%" ' \
                       'onclick="launchModal\(this.src\)" src=".*register.png"/></td></td><td></td></tr>'
        row = self.visual._get_html_row('diff', self._testMethodName, self.file_v1, self.file_v2)
        assertRegex(self, row, expected_row)

    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_crop_element(self):
        # Create element mock
        web_element = get_mock_element(x=250, y=40, height=40, width=300)

        # Resize image
        img = Image.open(self.file_v1)
        img = self.visual.crop_element(img, web_element)

        # Assert output image
        self.assert_image(img, self._testMethodName, 'register_cropped_element')

    def test_mobile_resize(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.driver.get_window_size.return_value = {'width': 375, 'height': 667}
        self.driver_wrapper.config.set('Driver', 'type', 'ios')
        self.visual = VisualTest(self.driver_wrapper)

        # Resize image
        img = Image.open(self.file_ios)
        img = self.visual.mobile_resize(img)

        # Assert output image
        self.assert_image(img, self._testMethodName, 'ios_resized')

    def test_mobile_no_resize(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.driver.get_window_size.return_value = {'width': 750, 'height': 1334}
        self.driver_wrapper.config.set('Driver', 'type', 'ios')
        self.visual = VisualTest(self.driver_wrapper)

        # Resize image
        orig_img = Image.open(self.file_ios)
        img = self.visual.mobile_resize(orig_img)

        # Assert that image object has not been modified
        assert_equal(orig_img, img)

    def test_exclude_elements(self):
        # Create elements mock
        web_elements = [get_mock_element(x=250, y=40, height=40, width=300),
                        get_mock_element(x=250, y=90, height=20, width=100)]
        img = Image.open(self.file_v1)

        # Exclude elements
        img = self.visual.exclude_elements(img, web_elements)

        # Assert output image
        self.assert_image(img, self._testMethodName, 'register_exclude')

    def test_exclude_element_outofimage(self):
        # Create elements mock
        web_elements = [get_mock_element(x=250, y=40, height=40, width=1500)]
        img = Image.open(self.file_v1)

        # Exclude elements
        img = self.visual.exclude_elements(img, web_elements)

        # Assert output image
        self.assert_image(img, self._testMethodName, 'register_exclude_outofimage')

    def test_exclude_no_elements(self):
        # Exclude no elements
        img = Image.open(self.file_v1)
        img = self.visual.exclude_elements(img, [])

        # Assert output image
        self.assert_image(img, self._testMethodName, 'register')

    def test_assert_screenshot_no_enabled_force(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

        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'enabled', 'false')
        self.visual = VisualTest(self.driver_wrapper, force=True)

        # 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)

    def test_assert_screenshot_no_enabled_force_fail(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

        # Add v2 baseline image
        baseline_file = os.path.join(self.root_path, 'output', 'visualtests', 'baseline', 'firefox',
                                     'screenshot_full.png')
        shutil.copyfile(self.file_v2, baseline_file)

        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'fail', 'false')
        self.driver_wrapper.config.set('VisualTests', 'enabled', 'false')
        self.visual = VisualTest(self.driver_wrapper, force=True)

        # Assert screenshot
        with assert_raises(AssertionError):
            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)

    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)

    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 test_assert_screenshot_full_and_compare(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

        # Add baseline image
        baseline_file = os.path.join(self.root_path, 'output', 'visualtests', 'baseline', 'firefox',
                                     'screenshot_full.png')
        shutil.copyfile(self.file_v1, baseline_file)

        # 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)

    def test_assert_screenshot_element_and_compare(self):
        # Add baseline image
        expected_image = os.path.join(self.root_path, 'resources', 'register_cropped_element.png')
        baseline_file = os.path.join(self.root_path, 'output', 'visualtests', 'baseline', 'firefox',
                                     'screenshot_elem.png')
        shutil.copyfile(expected_image, baseline_file)

        # 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()

    def test_assert_screenshot_mobile_resize_and_exclude(self):
        # Create elements mock
        exclude_elements = [get_mock_element(x=0, y=0, height=24, width=375)]

        # Configure driver mock
        with open(self.file_ios, "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.visual = VisualTest(self.driver_wrapper)

        # Assert screenshot
        self.visual.assert_screenshot(None, filename='screenshot_ios', 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_excluded.png')
        output_file = os.path.join(self.visual.output_directory, '01_screenshot_ios__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.png')
        PilEngine().assertSameFiles(output_file, baseline_file, 0.1)

    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 test_assert_screenshot_str_threshold(self):
        with assert_raises(TypeError):
            self.visual.assert_screenshot(None, 'screenshot_full', threshold='name')

    def test_assert_screenshot_greater_threshold(self):
        with assert_raises(TypeError):
            self.visual.assert_screenshot(None, 'screenshot_full', threshold=2)
Exemplo n.º 24
0
def test_assert_screenshot_greater_threshold(driver_wrapper):
    visual = VisualTest(driver_wrapper)
    with pytest.raises(TypeError):
        visual.assert_screenshot(None, 'screenshot_full', threshold=2)
Exemplo n.º 25
0
def test_assert_screenshot_greater_threshold(driver_wrapper):
    visual = VisualTest(driver_wrapper)
    with pytest.raises(TypeError) as exc:
        visual.assert_screenshot(None, 'screenshot_full', threshold=2)
    assert str(exc.value) == 'Threshold must be a number between 0 and 1: 2'
Exemplo n.º 26
0
class VisualTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # Get file paths
        cls.root_path = os.path.dirname(os.path.realpath(__file__))
        cls.file_v1 = os.path.join(cls.root_path, 'resources', 'register.png')
        cls.file_v2 = os.path.join(cls.root_path, 'resources',
                                   'register_v2.png')
        cls.file_small = os.path.join(cls.root_path, 'resources',
                                      'register_small.png')
        cls.file_ios = os.path.join(cls.root_path, 'resources', 'ios.png')

    def setUp(self):
        # Remove previous visual path
        visual_path = os.path.join(self.root_path, 'output', 'visualtests')
        if os.path.exists(visual_path):
            shutil.rmtree(visual_path)

        # Reset wrappers pool values
        DriverWrappersPool._empty_pool()
        DriverWrapper.config_properties_filenames = None

        # Create a new wrapper
        self.driver_wrapper = DriverWrappersPool.get_default_wrapper()
        self.driver_wrapper.driver = mock.MagicMock()

        # Configure properties
        config_files = ConfigFiles()
        config_files.set_config_directory(os.path.join(self.root_path, 'conf'))
        config_files.set_config_properties_filenames('properties.cfg')
        config_files.set_output_directory(
            os.path.join(self.root_path, 'output'))
        self.driver_wrapper.configure(tc_config_files=config_files)
        self.driver_wrapper.config.set('VisualTests', 'enabled', 'true')

        # Create a new VisualTest instance
        self.visual = VisualTest(self.driver_wrapper)

    @classmethod
    def tearDownClass(cls):
        # Remove visual path
        visual_path = os.path.join(cls.root_path, 'output', 'visualtests')
        if os.path.exists(visual_path):
            shutil.rmtree(visual_path)

        # Reset wrappers pool values
        DriverWrappersPool._empty_pool()
        DriverWrapper.config_properties_filenames = None

    def test_no_enabled(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'enabled', 'false')
        self.visual = VisualTest(self.driver_wrapper)

        self.visual.assert_screenshot(None,
                                      filename='screenshot_full',
                                      file_suffix='screenshot_suffix')
        self.driver_wrapper.driver.save_screenshot.assert_not_called()

    def test_engine_pil(self):
        assert_is_instance(self.visual.engine, PilEngine)

    def test_engine_perceptual(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'engine',
                                       'perceptualdiff')
        self.visual = VisualTest(self.driver_wrapper)

        assert_is_instance(self.visual.engine, PerceptualEngine)

    # def test_engine_magick(self):
    #    self.driver_wrapper.config.set('VisualTests', 'engine', 'imagemagick')
    #    visual = VisualTest()
    #    assert_is_instance(visual.engine, MagickEngine)

    def test_engine_empty(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'engine', '')
        self.visual = VisualTest(self.driver_wrapper)

        assert_is_instance(self.visual.engine, PilEngine)

    def test_engine_unknown(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'engine', 'unknown')
        self.visual = VisualTest(self.driver_wrapper)

        assert_is_instance(self.visual.engine, PilEngine)

    def test_compare_files_equals(self):
        message = self.visual.compare_files(self._testMethodName, self.file_v1,
                                            self.file_v1, 0)
        assert_is_none(message)

    def test_compare_files_diff(self):
        message = self.visual.compare_files(self._testMethodName, self.file_v1,
                                            self.file_v2, 0)
        assert_in('by a distance of 522.65', message)

    def test_compare_files_diff_fail(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'fail', 'true')
        self.visual = VisualTest(self.driver_wrapper)

        with assert_raises(AssertionError):
            self.visual.compare_files(self._testMethodName, self.file_v1,
                                      self.file_v2, 0)

    def test_compare_files_size(self):
        message = self.visual.compare_files(self._testMethodName, self.file_v1,
                                            self.file_small, 0)
        # PIL returns an empty error
        assert_equal('', message)

    def test_compare_files_size_fail(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'fail', 'true')
        self.visual = VisualTest(self.driver_wrapper)

        with assert_raises(AssertionError):
            self.visual.compare_files(self._testMethodName, self.file_v1,
                                      self.file_small, 0)

    def test_get_html_row(self):
        expected_row = '<tr class=diff><td>test_get_html_row</td><td><img style="width: 100%" onclick=' \
                       '"launchModal\(this.src\)" src=".*register_v2.png"/></td></td><td><img style="width: 100%" ' \
                       'onclick="launchModal\(this.src\)" src=".*register.png"/></td></td><td></td></tr>'
        row = self.visual._get_html_row('diff', self._testMethodName,
                                        self.file_v1, self.file_v2)
        assertRegex(self, row, expected_row)

    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_crop_element(self):
        # Create element mock
        web_element = get_mock_element(x=250, y=40, height=40, width=300)

        # Resize image
        img = Image.open(self.file_v1)
        img = self.visual.crop_element(img, web_element)

        # Assert output image
        self.assert_image(img, self._testMethodName,
                          'register_cropped_element')

    def test_mobile_resize(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.driver.get_window_size.return_value = {
            'width': 375,
            'height': 667
        }
        self.driver_wrapper.config.set('Driver', 'type', 'ios')
        self.visual = VisualTest(self.driver_wrapper)

        # Resize image
        img = Image.open(self.file_ios)
        img = self.visual.mobile_resize(img)

        # Assert output image
        self.assert_image(img, self._testMethodName, 'ios_resized')

    def test_mobile_no_resize(self):
        # Update conf and create a new VisualTest instance
        self.driver_wrapper.driver.get_window_size.return_value = {
            'width': 750,
            'height': 1334
        }
        self.driver_wrapper.config.set('Driver', 'type', 'ios')
        self.visual = VisualTest(self.driver_wrapper)

        # Resize image
        orig_img = Image.open(self.file_ios)
        img = self.visual.mobile_resize(orig_img)

        # Assert that image object has not been modified
        assert_equal(orig_img, img)

    def test_exclude_elements(self):
        # Create elements mock
        web_elements = [
            get_mock_element(x=250, y=40, height=40, width=300),
            get_mock_element(x=250, y=90, height=20, width=100)
        ]
        img = Image.open(self.file_v1)

        # Exclude elements
        img = self.visual.exclude_elements(img, web_elements)

        # Assert output image
        self.assert_image(img, self._testMethodName, 'register_exclude')

    def test_exclude_element_outofimage(self):
        # Create elements mock
        web_elements = [get_mock_element(x=250, y=40, height=40, width=1500)]
        img = Image.open(self.file_v1)

        # Exclude elements
        img = self.visual.exclude_elements(img, web_elements)

        # Assert output image
        self.assert_image(img, self._testMethodName,
                          'register_exclude_outofimage')

    def test_exclude_no_elements(self):
        # Exclude no elements
        img = Image.open(self.file_v1)
        img = self.visual.exclude_elements(img, [])

        # Assert output image
        self.assert_image(img, self._testMethodName, 'register')

    def test_assert_screenshot_no_enabled_force(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

        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'enabled', 'false')
        self.visual = VisualTest(self.driver_wrapper, force=True)

        # 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)

    def test_assert_screenshot_no_enabled_force_fail(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

        # Add v2 baseline image
        baseline_file = os.path.join(self.root_path, 'output', 'visualtests',
                                     'baseline', 'firefox',
                                     'screenshot_full.png')
        shutil.copyfile(self.file_v2, baseline_file)

        # Update conf and create a new VisualTest instance
        self.driver_wrapper.config.set('VisualTests', 'fail', 'false')
        self.driver_wrapper.config.set('VisualTests', 'enabled', 'false')
        self.visual = VisualTest(self.driver_wrapper, force=True)

        # Assert screenshot
        with assert_raises(AssertionError):
            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)

    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)

    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 test_assert_screenshot_full_and_compare(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

        # Add baseline image
        baseline_file = os.path.join(self.root_path, 'output', 'visualtests',
                                     'baseline', 'firefox',
                                     'screenshot_full.png')
        shutil.copyfile(self.file_v1, baseline_file)

        # 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)

    def test_assert_screenshot_element_and_compare(self):
        # Add baseline image
        expected_image = os.path.join(self.root_path, 'resources',
                                      'register_cropped_element.png')
        baseline_file = os.path.join(self.root_path, 'output', 'visualtests',
                                     'baseline', 'firefox',
                                     'screenshot_elem.png')
        shutil.copyfile(expected_image, baseline_file)

        # 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(
        )

    def test_assert_screenshot_mobile_resize_and_exclude(self):
        # Create elements mock
        exclude_elements = [get_mock_element(x=0, y=0, height=24, width=375)]

        # Configure driver mock
        with open(self.file_ios, "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.visual = VisualTest(self.driver_wrapper)

        # Assert screenshot
        self.visual.assert_screenshot(None,
                                      filename='screenshot_ios',
                                      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_excluded.png')
        output_file = os.path.join(self.visual.output_directory,
                                   '01_screenshot_ios__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.png')
        PilEngine().assertSameFiles(output_file, baseline_file, 0.1)

    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 test_assert_screenshot_str_threshold(self):
        with assert_raises(TypeError):
            self.visual.assert_screenshot(None,
                                          'screenshot_full',
                                          threshold='name')

    def test_assert_screenshot_greater_threshold(self):
        with assert_raises(TypeError):
            self.visual.assert_screenshot(None, 'screenshot_full', threshold=2)