Skip to content

Commit 28e0fed

Browse files
adding methods to compare images from the api response
1 parent 1470d3a commit 28e0fed

File tree

5 files changed

+89
-27
lines changed

5 files changed

+89
-27
lines changed

Data/DynamicData/sample.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
created_from: snap mode in pyrest framework
22
test_sample_get_request_001:
3-
name: Naresh
43
age: 20
4+
name: Naresh
55
test_sample_get_request_002:
66
name: $notnull
7+
test_sample_get_request_003:
8+
image: https://avatars0.githubusercontent.com/u/27988169?s=460&v=4
9+
name: Naresh

Data/Images/Naresh.png

38.5 KB
Loading

Library/api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,11 @@ def json_compare(json1, json2):
119119
assert (result is True), "Key " + k + " is not in uuid format"
120120
d1_filtered[k] = d2_filtered[k]
121121
return d1_filtered == d2_filtered
122+
123+
@staticmethod
124+
def get_params_from_response(path):
125+
path_array = path.split(",")
126+
result = Store.current_response.json()
127+
for key in path_array:
128+
result = result[key]
129+
return result

Library/images.py

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,81 @@
22
import allure
33
import os
44
import diffimg
5+
import requests
56
from Library.variable import Var
67

78

89
class Img:
910

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+
1022
@staticmethod
1123
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
1734
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"
2647

2748
@staticmethod
2849
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)
3456
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"
4369

4470
@staticmethod
4571
def root_path():
4672
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)

Tests/sample.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import allure
22
import pytest
33
from Library.api import Api
4+
from Library.images import Img
45

56

67
@allure.feature("Sample get request")
@@ -23,3 +24,17 @@ def test_sample_get_request_002():
2324
Api.verify_response_code(200)
2425
Api.ignore_keys("age")
2526
Api.verify_response_json("sample.yml", "test_sample_get_request_002")
27+
28+
29+
@allure.feature("Sample get request with image URL")
30+
@allure.severity('Critical')
31+
@pytest.mark.regression # Custom pytest marker to run the test cases with ease on demand
32+
@pytest.mark.plain # Custom pytest marker to run the test cases with ease on demand
33+
def test_sample_get_request_002():
34+
Api.get("/image")
35+
Api.verify_response_code(200)
36+
Api.ignore_keys("age")
37+
Api.verify_response_json("sample.yml", "test_sample_get_request_003")
38+
image_url = Api.get_params_from_response("image")
39+
Img.download_image(image_url, "Naresh")
40+
Img.is_equal("Naresh", "Naresh")

0 commit comments

Comments
 (0)