Skip to content

Commit f814fec

Browse files
optimizing image methods
1 parent c407fa7 commit f814fec

File tree

7 files changed

+67
-14
lines changed

7 files changed

+67
-14
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ __pycache__/
1111
.python-version
1212

1313
# pytest reports
14-
#reports/
14+
reports/

Library/api.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import allure
2+
import requests
3+
from Library.variable import Var
4+
from Library.store import Store
5+
6+
7+
class Api:
8+
URL = None
9+
endpoint = None
10+
params = None
11+
response = None
12+
13+
def __init__(self, url):
14+
Api.URL = url
15+
16+
@classmethod
17+
def get(cls, **kwargs):
18+
response = requests.get(Api.URL + Api.endpoint, Api.params, **kwargs)
19+
allure.step("Response for Get with " + Api.endpoint)
20+
allure.step("Get request Prams " + str(Api.params))
21+
allure.step("Other get Args " + str(**kwargs))
22+
allure.attach(response.status_code, name="Response Status Code")
23+
allure.attach(response.headers, name="Response Headers")
24+
allure.attach(response.text, name="Response Text")
25+
allure.attach(response.json(), name="Response JSON")
26+
Api.response = response
27+
28+
@classmethod
29+
def set_end_point(cls, endpoint):
30+
Api.endpoint = endpoint
31+
32+
@classmethod
33+
def set_params(cls, params):
34+
Api.params = params

Library/images.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
11
import imgcompare
22
import allure
33
import os
4+
import diffimg
45
from Library.variable import Var
56

67

78
class Img:
89

910
@staticmethod
1011
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
1314
result = imgcompare.is_equal(image1_path, image2_path)
1415
allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG)
1516
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)
1825
return result
1926

2027
@staticmethod
2128
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
2431
result = imgcompare.is_equal(image1_path, image2_path, tolerance=tolerance)
2532
allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG)
2633
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)
2942
return result
43+
44+
@staticmethod
45+
def root_path():
46+
return os.path.dirname(os.path.abspath(__file__)).replace("/Library", "")

Library/store.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ class Store:
44
global_data_path = None
55
dynamic_data_path = None
66
static_data_path = None
7+
current_response = None

a.png

173 KB
Loading

b.png

173 KB
Loading

requirements.txt

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
imgcompare
1+
requests==2.23.0
22
pytest==5.3.2
33
pytest-cov==2.8.1
44
pytest-metadata==1.8
@@ -7,7 +7,8 @@ pytest-clarity==0.2.0a1
77
pytest-xdist==1.31.0
88
lxml==4.4.2
99
allure-pytest==2.8.6
10-
ipdb == 0.12.3
11-
pyyaml == 5.3
12-
flake8 == 3.7.9
13-
imgcompare = 2.0.1
10+
ipdb==0.12.3
11+
pyyaml==5.3
12+
flake8==3.7.9
13+
imgcompare==2.0.1
14+
diffimg==0.2.3

0 commit comments

Comments
 (0)