-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCypressTestFile.py
More file actions
273 lines (257 loc) · 9.17 KB
/
CypressTestFile.py
File metadata and controls
273 lines (257 loc) · 9.17 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os, re, pprint
from termcolor import colored
class CypressTestFile:
def __init__(self, name, cypress_file_path, remote_images_path, local_images_path, repository_url, htmlpreview):
self.name = name
self.cypress_file_path = cypress_file_path
self.remote_images_path = remote_images_path
self.local_images_path = local_images_path
self.repository_url = repository_url
self.htmlpreview = htmlpreview
file = open(cypress_file_path, "r")
self.text = file.read()
file.close()
def _delete_screenshots(self):
for image_path in os.scandir(self.local_images_path):
os.remove(image_path)
def document(self, mode):
doc = self.text
for repl in self._replacements(): # Recurse
# FIXME: Filter
# mode = python or html
doc = re.sub(repl[0], repl[1][mode], doc, flags=re.M)
return "<table>" + doc + "</table>"
def _header(self):
return f"""
<img src="https://mwstake.org/mwstake/branding/logo.png" style="width:50px;"/>
<ol>
<li>
This is the <b>documentation on the use cases</b> enabled by <a href="{self.repository_url}">{self.repository_url}</a>.
</li>
<li>
These use cases based on <a href='{self.repository_url}/tree/main/cypress/e2e/CRUD/{self.file_name()}'>{self.file_name()}</a> are curated by MWStake and currently <b>certified for MWCore 1.36</b> in conjunction with <a href="">this set of extensions</a>.
</li>
</ol>
"""
def file_name(self):
return self.cypress_file_path.name.split('/')[-1]
def _script(self):
return """
<script>
</script>
"""
def save_html_document(self, html_file_path):
html = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{self.name}</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
{self._script()}
{self._header()}
{self.document("html")}
</body>
</html>
"""
file = open(html_file_path, "w")
file.write(html + CypressTestFile.styles())
file.close()
@staticmethod
def styles():
return """
<style>
td {
border: 1px solid gray;
}
.hide {
display: none;
}
body {
font-family:Sans-serif;
line-height:150%;
padding:50px;
}
.describe {
padding-top:10px;
}
.aspect {
font-weight:bold; color:green;text-decoration: underline;
}
.feature {
font-weight:bold;
color:green;padding-bottom:5px;
}
.goto {
font-weight:bold; color:orange;
}
.do {
font-weight:bold; color:orange;
}
.left20 {
padding-left:20px; padding-top:20px;
}
.left40 {
padding-left:40px; padding-top:5px;
}
.left60 {
padding-left:60px; padding-top:5px;
}
.comment {
color:grey;
}
.screenshot {
color:green;
}
a {
text-decoration:none;
}
td {
vertical-align:top;
padding:10px;
}
.screenshotPNG {
box-shadow: -5px -5px 5px grey;
width:500px;
}
.command {
font-weight:bold;
}
.arguments {
font-family:mono;
}
</style>
"""
def _screenshot_name_patterns(self):
html = """
<tr><td>
<div class='left40'>\\1</div></td><td>
<div class='left60'>
<img class="screenshotPNG" src='"""+self.remote_images_path+"""/\\1.png' />
</div>
</td></tr>"""
return [
[
r"cy.__take_screenshot\(\"(.*)\"\);",
{
"python": colored("See", "blue") + " \\1",
"html": html,
"image": "\\1",
}
],
[
r"cy.clip_screenshot_and_click\(\n?\$target,[\n ]?\"(.*)\"\n?\);",
{
"python": colored("See", "blue") + " \\1",
"html": html,
"image": "\\1",
}
],
[
r"cy.clip_screenshot_and_save_search_facet\(\n?unixTimestamp,[\n ]?\"(.*)\"\n?\);",
{
"python": colored("See", "blue") + " \\1",
"html": html,
"image": "\\1",
}
]
]
def _replacements(self):
return [
[r"^ *(cy.(get)).+", {"python": "", "html": ""}],
[r"^(?! *(describe|it|cy.|//)).+\n", {"python": "", "html": ""}],
[
r"^describe\(\"([\w -:]*)\",.*",
{
"python": colored("ASPECT", "green", attrs=["bold", "underline"]) + " \\1\n",
"html": "<tr><td colspan=2><div class='describe'><span class='aspect'>Aspect</span>: <b>\\1</b></div></td></tr>",
},
],
[
r"^ ?it\.?[a-z]*\(\"(.*)\"",
{
"python": colored("Use Case: ", "green", attrs=["bold"]) + "it \\1",
"html": "<tr><td colspan=2><div class='left20'><span class='feature'>Use Case</span>: <b>it \\1</b></div></td></tr>",
},
],
[
r"cy.visit\(\"(.*)\"\);",
{
"python": colored("Go to", "yellow") + " \\1",
"html": "<tr><td colspan=2><div class='left40'><span class='goto'>Go to</span> \\1</div></td></tr>",
},
]
] + self._screenshot_name_patterns() + [
[ # Remove cy.__* commands
r"cy.(__\w*)\(.*(\);)?",
{
"python": "",
"html": "",
},
],
[
r"cy.(\w*)\(.*(\);)?",
{
"python": colored("Do", "yellow") + " \\1()",
"html": f"<tr><td colspan=2><div class='left40'><span class='do'>Do</span> <a href='{self.htmlpreview}/commands.html' class='command' title='Click to get an explanation for \\1'>\\1</a>(<span class='arguments'></span>)</div></td></tr>",
},
],
[
r", \((.*)\) => {",
{
"python": "\\1",
"html": "\\1"
}
],
[ r"\);\n",
{
"python": "",
"html": ""
}
],
[
r"^( +)// *(.*)$",
{
"python": colored("\\1\\2", "yellow"),
"html": "<tr><td colspan=2><div class='left40 comment'>\\1\\2</div></td></tr>",
},
],
]
@staticmethod
def list_commands(commands_file_path, html_file_path):
file = open(commands_file_path, "r")
text = file.read()
file.close()
commands_list = "<hr/>".join(
list(
map(
lambda x: f"<span class='command'>{x[0]}</span>(<span class='arguments'>{x[1]}</span>)",
re.findall(r"Cypress\.Commands\.add\(\"(\w+)\", \(([, \w]*)\) => {", text),
)
)
)
html = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>MWStake Doc</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<script src="index.js"></script>
{commands_list}
{CypressTestFile.styles()}
</body>
</html>
"""
file = open(html_file_path, "w")
file.write(html)
file.close()
return