-
Notifications
You must be signed in to change notification settings - Fork 1
/
submit_webform.py
176 lines (130 loc) · 5.76 KB
/
submit_webform.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
174
175
176
from os import path
import sys
import json
import re
import argparse
import requests
from datetime import date
from datetime import timedelta
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class CompletionCode:
SUCCESS = 0
DATA_FILE_NOT_FOUND = 1
FAILED_VALIDATION = 2
FINAL_VALIDATION_FAILED = 3
NOT_VALIDATED_SUBMITTION = 4
RUNTIME_ERROR = 127
def calculate_parameters(parameter_list):
params = {}
for param_name in parameter_list.keys():
param_value = parameter_list[param_name].strip()
pattern = '^\$\((.*)\)$'
matched = re.search(pattern, param_value)
if matched:
param_value = eval(matched.group(1))
params[param_name] = param_value
return params
def send_email(subject, body, email_setup):
message = MIMEMultipart()
message['Subject'] = subject
message['From'] = email_setup['from']
message['To'] = email_setup['to']
body_text = MIMEText(body, "plain")
message.attach(body_text)
with smtplib.SMTP(email_setup['server'], email_setup['port']) as server:
server.ehlo()
server.starttls()
server.ehlo()
server.login(email_setup['user'], email_setup['password'])
server.sendmail(email_setup['from'], email_setup['to'], message.as_string())
def collect_hidden_values(web_text):
pattern = 'input type="hidden" name="(.*)" value="(.*)"'
hidden_parameter_list = {}
matched = re.findall(pattern, web_text)
if matched:
for hidden_value in matched:
name, value = hidden_value
name = name.strip()
value = value.strip()
hidden_parameter_list[name] = value
return hidden_parameter_list
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Autofill and submit webforms")
parser.add_argument("-f", "--file", dest='file',
required=True,
help="Definition file path")
args = parser.parse_args()
data_json_path = path.abspath(args.file)
if path.exists(data_json_path) and path.isfile(data_json_path):
data_json = json.load(open(data_json_path))
email_setup = data_json['emailsetup']
caption = data_json['setup']['caption']
send_success_email = (data_json['setup']['sendsuccessemail']).lower()
error_message = None
web_response = None
web_text = None
completion_code = CompletionCode.RUNTIME_ERROR
hidden_parameter_list = {}
for webform in data_json['urls']:
url = webform['url']
validation_text_list = webform['validationtext']
parameter_list = webform['parameters']
submit_hidden = webform['submithidden'].lower()
success_mesage = webform['successmessage']
is_post = len(parameter_list)>0 or submit_hidden=='yes'
if is_post:
params = calculate_parameters(parameter_list)
# add hidden values as parameters
if submit_hidden=='yes':
for hidden_parameter in hidden_parameter_list.keys():
if not hidden_parameter in params:
params[hidden_parameter] = hidden_parameter_list[hidden_parameter]
web_response = requests.post(url, data=params)
else:
web_response = requests.get(url)
web_text= web_response.text
hidden_parameter_list = collect_hidden_values(web_text)
found_validation_text_list = {}
for validation_text in validation_text_list:
matched = re.search(validation_text, web_text)
if matched:
found_validation_text_list[validation_text] = matched.group(0)
else:
error_message = 'Failed validation "{}" in "{}"'.format(validation_text, url)
break
if not error_message==None:
break
if error_message:
body = error_message
if web_response:
body += '\n\n' + str(web_response)
if web_text:
body += '\n\n' + web_text
send_email(caption + ' - Error', body, email_setup)
completion_code = CompletionCode.FAILED_VALIDATION
else:
if success_mesage:
matched = re.search(success_mesage, web_text)
if matched:
if send_success_email=='yes':
body = web_text
send_email(caption + ' - Success', body, email_setup)
completion_code = CompletionCode.SUCCESS
else:
body = web_text
send_email(caption + ' - Error', body, email_setup)
completion_code = CompletionCode.FINAL_VALIDATION_FAILED
else:
body = 'Succesful message is not found in data setup file'
if web_response:
body += '\n\n' + str(web_response)
if web_text:
body += '\n\n' + web_text
send_email(caption + ' - Not validated submittion', body, email_setup)
completion_code = CompletionCode.NOT_VALIDATED_SUBMITTION
else:
print('Error. "{}" data file not found'.format(data_json_path))
completion_code = CompletionCode.DATA_FILE_NOT_FOUND
sys.exit(completion_code)