Skip to content

Commit

Permalink
Adding snap way of storing data to the dynamic file
Browse files Browse the repository at this point in the history
  • Loading branch information
nareshnavinash committed Jan 18, 2020
1 parent 48d1145 commit 12a962b
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Data/DynamicData/dyn_google.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: q
search_text: ''
type: text
9 changes: 9 additions & 0 deletions Library/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def text(self) -> str:
else:
return txt_value

def get_attribute(self, name) -> str:
try:
att_value = Store.current_driver.find_element(self.by, self.value).get_attribute(name)
except Exception as e:
print("get attribute not worked at \n" + self.by + "\n" + self.value + "\n Exception: \n" + str(e))
return False
else:
return att_value

def texts(self) -> list:
try:
arr_text = []
Expand Down
72 changes: 64 additions & 8 deletions Library/variable.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
import os
import yaml
import allure


class Var:
file_name = None
local_variable = None
dynamic_variable = None
file_path = None

def __init__(self, file_name):
def __init__(self, file_name, type):
self.file_name = file_name
try:
root_dir = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "")
with open(root_dir + '/Data/TestData/' + file_name) as file:
self.local_variable = yaml.load(file, Loader=yaml.FullLoader)
except Exception as e:
print(e)
if type == "local":
try:
root_dir = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "")
self.file_path = root_dir + '/Data/TestData/' + file_name
with open(self.file_path) as file:
self.local_variable = yaml.load(file, Loader=yaml.FullLoader)
allure.attach.file(self.file_path, name=self.file_name, attachment_type=allure.attachment_type.TEXT)
except Exception as e:
print(e)
if type == "dynamic":
try:
root_dir = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "")
self.file_path = root_dir + '/Data/DynamicData/' + file_name
if not os.path.isfile(self.file_path):
if str(self.env("snap")) == "1":
f = open(self.file_path, "w")
f.close()
with open(self.file_path) as file:
self.dynamic_variable = yaml.load(file, Loader=yaml.FullLoader)
allure.attach.file(self.file_path, name=self.file_name, attachment_type=allure.attachment_type.TEXT)
except IOError as e:
print("File is not accessible\n" + str(e))
except Exception as e:
print(e)

@staticmethod
def env(string):
Expand All @@ -34,9 +55,44 @@ def glob(string):
print(e)
return "None"

def loc(self, string):
def local_value_for(self, string) -> str:
try:
return self.local_variable[string]
except Exception as e:
print(e)
return ""

def write(self, params):
if self.env("snap") == "1":
with open(self.file_path, 'w') as file:
documents = yaml.dump(params, file)

def dynamic_value_for(self, string) -> str:
try:
return self.dynamic_variable[string]
except Exception as e:
print(e)
return ""

def compare(self, displayed_variable):
if self.env("snap") == "1":
self.write(displayed_variable)
for key, value in displayed_variable.items():
try:
file_value = self.dynamic_variable[key]
except Exception as e:
print(e)
file_value = "key_not_available"
if file_value == "key_not_available":
with allure.step("Verifying the key: " + str(key)):
assert (file_value == value), "Key is not available in the dynamic data file\n Key:- " + key \
+ "\nTo store the displayed value try running the suite with\n" \
+ "snap=1 pytest"
else:
with allure.step("Verifying the key: " + str(key)):
assert (file_value == value), "Value for the Key:- " + key + ", Mismatches\n" \
+ "File Value:- " + file_value \
+ "\nDisplayed Value:- " + value \
+ "\nFile used for validation is:" + self.file_name \
+ "\nTo change the Dynamic file value run the suite with" \
+ "\nsnap=1 pytest"
7 changes: 7 additions & 0 deletions Pages/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ def __init__(self):
def enter_search_text(cls, string):
GoogleLocator.search_box.send_keys(string)

@classmethod
def get_search_text(cls) -> str:
return GoogleLocator.search_box.text()

@classmethod
def is_search_box_displayed(cls) -> str:
return GoogleLocator.search_box.is_displayed_with_wait(10)
6 changes: 3 additions & 3 deletions Tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def before_each():
yield
for driver in Store.drivers:
root_dir = os.path.dirname(os.path.abspath(__file__)).replace("/Tests", "")
config_path = root_dir + '/reports/screenshots/img%s.png' % datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
driver.save_screenshot(config_path)
name = 'img%s.png' % datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
allure.attach(config_path, name=name, attachment_type="PNG")
config_path = root_dir + '/reports/screenshots/' + name
driver.save_screenshot(config_path)
allure.attach.file(config_path, name=name, attachment_type=allure.attachment_type.PNG)
except Exception as e:
print(e)
print('*-* After each END')
Expand Down
16 changes: 10 additions & 6 deletions Tests/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@


@allure.feature("Google Search")
@allure.step('Enter and search')
@allure.severity('Critical')
def test_google_search():
dynamic_data = {}
with allure.step("Set the test data file needed for this test run"):
variable = Var("google.yml")
variable = Var("google.yml", "local")
dynamic_var = Var("dyn_google.yml", "dynamic")

with allure.step("first step"):
d = Driver()
d.get(variable.loc("url"))
d.get(variable.local_value_for("url"))
print("landed in google home page")

with allure.step("second step"):
GooglePage.enter_search_text(variable.loc("search_text"))


assert (GooglePage.is_search_box_displayed() == True)
GooglePage.enter_search_text(variable.local_value_for("search_text"))
dynamic_data["search_text"] = GooglePage.get_search_text()
dynamic_data["name"] = GooglePage.search_box.get_attribute("name")
dynamic_data["type"] = GooglePage.search_box.get_attribute("type")
dynamic_var.compare(dynamic_data)
6 changes: 3 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ ignore_missing_imports = True
warn_unused_ignores = False
show_error_codes = True
show_column_numbers = True
pretty = True
color_output = True
pretty = False
color_output = False
error_summary = True
show_absolute_path = True
show_traceback = True
raise_exceptions = True
html_report = html_report
txt_report = txt_report
verbosity = 50
verbosity = 5

0 comments on commit 12a962b

Please sign in to comment.