-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
387 lines (349 loc) · 19.9 KB
/
chatbot.py
File metadata and controls
387 lines (349 loc) · 19.9 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import os
import time
import boto3
from boto3.s3.transfer import S3Transfer
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
boto3.setup_default_session(
region_name='ap-northeast-2',
aws_access_key_id=os.getenv('FLANB_AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('FLANB_AWS_SECRET_ACCESS_KEY'))
s3_client = boto3.client('s3')
s3_transfer = S3Transfer(s3_client)
class FBChatbotTest:
def init(self):
chrome_options = Options()
chrome_options.add_argument('headless')
# chrome_options.add_argument('window-size=1920x1080')
# chrome_options.add_argument("disable-gpu")
# chrome_options.add_argument("lang=zh")
self.driver = webdriver.Chrome('./chromedriver_%s' % os.getenv('FLANB_OS'), chrome_options=chrome_options)
self.driver.get('https://www.messenger.com/t/TravelFlan')
self.driver.implicitly_wait(1)
self.login(os.getenv('FLANB_FB_ADMIN_ACCOUNT'), os.getenv('FLANB_FB_ADMIN_PASSWORD'))
self.clear()
# 시작하기
self.execute_while_limited(self.driver.find_element_by_xpath, '//a[@class="_2xh6 _2xh7"]').click()
def send_keys_with_speed(self, element, text, pause=0.1):
for character in text:
element.send_keys(character)
time.sleep(pause)
def execute(self, method, times):
start_time = time.time()
print('%s start' % (method.__name__, ))
for e in range(times):
self.init()
print('Try %s.....' % (e+1,))
if self._execute(method) is True:
print('%s end' % (method.__name__,))
self.driver.quit()
print('SUCCESSFUL! %s secs' % (time.time() - start_time))
return True
else:
filename = 'screenshots/' + str(datetime.now()) + '.png'
self.driver.save_screenshot(filename)
s3_transfer.upload_file('./%s' % filename, 'flanb-data', 'travis/screenshots/%s/%s' % (
os.getenv('TRAVIS_BUILD_NUMBER'), str(datetime.now()) + '.png'))
self.driver.quit()
return False
def _execute(self, method):
try:
return method()
except Exception as e:
print(e)
return False
def execute_while_limited(self, command, param, timeout=30):
for i in range(timeout-1):
try:
cmd = command(param)
except Exception as e:
time.sleep(1)
else:
# print('%s(\'%s\')' % (command.__name__, param))
# time.sleep(1)
return cmd
raise Exception('[FAILED] %s(\'%s\')' % (command.__name__, param))
def clear(self):
# settings button
self.execute_while_limited(self.driver.find_element_by_xpath,
'//*[@class="_1jt6"]//*[@class="_5blh _4-0h"]').click()
# remove button
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()="삭제"]|//*[text()="Delete"]').click()
time.sleep(1)
# remove button
self.execute_while_limited(self.driver.find_element_by_xpath,
'//button[@class="_3quh _30yy _2t_ _3ay_ _5ixy"]').click()
self.driver.get('https://www.messenger.com/t/TravelFlan')
def login(self, id, pw):
print('login...')
self.execute_while_limited(self.driver.find_element_by_id, 'email').send_keys(id)
self.execute_while_limited(self.driver.find_element_by_id, 'pass').send_keys(pw)
self.execute_while_limited(self.driver.find_element_by_id, 'loginbutton').click()
print('login succeed')
def back_to_main(self):
# Menu button
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[@class="_3km2"]').click()
# Back to main menu button
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[@class="_1wc8 _1wc9"]').click()
def itinerary(self):
self.back_to_main()
self.execute_while_limited(self.driver.find_element_by_xpath, '//a[text()="Start Planning"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[text()="Itinerary"]').click()
self.driver.implicitly_wait(3)
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()="Get started"]').click()
self.driver.switch_to.frame('messenger_ref')
self.execute_while_limited(self.driver.find_element_by_xpath, '//span[text()="seoul"]').click()
self.driver.execute_script("window.scrollTo(0,500)")
self.execute_while_limited(self.driver.find_element_by_xpath, '//span[text()="3 Day"]').click()
self.driver.execute_script("window.scrollTo(0,800)")
self.execute_while_limited(self.driver.find_element_by_xpath, '//li[@class="theme-filter"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//li[@class="theme-filter"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//li[@class="theme-filter"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//span[text()="Send to TravelFlan"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath,
'//*[text()="Btw, we just saved 4 hours for you! Did you like our service?"]')
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()="⭐⭐⭐⭐⭐"]').click()
return True
def hotels(self):
self.back_to_main()
self.execute_while_limited(self.driver.find_element_by_xpath, '//a[text()="Start Planning"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[text()="Hotels"]').click()
self.driver.implicitly_wait(3)
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()="Get started"]').click()
self.driver.implicitly_wait(1)
self.driver.switch_to.frame('messenger_ref')
self.send_keys_with_speed(
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[@class="SearchBox"]//input'), 'seoul')
self.execute_while_limited(self.driver.find_elements_by_xpath,
'//ul[@class="ul--HotelsSearchResult"]//div')[0].click()
self.execute_while_limited(self.driver.find_element_by_xpath,
'//label[text()="Search without room details"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath,
'//*[text()[contains(., "Send to TravelFlan")]]').click()
self.execute_while_limited(self.driver.find_element_by_xpath,
'//*[text()="You are nearly there! Here\'s your search result:"]')
return True
def restaurants(self):
self.back_to_main()
self.execute_while_limited(self.driver.find_element_by_xpath, '//a[text()="Start Planning"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[text()="Restaurants"]').click()
self.driver.implicitly_wait(3)
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()="Get started"]').click()
self.driver.implicitly_wait(1)
self.driver.switch_to.frame('messenger_ref')
time.sleep(3)
self.send_keys_with_speed(
self.execute_while_limited(self.driver.find_element_by_xpath, '//input[@class="geocoder-search"]'), 'seoul')
time.sleep(1)
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()="Seoul, South Korea"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//span[text()="Korean"]').click()
time.sleep(1)
self.driver.execute_script("window.scrollTo(0,10000)")
self.execute_while_limited(self.driver.find_element_by_xpath, '//span[text()="Restaurant"]').click()
time.sleep(1)
self.driver.execute_script("window.scrollTo(0,10000)")
self.execute_while_limited(self.driver.find_element_by_xpath, '//span[text()="Rice"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath,
'//*[text()[contains(., "Send to TravelFlan")]]').click()
self.execute_while_limited(self.driver.find_element_by_xpath,
'//*[text()="Btw, we just saved 25 minutes for you! Did you like our service?"]')
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()="⭐⭐⭐⭐⭐"]').click()
return True
def weather(self):
self.back_to_main()
time.sleep(1)
self.execute_while_limited(self.driver.find_element_by_xpath, '//a[@class="_32rk _32rh _1cy6"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//a[text()="Get Help"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[text()="Weather"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath,
'//*[text()[contains(., "What is the location that you wish to search for?")]]')
time.sleep(5)
action = ActionChains(self.driver)
action.send_keys('seoul')
action.send_keys(Keys.RETURN)
action.perform()
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[@class="_3u69 _36wf _2pi9"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath,
'//*[text()="Keep exploring our services and products"]')
return True
def directions(self):
self.back_to_main()
time.sleep(1)
self.execute_while_limited(self.driver.find_element_by_xpath, '//a[@class="_32rk _32rh _1cy6"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//a[text()="Get Help"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[text()="Directions"]').click()
self.driver.implicitly_wait(3)
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()="Get started"]').click()
self.driver.implicitly_wait(3)
self.driver.switch_to.frame('messenger_ref')
time.sleep(3)
self.send_keys_with_speed(
self.execute_while_limited(self.driver.find_elements_by_xpath, '//input[@class="trans-search"]')[0],
'seoul')
time.sleep(1)
# self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "Seoul, 대한민국")]]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()="Seoul, South Korea"]').click()
self.send_keys_with_speed(
self.execute_while_limited(self.driver.find_elements_by_xpath, '//input[@class="trans-search"]')[1],
'busan')
time.sleep(1)
# self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "Busan, 대한민국")]]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()="Busan, South Korea"]').click()
time.sleep(1)
self.execute_while_limited(self.driver.find_element_by_xpath,
'//*[text()[contains(., "Send to TravelFlan")]]').click()
self.execute_while_limited(self.driver.find_element_by_xpath,
'//*[text()="Keep exploring our services and products"]')
return True
def main(self):
if not self.execute(self.itinerary, 5):
raise Exception('FAILED ITINERARY')
if not self.execute(self.hotels, 5):
raise Exception('FAILED HOTELS')
if not self.execute(self.restaurants, 5):
raise Exception('FAILED RESTAURANTS')
if not self.execute(self.weather, 5):
raise Exception('FAILED WEATHER')
if not self.execute(self.directions, 5):
raise Exception('FAILED DIRECTIONS')
class WDChatbotTest:
def init(self):
chrome_options = Options()
# chrome_options.add_argument('headless')
# chrome_options.add_argument('window-size=1920x1080')
# chrome_options.add_argument("disable-gpu")
# chrome_options.add_argument("lang=zh")
self.driver = webdriver.Chrome('./chromedriver_%s' % os.getenv('FLANB_OS'), chrome_options=chrome_options)
self.driver.get('https://www.travelflan.com/')
self.driver.implicitly_wait(1)
time.sleep(5)
self.driver.switch_to.frame('tf-widget-iframe')
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[@class="tf-icon"]').click()
time.sleep(1)
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "行程規劃太費事")]]')
def send_keys_with_speed(self, element, text, pause=0.1):
for character in text:
element.send_keys(character)
time.sleep(pause)
def execute(self, method, times):
start_time = time.time()
print('%s start' % (method.__name__,))
for e in range(times):
self.init()
print('Try %s.....' % (e+1,))
if self._execute(method) is True:
print('%s end' % (method.__name__,))
self.driver.quit()
print('SUCCESSFUL! %s secs' % (time.time() - start_time))
return True
else:
filename = 'screenshots/' + str(datetime.now()) + '.png'
self.driver.save_screenshot(filename)
s3_transfer.upload_file('./%s' % filename, 'flanb-data', 'travis/screenshots/%s/%s' % (
os.getenv('TRAVIS_BUILD_NUMBER'), str(datetime.now()) + '.png'))
self.driver.quit()
return False
def _execute(self, method):
try:
return method()
except Exception as e:
print(e)
return False
def execute_while_limited(self, command, param, timeout=30):
for i in range(timeout-1):
try:
cmd = command(param)
except Exception as e:
time.sleep(1)
else:
# print('%s(\'%s\')' % (command.__name__, param))
# time.sleep(1)
return cmd
raise Exception('[FAILED] %s(\'%s\')' % (command.__name__, param))
def itinerary(self):
self.execute_while_limited(self.driver.find_element_by_xpath,
'//div[@class="quickmenu-image itinerary"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()=" 首爾 "]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()="3 天 "]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "景點推薦")]]').click()
self.driver.execute_script("window.scrollTo(0,10000)")
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[text()[contains(., "送出")]]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "Day 1-1")]]')
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "Day 2-1")]]')
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "Day 3-1")]]')
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "評分")]]')
return True
def hotels(self):
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[@class="quickmenu-image hotel"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//button[@class="hotel__button"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath,
'//div[@class="SearchBox"]/input[@class="inputBox"]').send_keys('首爾')
time.sleep(1)
self.execute_while_limited(self.driver.find_elements_by_xpath, '//ul[@class="ul--HotelsSearchResult"]/li')[
0].click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "暫不指定客房要求")]]').click()
self.execute_while_limited(self.driver.find_element_by_xpath,
'//*[text()[contains(., "Send to TravelFlan")]]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "按此瀏覽")]]')
return True
def booking(self):
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[@class="quickmenu-image booking"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "歡迎使用”餐廳預約服務”,請")]]')
return True
def weather(self):
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[@class="quickmenu-image weather"]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "您想查詢哪裡的天氣呢 ?")]]')
self.execute_while_limited(self.driver.find_element_by_xpath,
'//div[@class="search--input__box"]/input[@class="search--input"]').send_keys('Pangyo')
time.sleep(2)
self.execute_while_limited(self.driver.find_elements_by_xpath, '//ul[@class="search--result__container"]/li')[
0].click()
time.sleep(1)
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "送出")]]').click()
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "的天氣")]]')
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., " 是")]]').click()
forecast = self.execute_while_limited(
self.driver.find_elements_by_xpath,
'//div[@class="Weather-forecast__container"]/div[@class="Weather-day__container"]')
assert len(forecast) == 10
return True
def directions(self):
self.execute_while_limited(self.driver.find_element_by_xpath, '//div[@class="quickmenu-image direction"]').click()
self.execute_while_limited(
self.driver.find_element_by_xpath,
'//div[@class="search--input__box add-padding"]/input[@class="search--input"]').send_keys('Pangyo Techno Valley')
time.sleep(1)
self.execute_while_limited(self.driver.find_elements_by_xpath, '//ul[@class="search--result__container"]/li')[
0].click()
time.sleep(3)
self.execute_while_limited(
self.driver.find_element_by_xpath,
'//div[@class="search--input__box add-padding"]/input[@class="search--input"]')
self.execute_while_limited(
self.driver.find_elements_by_xpath,
'//div[@class="search--input__box add-padding"]/input[@class="search--input"]')[1].send_keys('Yatap Station')
time.sleep(2)
self.execute_while_limited(self.driver.find_elements_by_xpath, '//ul[@class="search--result__container"]/li')[
0].click()
time.sleep(2)
self.execute_while_limited(self.driver.find_element_by_xpath, '//*[text()[contains(., "送出")]]').click()
return True
def main(self):
if not self.execute(self.itinerary, 5):
raise Exception('FAILED ITINERARY')
if not self.execute(self.hotels, 5):
raise Exception('FAILED HOTELS')
if not self.execute(self.booking, 5):
raise Exception('FAILED BOOKING')
if not self.execute(self.weather, 5):
raise Exception('FAILED WEATHER')
if not self.execute(self.directions, 5):
raise Exception('FAILED DIRECTIONS')
if __name__ == '__main__':
# fb_test = FBChatbotTest()
# fb_test.main()
wd_test = WDChatbotTest()
wd_test.main()