Skip to content

Commit 12a962b

Browse files
Adding snap way of storing data to the dynamic file
1 parent 48d1145 commit 12a962b

File tree

7 files changed

+99
-20
lines changed

7 files changed

+99
-20
lines changed

Data/DynamicData/dyn_google.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: q
2+
search_text: ''
3+
type: text

Library/locator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ def text(self) -> str:
4949
else:
5050
return txt_value
5151

52+
def get_attribute(self, name) -> str:
53+
try:
54+
att_value = Store.current_driver.find_element(self.by, self.value).get_attribute(name)
55+
except Exception as e:
56+
print("get attribute not worked at \n" + self.by + "\n" + self.value + "\n Exception: \n" + str(e))
57+
return False
58+
else:
59+
return att_value
60+
5261
def texts(self) -> list:
5362
try:
5463
arr_text = []

Library/variable.py

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
import os
22
import yaml
3+
import allure
34

45

56
class Var:
67
file_name = None
78
local_variable = None
9+
dynamic_variable = None
10+
file_path = None
811

9-
def __init__(self, file_name):
12+
def __init__(self, file_name, type):
1013
self.file_name = file_name
11-
try:
12-
root_dir = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "")
13-
with open(root_dir + '/Data/TestData/' + file_name) as file:
14-
self.local_variable = yaml.load(file, Loader=yaml.FullLoader)
15-
except Exception as e:
16-
print(e)
14+
if type == "local":
15+
try:
16+
root_dir = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "")
17+
self.file_path = root_dir + '/Data/TestData/' + file_name
18+
with open(self.file_path) as file:
19+
self.local_variable = yaml.load(file, Loader=yaml.FullLoader)
20+
allure.attach.file(self.file_path, name=self.file_name, attachment_type=allure.attachment_type.TEXT)
21+
except Exception as e:
22+
print(e)
23+
if type == "dynamic":
24+
try:
25+
root_dir = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "")
26+
self.file_path = root_dir + '/Data/DynamicData/' + file_name
27+
if not os.path.isfile(self.file_path):
28+
if str(self.env("snap")) == "1":
29+
f = open(self.file_path, "w")
30+
f.close()
31+
with open(self.file_path) as file:
32+
self.dynamic_variable = yaml.load(file, Loader=yaml.FullLoader)
33+
allure.attach.file(self.file_path, name=self.file_name, attachment_type=allure.attachment_type.TEXT)
34+
except IOError as e:
35+
print("File is not accessible\n" + str(e))
36+
except Exception as e:
37+
print(e)
1738

1839
@staticmethod
1940
def env(string):
@@ -34,9 +55,44 @@ def glob(string):
3455
print(e)
3556
return "None"
3657

37-
def loc(self, string):
58+
def local_value_for(self, string) -> str:
3859
try:
3960
return self.local_variable[string]
4061
except Exception as e:
4162
print(e)
4263
return ""
64+
65+
def write(self, params):
66+
if self.env("snap") == "1":
67+
with open(self.file_path, 'w') as file:
68+
documents = yaml.dump(params, file)
69+
70+
def dynamic_value_for(self, string) -> str:
71+
try:
72+
return self.dynamic_variable[string]
73+
except Exception as e:
74+
print(e)
75+
return ""
76+
77+
def compare(self, displayed_variable):
78+
if self.env("snap") == "1":
79+
self.write(displayed_variable)
80+
for key, value in displayed_variable.items():
81+
try:
82+
file_value = self.dynamic_variable[key]
83+
except Exception as e:
84+
print(e)
85+
file_value = "key_not_available"
86+
if file_value == "key_not_available":
87+
with allure.step("Verifying the key: " + str(key)):
88+
assert (file_value == value), "Key is not available in the dynamic data file\n Key:- " + key \
89+
+ "\nTo store the displayed value try running the suite with\n" \
90+
+ "snap=1 pytest"
91+
else:
92+
with allure.step("Verifying the key: " + str(key)):
93+
assert (file_value == value), "Value for the Key:- " + key + ", Mismatches\n" \
94+
+ "File Value:- " + file_value \
95+
+ "\nDisplayed Value:- " + value \
96+
+ "\nFile used for validation is:" + self.file_name \
97+
+ "\nTo change the Dynamic file value run the suite with" \
98+
+ "\nsnap=1 pytest"

Pages/google.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ def __init__(self):
1010
def enter_search_text(cls, string):
1111
GoogleLocator.search_box.send_keys(string)
1212

13+
@classmethod
14+
def get_search_text(cls) -> str:
15+
return GoogleLocator.search_box.text()
16+
17+
@classmethod
18+
def is_search_box_displayed(cls) -> str:
19+
return GoogleLocator.search_box.is_displayed_with_wait(10)

Tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def before_each():
1313
yield
1414
for driver in Store.drivers:
1515
root_dir = os.path.dirname(os.path.abspath(__file__)).replace("/Tests", "")
16-
config_path = root_dir + '/reports/screenshots/img%s.png' % datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
17-
driver.save_screenshot(config_path)
1816
name = 'img%s.png' % datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
19-
allure.attach(config_path, name=name, attachment_type="PNG")
17+
config_path = root_dir + '/reports/screenshots/' + name
18+
driver.save_screenshot(config_path)
19+
allure.attach.file(config_path, name=name, attachment_type=allure.attachment_type.PNG)
2020
except Exception as e:
2121
print(e)
2222
print('*-* After each END')

Tests/google.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@
77

88

99
@allure.feature("Google Search")
10-
@allure.step('Enter and search')
1110
@allure.severity('Critical')
1211
def test_google_search():
12+
dynamic_data = {}
1313
with allure.step("Set the test data file needed for this test run"):
14-
variable = Var("google.yml")
14+
variable = Var("google.yml", "local")
15+
dynamic_var = Var("dyn_google.yml", "dynamic")
1516

1617
with allure.step("first step"):
1718
d = Driver()
18-
d.get(variable.loc("url"))
19+
d.get(variable.local_value_for("url"))
1920
print("landed in google home page")
2021

2122
with allure.step("second step"):
22-
GooglePage.enter_search_text(variable.loc("search_text"))
23-
24-
23+
assert (GooglePage.is_search_box_displayed() == True)
24+
GooglePage.enter_search_text(variable.local_value_for("search_text"))
25+
dynamic_data["search_text"] = GooglePage.get_search_text()
26+
dynamic_data["name"] = GooglePage.search_box.get_attribute("name")
27+
dynamic_data["type"] = GooglePage.search_box.get_attribute("type")
28+
dynamic_var.compare(dynamic_data)

mypy.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ ignore_missing_imports = True
66
warn_unused_ignores = False
77
show_error_codes = True
88
show_column_numbers = True
9-
pretty = True
10-
color_output = True
9+
pretty = False
10+
color_output = False
1111
error_summary = True
1212
show_absolute_path = True
1313
show_traceback = True
1414
raise_exceptions = True
1515
html_report = html_report
1616
txt_report = txt_report
17-
verbosity = 50
17+
verbosity = 5

0 commit comments

Comments
 (0)