|
2 | 2 | import allure
|
3 | 3 | import os
|
4 | 4 | import diffimg
|
| 5 | +import requests |
5 | 6 | from Library.variable import Var
|
6 | 7 |
|
7 | 8 |
|
8 | 9 | class Img:
|
9 | 10 |
|
| 11 | + @staticmethod |
| 12 | + def download_image(url, name): |
| 13 | + with allure.step("Downloading image from the URL: " + url): |
| 14 | + response = requests.get(url, stream=True) |
| 15 | + image_path = Img.root_path() + "/reports/images/" + name + ".png" |
| 16 | + with open(image_path, "wb") as image: |
| 17 | + for chunk in response.iter_content(chunk_size=1024): |
| 18 | + if chunk: |
| 19 | + image.write(chunk) |
| 20 | + allure.attach.file(image_path, name="downloaded_image: " + name, attachment_type=allure.attachment_type.PNG) |
| 21 | + |
10 | 22 | @staticmethod
|
11 | 23 | def is_equal(image1, image2):
|
12 |
| - image1_path = Img.root_path() + "/Data/Images/" + image1 |
13 |
| - image2_path = Img.root_path() + "/reports/images/" + image2 |
14 |
| - result = imgcompare.is_equal(image1_path, image2_path) |
15 |
| - allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG) |
16 |
| - allure.attach.file(image2_path, name=image2, attachment_type=allure.attachment_type.PNG) |
| 24 | + with allure.step("Comparing two images"): |
| 25 | + try: |
| 26 | + image1_path = Img.root_path() + "/Data/Images/" + image1 + ".png" |
| 27 | + image2_path = Img.root_path() + "/reports/images/" + image2 + ".png" |
| 28 | + result = imgcompare.is_equal(image1_path, image2_path) |
| 29 | + allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG) |
| 30 | + allure.attach.file(image2_path, name=image2, attachment_type=allure.attachment_type.PNG) |
| 31 | + except Exception as e: |
| 32 | + allure.step("Exception happened while comparing the image: " + str(e)) |
| 33 | + result = False |
17 | 34 | 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) |
25 |
| - return result |
| 35 | + if Var.env("snap") == "1": |
| 36 | + with allure.step("Copying the response file to the source file"): |
| 37 | + Img.copy_image(image1_path, image2_path) |
| 38 | + with allure.step("Attaching the diff image file"): |
| 39 | + image_diff_percent = imgcompare.image_diff_percent(image1_path, image2_path) |
| 40 | + allure.step("Difference Percentage for comparing images is" + str(image_diff_percent)) |
| 41 | + diff_img_name = "diff_" + image1 + image2 + ".png" |
| 42 | + diff_img_path = Img.root_path() + "/reports/images/" + diff_img_name |
| 43 | + diffimg.diff(image1_path, image2_path, delete_diff_file=False, |
| 44 | + diff_img_file=diff_img_path, ignore_alpha=False) |
| 45 | + allure.attach.file(diff_img_path, name=diff_img_name, attachment_type=allure.attachment_type.PNG) |
| 46 | + assert (result is True), "Compared images are not equal" |
26 | 47 |
|
27 | 48 | @staticmethod
|
28 | 49 | def is_equal_with_tolerance(image1, image2, tolerance=Var.current("tolerance")):
|
29 |
| - image1_path = Img.root_path() + "/Data/Images/" + image1 |
30 |
| - image2_path = Img.root_path() + "/reports/images/" + image2 |
31 |
| - result = imgcompare.is_equal(image1_path, image2_path, tolerance=tolerance) |
32 |
| - allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG) |
33 |
| - allure.attach.file(image2_path, name=image2, attachment_type=allure.attachment_type.PNG) |
| 50 | + with allure.step("Comparing two images with tolerance: " + tolerance): |
| 51 | + image1_path = Img.root_path() + "/Data/Images/" + image1 |
| 52 | + image2_path = Img.root_path() + "/reports/images/" + image2 |
| 53 | + result = imgcompare.is_equal(image1_path, image2_path, tolerance=tolerance) |
| 54 | + allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG) |
| 55 | + allure.attach.file(image2_path, name=image2, attachment_type=allure.attachment_type.PNG) |
34 | 56 | 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) |
42 |
| - return result |
| 57 | + if Var.env("snap") == "1": |
| 58 | + with allure.step("Copying the response file to the source file"): |
| 59 | + Img.copy_image(image2_path, image1_path) |
| 60 | + with allure.step("Attaching the diff image file"): |
| 61 | + image_diff_percent = imgcompare.image_diff_percent(image1_path, image2_path) |
| 62 | + allure.step("Difference Percentage for comparing images is" + image_diff_percent) |
| 63 | + diff_img_name = "diff_" + image1 + image2 + ".png" |
| 64 | + diff_img_path = Img.root_path() + "/reports/images/" + diff_img_name |
| 65 | + diffimg.diff(image1_path, image2_path, delete_diff_file=False, |
| 66 | + diff_img_file=diff_img_path, ignore_alpha=False) |
| 67 | + allure.attach.file(diff_img_path, name=diff_img_name, attachment_type=allure.attachment_type.PNG) |
| 68 | + assert (result is True), "Compared images are not equal even with tolerance" |
43 | 69 |
|
44 | 70 | @staticmethod
|
45 | 71 | def root_path():
|
46 | 72 | return os.path.dirname(os.path.abspath(__file__)).replace("/Library", "")
|
| 73 | + |
| 74 | + @staticmethod |
| 75 | + def copy_image(source, destination): |
| 76 | + file = open(source, "wb") |
| 77 | + with open(destination, "rb") as f: |
| 78 | + while True: |
| 79 | + byte = f.read(1) |
| 80 | + if not byte: |
| 81 | + break |
| 82 | + file.write(byte) |
0 commit comments