Skip to content

Commit c407fa7

Browse files
Image compare module with folder structure
1 parent 23b54e7 commit c407fa7

File tree

8 files changed

+53
-10
lines changed

8 files changed

+53
-10
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ __pycache__/
1111
.python-version
1212

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

Data/GlobalData/global_data.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
URL: https://naresh.free.beeceptor.com
22
timeout: 10
3+
tolerance: 0.01

Data/Images/.keep

Whitespace-only changes.

Library/images.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import imgcompare
2+
import allure
3+
import os
4+
from Library.variable import Var
5+
6+
7+
class Img:
8+
9+
@staticmethod
10+
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
13+
result = imgcompare.is_equal(image1_path, image2_path)
14+
allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG)
15+
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)
18+
return result
19+
20+
@staticmethod
21+
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
24+
result = imgcompare.is_equal(image1_path, image2_path, tolerance=tolerance)
25+
allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG)
26+
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)
29+
return result

Library/variable.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ class Var:
1313
dynamic_data_path = Store.dynamic_data_path
1414
global_data_path = Store.global_data_path
1515

16-
def __init__(self, file_name, type):
16+
def __init__(self, file_name, file_type):
1717
self.file_name = file_name
18-
if type == "static":
18+
if file_type == "static":
1919
try:
2020
self.file_path = self.static_data_path + '/' + file_name
2121
with open(self.file_path) as file:
2222
self.static_variable = yaml.load(file, Loader=yaml.FullLoader)
2323
allure.attach.file(self.file_path, name=self.file_name, attachment_type=allure.attachment_type.TEXT)
2424
except Exception as e:
2525
print(e)
26-
if type == "dynamic":
26+
if file_type == "dynamic":
2727
try:
2828
self.file_path = self.dynamic_data_path + '/' + file_name
2929
if not os.path.isfile(self.file_path):
@@ -56,6 +56,13 @@ def glob(string):
5656
print(e)
5757
return "None"
5858

59+
@staticmethod
60+
def current(string):
61+
if Var.env(string) == "None":
62+
return Var.glob(string)
63+
else:
64+
return Var.env(string)
65+
5966
def static_value_for(self, string) -> str:
6067
try:
6168
return self.static_variable[string]

Tests/conftest.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import datetime
21
import os
32
import pytest
4-
import allure
53
import glob
64
from Library.store import Store
75

@@ -31,32 +29,38 @@ def pytest_configure(config):
3129
This hook is called for every plugin and initial conftest
3230
file after command line options have been parsed.
3331
"""
34-
print("*-* pytest_configure")
32+
print("*-* pytest_configure" + config)
3533
# Configuring the selpy with data path location
3634
Store.global_data_path = os.path.dirname(
3735
os.path.abspath(__file__)).replace("/Tests", "") + '/Data/GlobalData/global_data.yml'
3836
Store.static_data_path = os.path.dirname(os.path.abspath(__file__)).replace("/Tests", "") + '/Data/TestData/'
3937
Store.dynamic_data_path = os.path.dirname(os.path.abspath(__file__)).replace("/Tests", "") + '/Data/DynamicData/'
38+
root_dir = os.path.dirname(os.path.abspath(__file__)).replace("/Tests", "")
39+
# Clearing the old screenshots from the location.
40+
config_path = root_dir + '/reports/images/*.png'
41+
for CleanUp in glob.glob(config_path):
42+
print(CleanUp)
43+
os.remove(CleanUp)
4044

4145

4246
def pytest_sessionstart(session):
4347
"""
4448
Called after the Session object has been created and
4549
before performing collection and entering the run test loop.
4650
"""
47-
print("*-* pytest_sessionstart")
51+
print("*-* pytest_sessionstart" + session)
4852

4953

5054
def pytest_sessionfinish(session, exitstatus):
5155
"""
5256
Called after whole test run finished, right before
5357
returning the exit status to the system.
5458
"""
55-
print("*-* pytest_sessionfinish")
59+
print("*-* pytest_sessionfinish" + session + exitstatus)
5660

5761

5862
def pytest_unconfigure(config):
5963
"""
6064
called before test process is exited.
6165
"""
62-
print("*-* pytest_unconfigure")
66+
print("*-* pytest_unconfigure" + config)

reports/images/.keep

Whitespace-only changes.

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
imgcompare
12
pytest==5.3.2
23
pytest-cov==2.8.1
34
pytest-metadata==1.8
@@ -9,3 +10,4 @@ allure-pytest==2.8.6
910
ipdb == 0.12.3
1011
pyyaml == 5.3
1112
flake8 == 3.7.9
13+
imgcompare = 2.0.1

0 commit comments

Comments
 (0)