This repository has been archived by the owner on Aug 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodels.py
173 lines (135 loc) · 5.07 KB
/
models.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
166
167
168
169
170
171
172
173
import random
import copy
import json
import string
__license__ = "MIT"
__maintainer__ = "Samuel Vandamme"
__email__ = "[email protected]"
__author__ = "Samuel Vandamme"
__credits__ = ["Stijn Polfliet", "Samuel Vandamme", "Hatem Mostafa"]
__version__ = "alpha"
class Options:
DEFAULT_THREADS = 5
DEFAULT_VERBOSE = False
DEFAULT_DEBUG = False
DEFAULT_MAXTIME = 0
DEFAULT_TEST = False
DEFAULT_BROWSER = 'phantomjs'
DEFAULT_SCREENSHOT = False
DEFAULT_USERAGENT = 'Mozilla/5.0 (X11; Linux i686; rv:10.0)' + \
' Gecko/20100101 Firefox/10.0'
def __init__(self):
self._threads = Options.DEFAULT_THREADS
self._verbose = Options.DEFAULT_VERBOSE
self._debug = Options.DEFAULT_DEBUG
self._maxTime = Options.DEFAULT_MAXTIME
self._test = Options.DEFAULT_TEST
self._browser = Options.DEFAULT_BROWSER
self._screenshot = Options.DEFAULT_SCREENSHOT
self._userAgent = Options.DEFAULT_USERAGENT
def setThreads(self, threads):
if (self._test):
print "Error: Only 1 thread allowed in test mode"
return
self._threads = threads
def setVerbose(self, verbose):
self._verbose = verbose
def setDebug(self, debug):
self._debug = debug
def setMaximumExectionTime(self, maximum):
self._maxTime = maximum
def setBrowser(self, browser):
self._browser = browser
def setScreenshot(self, screenshot):
self._screenshot = screenshot
def setUserAgent(self, userAgent):
self._userAgent = userAgent
def setTest(self, test):
self._test = test
self._threads = 1
def getThreads(self):
return self._threads
def getMaximumExectionTime(self):
return self._maxTime
def getBrowser(self):
return self._browser
def getUserAgent(self):
return self._userAgent
def getScreenshot(self):
return self._screenshot
def isVerbose(self):
return self._verbose
def isTest(self):
return self._test
def isDebug(self):
return self._debug
def getRunnerOptions(self):
return {
'debug': self.isDebug(),
'verbose': self.isVerbose(),
'screenshot': self.getScreenshot(),
'userAgent': self.getUserAgent()
}
class Scenario():
def __init__(self, scenario):
self._scenario = scenario
def preprocessScenario(self):
"""Preprocess the scenario so that the variables are filled in."""
obj = copy.deepcopy(self._scenario)
variables = copy.deepcopy(obj['variables'])
steps = copy.deepcopy(obj['steps'])
# Loop all variables
for idx in range(len(variables)):
# Current variable
variable = variables[idx]
# Type
varType = variable['type']
searchForExactMatch = False
# Fetch value of variable
if varType == 'randomString':
value = self.getRandomString(variable['length'])
elif varType == 'constant':
value = variable['value']
if isinstance(value, list):
# We will be searching for exact match in case of arrays
# because we will replace the whole variable.
searchForExactMatch = True
# Error message in case unknown type
else:
value = None
print 'Unknown variable type `' + variable['type'] + '`'
# If valid variable
if value is not None:
# Replacing the variable name with
# its value in the following variables.
# String to find
vsyntax = '$[' + variable['name'] + ']'
# Loop variables after current one
for idx2 in range(idx + 1, len(variables)):
variable2 = variables[idx2]
if 'value' in variable2:
if searchForExactMatch:
if variable2['value'] == vsyntax:
variable2['value'] = value
else:
variable2['value'] = json.loads(
json.dumps(variable2['value'])
.replace(vsyntax, value))
# Replacing the variable name with its value in the steps.
for step in steps:
if 'value' in step:
if searchForExactMatch:
if step['value'] == vsyntax:
step['value'] = value
else:
step['value'] = json.loads(
json.dumps(step['value'])
.replace(vsyntax, value))
return steps
def getRandomString(
self,
size=6,
chars=string.ascii_lowercase + string.digits
):
"""Generate random string."""
return ''.join(random.choice(chars) for x in range(size))