-
Notifications
You must be signed in to change notification settings - Fork 0
/
Template.py
166 lines (144 loc) · 5.85 KB
/
Template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from ConfigParser import RawConfigParser
from selenium import webdriver
import time
from Section import Section
from bcolors import bcolors
import os
import errors
DRIVER_PATH = "/Users/copelanda1/PycharmProjects/ChangePassword/chromedriver"
class Template():
"""
This class represents a template file.
It is made up of sections from the section class.
"""
def __init__(self):
self.file_path = None
self.driver = None
self.exec_order = None
self.report = None
def set_execute_order(self):
"""
get the order of sections to execute from the template file
:return: A list of the commands to execute
"""
order = []
if os.path.isfile(self.file_path):
f = open(self.file_path, 'r')
else:
raise errors.TemplatePathNotFoundException("The template path cannot be found. Specify the correct template.", 4)
lines = f.readlines()
for line in lines:
# print "Line: {}".format(line)
if line.startswith('[') and line.rstrip().endswith(']'):
section_name = line.replace('[', '').replace(']', '').replace(' ', '').replace('\n', '')
print "Found section: {}".format(section_name)
if section_name != 'general':
order.append(section_name)
f.close()
# print "order: {}".format(order)
self.exec_order = order
def set_template_path(self, path):
"""
Sets the template path self.file_path
:param path: The path to the template file
"""
self.file_path = path
def start_driver(self):
"""
This starts the webdriver and goes to the url specified in the template
"""
self.driver = webdriver.Chrome(DRIVER_PATH)
self.driver.get(self.general.url)
def stop_driver(self):
"""
Stop and close the driver
:return:
"""
self.driver.quit()
self.driver = None
def load_sections(self):
"""
This method will load all of the sections to the template.
Each section will become its own attribute according to the template
e.g. logon_username section will turn into t.logon_username which is type(t.logon_username) == Sections()
"""
config = RawConfigParser()
config.read(self.file_path)
sections = config._sections
for sec in sections:
new_section = Section()
new_section.set_name(sec)
new_section.template = self
for attr in sections[sec]:
new_section.set_attribute(attr, sections[sec][attr])
setattr(self, sec, new_section)
def run(self):
"""
This method is invoked when running on the template is ready.
:return: The report of the run
"""
print "Template dir: {}".format(dir(self))
print "Execution Order: {}".format(self.exec_order)
report = {}
time.sleep(float(self.general.start_wait))
for section in self.exec_order:
print "Executing section: {}".format(section)
sec = getattr(self, section)
res = sec.execute()
report[res['name']] = res
time.sleep(float(self.general.between_wait))
self.report = report
print "Closing the Browser/Driver"
self.stop_driver()
return report
def pprint(self, exclude_none=False):
"""
pretty print the report with nice and preetttyyy collors
"""
if self.report is None:
print "No report to print please run the template"
else:
for section_name in self.exec_order:
o_sec = getattr(self, section_name)
o_sec.pprint(exclude_none=exclude_none)
def easy_report(self, exclude_none=False):
"""
pretty print the report with nice and preetttyyy collors
"""
for section_name in self.exec_order:
o_sec = getattr(self, section_name)
o_sec.easy_report(exclude_none=exclude_none)
def append_log_report(self, log_fname, exclude):
"""
Append the log report of all the sections of the template
:param log_fname: (string) Name of the log file.
:param exclude: (bool) (bool) Exclude section actions that were not performed/tried
:return: Create a log report of this template
"""
f = open(log_fname, 'a')
f.write('~~########################~~\n')
f.write(self.file_path + '\n')
f.write('~~########################~~\n')
f.close()
for s in self.exec_order:
s = getattr(self, s)
s.append_log_report(log_fname, exclude)
def set_dynamic_vars(self, dynamic_dict):
sections = ['general']
sections.extend(self.exec_order)
for sec_name in sections:
o_sec = getattr(self, sec_name)
for attr_name in Section.VALID_ATTRIBUTES:
attr = getattr(o_sec, attr_name)
if attr is not None and attr is not False and attr is not True and isinstance(attr, (str, unicode)):
attr = attr.strip()
if '<' in attr and '>' in attr:
# Attr is has a var placeholder
front_attr, back_attr = attr.split('<', 1)
attr, back_attr = back_attr.split('>', 1)
if attr in dynamic_dict:
# Attr was found in dynamic dict
new_attr = "{}{}{}".format(front_attr, dynamic_dict[attr], back_attr)
setattr(o_sec, attr_name, new_attr)
else:
errors.DynamicVariableNotFoundInTemplateException("Error: Attribute {} was found in Template but not in Dynamic input".format(attr), 1001)