|
1 | 1 | import imgcompare
|
2 | 2 | import allure
|
3 | 3 | import os
|
| 4 | +import diffimg |
4 | 5 | from Library.variable import Var
|
5 | 6 |
|
6 | 7 |
|
7 | 8 | class Img:
|
8 | 9 |
|
9 | 10 | @staticmethod
|
10 | 11 | def is_equal(image1, image2):
|
11 |
| - image1_path = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "") + "/Data/Images/" + image1 |
12 |
| - image2_path = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "") + "/reports/images/" + image2 |
| 12 | + image1_path = Img.root_path() + "/Data/Images/" + image1 |
| 13 | + image2_path = Img.root_path() + "/reports/images/" + image2 |
13 | 14 | result = imgcompare.is_equal(image1_path, image2_path)
|
14 | 15 | allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG)
|
15 | 16 | allure.attach.file(image2_path, name=image2, attachment_type=allure.attachment_type.PNG)
|
16 |
| - image_diff_percent = imgcompare.image_diff_percent(image1_path, image2_path) |
17 |
| - allure.step("Difference Percentage for comparing images is" + image_diff_percent) |
| 17 | + if not result: |
| 18 | + image_diff_percent = imgcompare.image_diff_percent(image1_path, image2_path) |
| 19 | + allure.step("Difference Percentage for comparing images is" + image_diff_percent) |
| 20 | + diff_img_name = "diff_" + image1 + image2 + ".png" |
| 21 | + diff_img_path = Img.root_path() + "/reports/images/" + diff_img_name |
| 22 | + diffimg.diff(image1_path, image2_path, delete_diff_file=False, |
| 23 | + diff_img_file=diff_img_path, ignore_alpha=False) |
| 24 | + allure.attach.file(diff_img_path, name=diff_img_name, attachment_type=allure.attachment_type.PNG) |
18 | 25 | return result
|
19 | 26 |
|
20 | 27 | @staticmethod
|
21 | 28 | def is_equal_with_tolerance(image1, image2, tolerance=Var.current("tolerance")):
|
22 |
| - image1_path = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "") + "/Data/Images/" + image1 |
23 |
| - image2_path = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "") + "/reports/images/" + image2 |
| 29 | + image1_path = Img.root_path() + "/Data/Images/" + image1 |
| 30 | + image2_path = Img.root_path() + "/reports/images/" + image2 |
24 | 31 | result = imgcompare.is_equal(image1_path, image2_path, tolerance=tolerance)
|
25 | 32 | allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG)
|
26 | 33 | allure.attach.file(image2_path, name=image2, attachment_type=allure.attachment_type.PNG)
|
27 |
| - image_diff_percent = imgcompare.image_diff_percent(image1_path, image2_path) |
28 |
| - allure.step("Difference Percentage for comparing images is" + image_diff_percent) |
| 34 | + if not result: |
| 35 | + image_diff_percent = imgcompare.image_diff_percent(image1_path, image2_path) |
| 36 | + allure.step("Difference Percentage for comparing images is" + image_diff_percent) |
| 37 | + diff_img_name = "diff_" + image1 + image2 + ".png" |
| 38 | + diff_img_path = Img.root_path() + "/reports/images/" + diff_img_name |
| 39 | + diffimg.diff(image1_path, image2_path, delete_diff_file=False, |
| 40 | + diff_img_file=diff_img_path, ignore_alpha=False) |
| 41 | + allure.attach.file(diff_img_path, name=diff_img_name, attachment_type=allure.attachment_type.PNG) |
29 | 42 | return result
|
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def root_path(): |
| 46 | + return os.path.dirname(os.path.abspath(__file__)).replace("/Library", "") |
0 commit comments