def assertSameImage(img_expected, img_actual,image_test_threshold=0.05, error_msg='same image is expected but diff image found'): ''' exception if different image ''' img_filename = os.path.basename(img_actual) print("debug: diff result {}".format(diff(img_expected, img_actual))) print("debug: image_test_threshold {}".format(image_test_threshold)) print("debug: diff assert diff result {}, {}".format(diff(img_expected, img_actual) < image_test_threshold), img_filename) assert diff(img_expected, img_actual) < image_test_threshold, error_msg + "debug: diff result {} for file {}".format(diff(img_expected, img_actual))
def test_make_png_diff_img(self): # these images are the same, but it shouldn't matter for this test diff(IMG4, IMG4, diff_img_file=TEST_PNG_OUT) self.assertTrue(os.path.exists(TEST_PNG_OUT)) os.remove(TEST_PNG_OUT)
def compare_to_baselines(self, num_of_screenshots=10): out_dir = self._get_output_dir('out') baselines_dir = self._get_output_dir('baselines') diff_dir = self._get_output_dir('diff') has_failed = False for i in range(num_of_screenshots): screenshot_filename = '{}{}.png'.format(self._test_name, i + 1) out_img = os.path.join(out_dir, screenshot_filename) baseline_img = os.path.join(baselines_dir, screenshot_filename) diff_img = os.path.join(diff_dir, screenshot_filename) diff_percent = (diff(baseline_img, out_img, delete_diff_file=True, ignore_alpha=True) * 100) print('{}% difference in {} for {}'.format( diff_percent, self._get_browser_name(), screenshot_filename)) if diff_percent > UITester._SCREENSHOT_DIFF_THRESHOLD_PERCENT: # If an image comparison has failed, generate diff and print an error message in red has_failed = True diff( out_img, baseline_img, delete_diff_file=False, diff_img_file=diff_img, ignore_alpha=True, ) print( '\033[91mScreenshot comparison failed in {} for {} by {}%\033[0m' .format(self._get_browser_name(), screenshot_filename, diff_percent)) assert not has_failed
def test_delete_diff_img(self): if os.path.exists(TEST_JPG_OUT): os.remove(TEST_JPG_OUT) diff(IMG1, IMG2, delete_diff_file=True, diff_img_file=TEST_JPG_OUT) self.assertFalse(os.path.exists(TEST_JPG_OUT))
def test_upload_cover_hdd(self): self.fill_basic_config({'config_uploading': 1}) time.sleep(3) self.assertTrue(self.check_element_on_page((By.ID, 'flash_success'))) self.edit_user('admin', {'upload_role': 1}) self.get_book_details(5) original = self.check_element_on_page( (By.ID, "detailcover")).screenshot_as_png self.check_element_on_page((By.ID, "edit_book")).click() jpegcover = os.path.join(base_path, 'files', 'cover.jpg') self.edit_book(content={'local_cover': jpegcover}) time.sleep(5) self.get_book_details(5) time.sleep(5) jpeg = self.check_element_on_page( (By.ID, "detailcover")).screenshot_as_png self.assertGreater( diff(BytesIO(original), BytesIO(jpeg), delete_diff_file=True), 0.02) self.check_element_on_page((By.ID, "edit_book")).click() bmpcover = os.path.join(base_path, 'files', 'cover.bmp') self.edit_book(content={'local_cover': bmpcover}) self.assertFalse(self.check_element_on_page((By.CLASS_NAME, "alert"))) time.sleep(5) bmp = self.check_element_on_page( (By.ID, "detailcover")).screenshot_as_png self.assertGreater( diff(BytesIO(bmp), BytesIO(jpeg), delete_diff_file=True), 0.006) self.get_book_details(5) time.sleep(5) self.check_element_on_page((By.ID, "edit_book")).click() pngcover = os.path.join(base_path, 'files', 'cover.png') self.edit_book(content={'local_cover': pngcover}) time.sleep(5) self.get_book_details(5) time.sleep(5) png = self.check_element_on_page( (By.ID, "detailcover")).screenshot_as_png self.assertGreater( diff(BytesIO(bmp), BytesIO(png), delete_diff_file=True), 0.005) self.check_element_on_page((By.ID, "edit_book")).click() pngcover = os.path.join(base_path, 'files', 'cover.webp') self.edit_book(content={'local_cover': pngcover}) time.sleep(5) self.get_book_details(5) time.sleep(5) webp = self.check_element_on_page( (By.ID, "detailcover")).screenshot_as_png self.assertGreater( diff(BytesIO(webp), BytesIO(png), delete_diff_file=True), 0.005) self.fill_basic_config({'config_uploading': 0}) time.sleep(2)
def test_upload_cbz_coverformats(self): self.fill_basic_config({'config_uploading': 1}) time.sleep(3) self.assertTrue(self.check_element_on_page((By.ID, "flash_success"))) # Upload book to be sure we have a reference (delete book test runs maybe before this test) upload_file = os.path.join(base_path, 'files', 'book.fb2') self.goto_page('nav_new') upload = self.check_element_on_page((By.ID, 'btn-upload')) upload.send_keys(upload_file) time.sleep(2) original_cover = self.check_element_on_page((By.ID, "detailcover")).screenshot_as_png self.delete_book(-1) upload_file = os.path.join(base_path, 'files', 'book1.cbz') # upload webp book zipdata = [os.path.join(base_path, 'files', 'cover.webp')] names = ['cover1.weBp'] createcbz(upload_file, zipdata, names) self.goto_page('nav_new') upload = self.check_element_on_page((By.ID, 'btn-upload')) upload.send_keys(upload_file) time.sleep(2) cover = self.check_element_on_page((By.ID, "detailcover")).screenshot_as_png self.assertGreaterEqual(diff(BytesIO(cover), BytesIO(original_cover), delete_diff_file=True), 0.05) self.delete_book(-1) # upload png book zipdata = [os.path.join(base_path, 'files', 'cover.png')] names = ['cover10.png'] createcbz(upload_file, zipdata, names) self.goto_page('nav_new') upload = self.check_element_on_page((By.ID, 'btn-upload')) upload.send_keys(upload_file) time.sleep(2) cover = self.check_element_on_page((By.ID, "detailcover")).screenshot_as_png self.assertGreaterEqual(diff(BytesIO(cover), BytesIO(original_cover), delete_diff_file=True), 0.05) self.delete_book(-1) # upload bmp book zipdata = [os.path.join(base_path, 'files', 'cover.bmp')] names = ['cover-0.bmp'] createcbz(upload_file, zipdata, names) self.goto_page('nav_new') upload = self.check_element_on_page((By.ID, 'btn-upload')) upload.send_keys(upload_file) time.sleep(2) cover = self.check_element_on_page((By.ID, "detailcover")).screenshot_as_png self.assertGreaterEqual(diff(BytesIO(cover), BytesIO(original_cover), delete_diff_file=True), 0.049) self.delete_book(-1) time.sleep(2) self.fill_basic_config({'config_uploading': 0}) time.sleep(2) self.assertTrue(self.check_element_on_page((By.ID, "flash_success"))) os.remove(upload_file)
def compare_image(self, output_dir, image1, image2): from diffimg import diff result = diff(image1, image2) return result
def test_generate_data_with_word_split(self): FakeTextDataGenerator.generate( 14, "TEST TEST TEST", "tests/font.ttf", "tests/out/", 64, "png", 0, False, 0, False, 1, 0, 0, False, 0, -1, 0, "#010101", 0, 1, 0, (5, 5, 5, 5), 0, 0, True, ) self.assertTrue( diff( "tests/out/TEST TEST TEST_14.png", "tests/expected_results/TEST TEST TEST_14.png", delete_diff_file=True, ) < 0.01)
def is_images_identical(image_path_1, image_path_2, output_bool=True): percentage_diff = diffimg.diff(image_path_1, image_path_2, delete_diff_file=True) if output_bool: return True if percentage_diff == 0 else False return percentage_diff
def run_diff(): # 计算相似率 res = diff('./files/img1.png', './files/img2.png') # 写入日志 write_diff_log(res) # 删除文件 del_file('files') return res
def run_diff_base64(): # 计算相似率 res = diff('./base64/img1.png', './base64/img2.png') # 写入日志 write_diff_log(res) # 删除文件 del_file('base64') return res
def compare_img(self): res = diff(self.sample_img, self.compared_img_name) try: os.remove(self.compared_img_name) except Exception as e: logfunc(self.name, e) # If two image are same, then return True if res > 0.0: return False return True
def is_equal_with_tolerance(image1, image2, tolerance=Var.current("tolerance")): with allure.step("Comparing two images with tolerance: " + tolerance): image1_path = Img.root_path() + "/Data/Images/" + image1 image2_path = Img.root_path() + "/reports/images/" + image2 result = imgcompare.is_equal(image1_path, image2_path, tolerance=tolerance) allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG) allure.attach.file(image2_path, name=image2, attachment_type=allure.attachment_type.PNG) if not result: if Var.env("snap") == "1": with allure.step("Copying the response file to the source file"): Img.copy_image(image2_path, image1_path) with allure.step("Attaching the diff image file"): image_diff_percent = imgcompare.image_diff_percent(image1_path, image2_path) allure.step("Difference Percentage for comparing images is" + image_diff_percent) diff_img_name = "diff_" + image1 + image2 + ".png" diff_img_path = Img.root_path() + "/reports/images/" + diff_img_name diffimg.diff(image1_path, image2_path, delete_diff_file=False, diff_img_file=diff_img_path, ignore_alpha=False) allure.attach.file(diff_img_path, name=diff_img_name, attachment_type=allure.attachment_type.PNG) assert (result is True), "Compared images are not equal even with tolerance"
def compare_images(image1: str, image2: str): """ :param image1: a string to the first image path :param image2: a string to the second image path """ image_ratio_diff = diff(image1, image2, delete_diff_file=True) assert image_ratio_diff < 0.01, "There is an image ratio difference of {}% between the images". \ format(image_ratio_diff)
def test_generate_data_with_white_background(self): background_generator.plain_white( 64, 128).convert("RGB").save("tests/out/white_background.jpg") self.assertTrue( diff( "tests/out/white_background.jpg", "tests/expected_results/white_background.jpg", delete_diff_file=True, ) < 0.01) os.remove("tests/out/white_background.jpg")
def assertSameImage(img_expected, img_actual, image_test_threshold=0.05, error_msg='same image is expected but diff image found'): ''' exception if different image ''' img_diff_result = diff(img_expected, img_actual) verdict = img_diff_result < image_test_threshold check_point_name = os.path.basename(img_actual).replace('.png', '') DEBUG_MSG = "debug: file: {}, threshold {}, diff result {}, verdict {}".format( img_actual, image_test_threshold, img_diff_result, verdict) print(DEBUG_MSG) assert verdict, check_point_name + ' : ' + error_msg
def is_equal(image1, image2): with allure.step("Comparing two images"): try: image1_path = Img.root_path() + "/Data/Images/" + image1 + ".png" image2_path = Img.root_path() + "/reports/images/" + image2 + ".png" result = imgcompare.is_equal(image1_path, image2_path) allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG) allure.attach.file(image2_path, name=image2, attachment_type=allure.attachment_type.PNG) except Exception as e: allure.step("Exception happened while comparing the image: " + str(e)) result = False if not result: if Var.env("snap") == "1": with allure.step("Copying the response file to the source file"): Img.copy_image(image1_path, image2_path) with allure.step("Attaching the diff image file"): image_diff_percent = imgcompare.image_diff_percent(image1_path, image2_path) allure.step("Difference Percentage for comparing images is" + str(image_diff_percent)) diff_img_name = "diff_" + image1 + image2 + ".png" diff_img_path = Img.root_path() + "/reports/images/" + diff_img_name diffimg.diff(image1_path, image2_path, delete_diff_file=False, diff_img_file=diff_img_path, ignore_alpha=False) allure.attach.file(diff_img_path, name=diff_img_name, attachment_type=allure.attachment_type.PNG) assert (result is True), "Compared images are not equal"
def validate_image(file_name): file_name = os.path.basename(file_name) response = requests.get( f'http://{web_server_host}:{web_server_port}/thumbnails/{file_name}') while response.status_code == 404: time.sleep(1) response = requests.get( f'http://{web_server_host}:{web_server_port}/thumbnails/{file_name}' ) assert response.status_code == 200 assert "image/png" in response.headers['Content-Type'] file = open("test_images/thumbnail.png", "wb") file.write(response.content) file.close() assert diff('test_images/thumbnail.png', 'test_images/test_thumbnail.png') == 0.0
def test_load_metadata(self): self.fill_basic_config({'config_uploading': 1}) time.sleep(3) self.assertTrue(self.check_element_on_page((By.ID, "flash_success"))) self.get_book_details(1) self.check_element_on_page((By.ID, "edit_book")).click() original_cover = self.check_element_on_page((By.ID, "detailcover")).screenshot_as_png self.check_element_on_page((By.ID, "get_meta")).click() time.sleep(3) self.assertEqual("Der Buchtitel", self.check_element_on_page((By.ID, "keyword")).get_attribute("value")) google_scholar = self.check_element_on_page((By.ID, "show-Google Scholar")) google = self.check_element_on_page((By.ID, "show-Google")) comic_vine = self.check_element_on_page((By.ID, "show-ComicVine")) amazon = self.check_element_on_page((By.ID, "show-Amazon")) time.sleep(3) self.assertTrue(amazon) amazon.click() self.assertTrue(google_scholar) self.assertTrue(google) self.assertTrue(comic_vine) # check active searches self.assertTrue(google_scholar.is_selected()) self.assertTrue(google.is_selected()) self.assertTrue(comic_vine.is_selected()) # Check results results = self.find_metadata_results() self.assertEqual(30, len(results)) # Remove one search element comic_vine.click() google.click() results = self.find_metadata_results() self.assertEqual(10, len(results)) results[0]['cover_element'].click() time.sleep(1) cover = self.check_element_on_page((By.ID, "detailcover")).screenshot_as_png self.assertLessEqual(diff(BytesIO(cover), BytesIO(original_cover), delete_diff_file=True), 0.001) self.assertEqual(results[0]['title'], self.check_element_on_page((By.ID, "book_title")).get_attribute("value")) self.assertEqual(results[0]['author'], self.check_element_on_page((By.ID, "bookAuthor")).get_attribute("value")) self.assertEqual(results[0]['publisher'], self.check_element_on_page((By.ID, "publisher")).get_attribute("value")) self.fill_basic_config({'config_uploading': 0}) time.sleep(3) self.assertTrue(self.check_element_on_page((By.ID, "flash_success")))
def test_generate_data_with_tesseract_output_bounding_box(self): FakeTextDataGenerator.generate( 22, "TEST TEST TEST", "tests/font.ttf", "tests/out/", 64, "jpg", 0, False, 0, False, 1, 0, 0, False, 0, -1, 0, "#010101", 0, 1, 0, (5, 5, 5, 5), 0, 0, False, os.path.join( os.path.split(os.path.realpath(__file__))[0], "trdg/images"), output_bboxes=2, ) self.assertLess( diff( "tests/out/TEST TEST TEST_22.jpg", "tests/expected_results/TEST TEST TEST_22.jpg", delete_diff_file=True, ), 0.11) os.remove("tests/out/TEST TEST TEST_22.jpg") os.remove("tests/out/TEST TEST TEST_22.box")
def assert_images_equal(image_1: str, image_2: str): """ Compare if two PNG images are nearly similar. """ # img1 = Image.open(image_1) # img2 = Image.open(image_2) # Convert to same mode and size for comparison # img2 = img2.convert(img1.mode) # img2 = img2.resize(img1.size) # sum_sq_diff = np.sum( # (np.asarray(img1).astype("float") - np.asarray(img2).astype("float")) ** 2, # ) percent_diff = diff(image_1, image_2, delete_diff_file=True, ignore_alpha=False) if percent_diff <= 5.0: # Images are almost the same pass else: # normalized_sum_sq_diff = sum_sq_diff / np.sqrt(sum_sq_diff) # assert normalized_sum_sq_diff < 0.001 assert percent_diff < 5.0
def test_generate_data_with_arabic_text(self): FakeTextDataGenerator.generate( 21, "اختبار اختبار اختبار", "tests/font_ar.ttf", "tests/out/", 64, "png", 0, False, 0, False, 1, 0, 0, False, 1, -1, 0, "#010101", 0, 1, 0, (5, 5, 5, 5), 0, 0, True, os.path.join( os.path.split(os.path.realpath(__file__))[0], "trdg/images"), ) self.assertLess( diff( "tests/out/21_اختبار اختبار اختبار.png", "tests/expected_results/21_اختبار اختبار اختبار.png", delete_diff_file=True, ), 0.05) os.remove("tests/out/21_اختبار اختبار اختبار.png")
def test_upload_epub_cover_formats(self): orig = self.verify_upload(os.path.join(base_path, 'files', 'book.epub')) original = self.check_element_on_page((By.ID, "detailcover")).screenshot_as_png self.delete_book(orig['id']) # check cover-image is detected epub_file = os.path.join(base_path, 'files', 'cover.epub') change_epub_meta(epub_file, meta={'title': "png Cover", 'creator': "Testo"}, item={'change': {"find_id": "cover", 'id':'cover-image', 'href': 'cover.png'}}) with open(os.path.join(base_path, 'files', 'cover.png'), "rb") as f: data = f.read() epub_png = os.path.join(base_path, 'files', 'png.epub') updateZip(epub_png, epub_file, 'cover.png', data) ci = self.verify_upload(epub_png) cover_image = self.check_element_on_page((By.ID, "detailcover")).screenshot_as_png self.delete_book(ci['id']) self.assertAlmostEqual(diff(BytesIO(original), BytesIO(cover_image), delete_diff_file=True), 0.0058, delta=0.0001) os.remove(epub_file) os.remove(epub_png)
def test_generate_data_with_cosine_distorsion(self): FakeTextDataGenerator.generate( 5, "TEST TEST TEST", "tests/font.ttf", "tests/out/", 64, "jpg", 0, False, 3, False, 1, 2, 2, False, 0, -1, 0, "#010101", 0, 1, 0, (5, 5, 5, 5), 0, 0, False, os.path.join( os.path.split(os.path.realpath(__file__))[0], "trdg/images"), ) self.assertTrue( diff( "tests/out/TEST TEST TEST_5.jpg", "tests/expected_results/TEST TEST TEST_5.jpg", delete_diff_file=True, ) < 0.01) os.remove("tests/out/TEST TEST TEST_5.jpg")
def test_generate_data_with_hindi_text(self): FakeTextDataGenerator.generate( 22, "परीक्षा परीक्षा परीक्षा", "tests/font_hi.ttf", "tests/out/", 64, "png", 0, False, 0, False, 1, 0, 0, False, 1, -1, 0, "#010101", 0, 1, 0, (5, 5, 5, 5), 0, 0, True, os.path.join( os.path.split(os.path.realpath(__file__))[0], "trdg/images"), ) self.assertLess( diff( "tests/out/22_परीक्षा परीक्षा परीक्षा.png", "tests/expected_results/22_परीक्षा परीक्षा परीक्षा.png", delete_diff_file=True, ), 0.17) os.remove("tests/out/22_परीक्षा परीक्षा परीक्षा.png")
def test_generate_data_with_right_alignment(self): FakeTextDataGenerator.generate( 8, "TEST TEST TEST", "tests/font.ttf", "tests/out/", 64, "jpg", 0, False, 0, False, 1, 0, 0, False, 0, 1000, 2, "#010101", 0, 1, 0, (5, 5, 5, 5), 0, 0, False, os.path.join( os.path.split(os.path.realpath(__file__))[0], "trdg/images"), ) self.assertLess( diff( "tests/out/TEST TEST TEST_8.jpg", "tests/expected_results/TEST TEST TEST_8.jpg", delete_diff_file=True, ), 0.05) os.remove("tests/out/TEST TEST TEST_8.jpg")
def test_generate_data_with_fit(self): FakeTextDataGenerator.generate( 13, "TEST TEST TEST", "tests/font.ttf", "tests/out/", 64, "jpg", 0, False, 0, False, 1, 0, 0, False, 0, -1, 0, "#010101", 0, 1, 0, (0, 0, 0, 0), 1, 0, False, os.path.join( os.path.split(os.path.realpath(__file__))[0], "trdg/images"), ) self.assertLess( diff( "tests/out/TEST TEST TEST_13.jpg", "tests/expected_results/TEST TEST TEST_13.jpg", delete_diff_file=True, ), 0.19) os.remove("tests/out/TEST TEST TEST_13.jpg")
def test_generate_data_with_wrong_name_format(self): FakeTextDataGenerator.generate( 18, "TEST TEST TEST", "tests/font.ttf", "tests/out/", 64, "png", 0, False, 0, False, 1, 0, 0, False, 3, -1, 0, "#010101", 0, 1, 0, (5, 5, 5, 5), 0, 0, True, os.path.join( os.path.split(os.path.realpath(__file__))[0], "trdg/images"), ) self.assertLess( diff( "tests/out/TEST TEST TEST_18.png", "tests/expected_results/TEST TEST TEST_18.png", delete_diff_file=True, ), 0.05) os.remove("tests/out/TEST TEST TEST_18.png")
def test_generate_vertical_text_with_variable_space(self): FakeTextDataGenerator.generate( 12, "TEST TEST TEST", "tests/font.ttf", "tests/out/", 32, "jpg", 0, False, 0, False, 1, 0, 0, False, 0, -1, 0, "#010101", 1, 2, 0, (5, 5, 5, 5), 0, 0, False, os.path.join( os.path.split(os.path.realpath(__file__))[0], "trdg/images"), ) self.assertLess( diff( "tests/out/TEST TEST TEST_12.jpg", "tests/expected_results/TEST TEST TEST_12.jpg", delete_diff_file=True, ), 0.05) os.remove("tests/out/TEST TEST TEST_12.jpg")
def assertSameImage(json_metadata, img_expected, img_actual, image_test_threshold=0.05, error_msg='same image is expected but diff image found', make_asserts=True): ''' exception if different image ''' img_diff_result = diff(img_expected, img_actual) verdict = img_diff_result < image_test_threshold check_point_name = os.path.basename(img_actual).replace('.png', '') DEBUG_MSG = "debug: file: {}, threshold {}, diff result {}, verdict {}".format( img_actual, image_test_threshold, img_diff_result, verdict) print(DEBUG_MSG) addDebugMessage(json_metadata, 'img_actual', img_actual) addDebugMessage(json_metadata, 'image_test_threshold', image_test_threshold) addDebugMessage(json_metadata, 'img_diff_result', img_diff_result) addDebugMessage(json_metadata, 'verdict', verdict) if make_asserts: assert verdict, check_point_name + ' : ' + error_msg