-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonkeyInterpreter.py
98 lines (86 loc) · 3.14 KB
/
MonkeyInterpreter.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
from CodeGenerator import *
class MonkeyInterpreter:
def __init__(self, prog, filename=""):
self.prog = prog
self.func_table = {}
self.code_str = ''
self.filename = filename
self.url = ''
self.driver = ''
self.indent = 0
self._code_init()
def _code_init(self):
self.code_str = """from selenium import webdriver
from selenium.webdriver.common.keys import Keys
"""
def save(self):
if len(self.filename) > 0:
code_file = open(self.filename, 'w')
code_file.write(self.code_str)
code_file.close()
return (True, 'Success')
else:
return (False, 'No filename or empty filename')
def curr_indent(self):
return ' '*self.indent
def add_screenshot(self, filename):
self.code_str += "driver.get_screenshot_as_file('%s')\n" % filename
def translate(self):
for index, action in enumerate(self.prog):
if action['type'] == 'auth':
self.code_str += self.transAuth(action)
elif 'single' in action['type']:
self.code_str += self.transSingleAction(action)
elif 'target' in action['type']:
self.code_str += self.transTargetAction(action)
elif 'command' in action['type']:
self.code_str += self.transCommandAction(action)
elif 'judge' in action['type']:
self.code_str += self.transJudgeAction(action)
elif 'repeat' in action['type']:
self.code_str += self.transRepeat(action)
elif 'task' in action['type']:
self.code_str += self.transTask(action)
if "driver.get(" in self.code_str:
self.add_screenshot("%d.png"%index)
self.code_str += "driver.close()"
def transAuth(self, action):
username = action['username']
password = action['password']
return "driver.switch_to.alert.authenticate('%s','%s')" % (username, password)
def transSingleAction(self, action):
move = action['move']
is_success, stmt_str = globals()[move]()
if is_success:
return stmt_str
def transTargetAction(self, action):
move = action['move']
args = {
'target':action['target']
}
if 'value' in action:
args['value'] = action['value']
is_success, stmt_str = globals()[move](**args)
if is_success:
return stmt_str
def transCommandAction(self, action):
move = action['move']
args = {
'value':action['value']
}
is_success, stmt_str = globals()[move](**args)
if is_success:
return stmt_str
def transJudgeAction(self, action):
args = {
'target':action['target'],
'value':action['expect'],
'is_equal' : action['is_equal']
}
is_success, stmt_str = Judge(**args)
if is_success:
return stmt_str
def transRepeat(self, action):
return ""
def transTask(self, action):
return ""