forked from siversalih/pyChrome-Web-Automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyChrome.py
459 lines (402 loc) · 15.8 KB
/
pyChrome.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import time
import os
import sys
import logging
curr_dir = os.getcwd()
logging.info("Current Work directory: {}".format(curr_dir))
src_dir = "{}/src".format(curr_dir)
logging.info("Source directory: {}".format(src_dir))
if not(os.path.exists(src_dir)):
logging.critical("Path {} does not exist".format(src_dir))
exit(1)
sys.path.insert(0, src_dir)
try:
from window import Window
except ImportError:
logging.critical("window.py is missing...Exiting program.")
exit(1)
try:
from browser import Browser
except ImportError:
logging.critical("browser.py is missing...Exiting program.")
exit(1)
try:
from element import Element
except ImportError:
logging.critical("element.py is missing...Exiting program.")
exit(1)
try:
from interaction import Interaction
except ImportError:
logging.critical("interaction.py is missing...Exiting program.")
exit(1)
try:
from combo import Combo
except ImportError:
logging.critical("combo.py is missing...Exiting program.")
exit(1)
binary_dir = "{}/bin".format(curr_dir)
logging.info("Binary directory: {}".format(binary_dir))
if(not os.path.exists(binary_dir)):
logging.critical("Path {} does not exist".format(binary_dir))
exit(1)
try:
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
except ImportError:
logging.critical("Selenium module is not installed...Exiting program.")
exit(1)
try:
import json
except ImportError:
logging.critical("json module is not installed...Exiting program.")
exit(1)
class PyChrome(Element,Interaction,Combo):
config_filename = ""
drivername = ""
directory = ""
bin_dir = ""
pageload_timeout = 0
driver = None
ghost = 0
window = None
browser = None
selectedElement = None
elements = None
def __init__(self, config_filename=None, ghostmode=0):
self.config_filename = "config.json"
self.bin_dir = binary_dir
self.directory = os.getcwd()
self.pageload_timeout = -1
self.ghost = False
if ghostmode and self.validateGhostmode(ghostmode):
self.ghost = ghostmode
logging.info("Ghost Mode: {}".format(self.ghost))
if config_filename:
self.config_filename = config_filename
file_directory = "{}/{}".format(self.directory,self.config_filename)
if(os.path.exists(file_directory)):
self.__readJSONFile(self.config_filename,ghostmode=ghostmode)
else:
logging.critical("{} is not in {}".format(self.config_filename,self.directory))
exit(1)
self.__start(ghostmode=self.ghost)
return
def __readJSONFile(self, config_filename=None, ghostmode=0):
if config_filename:
self.config_filename = config_filename
logging.info("Reading configuration file {}".format(self.config_filename))
with open(self.config_filename) as jsonFile:
config = json.load(jsonFile)
self.drivername = config.get('driver')
directory = config.get('directory')
file_directory = "{}/{}".format(directory,self.drivername)
if directory and len(directory):
if os.path.exists(file_directory):
self.directory = directory
else:
logging.critical("{} is not in {}".format(self.drivername,directory))
exit(1)
else:
directory = self.bin_dir
file_directory = "{}/{}".format(directory,self.drivername)
if os.path.exists(file_directory):
self.bin_dir = directory
else:
logging.critical("{} is not in {}".format(self.drivername,self.directory))
exit(1)
pageload_timeout = config.get('pageload_timeout')
if self.validatePageLoadTimeout(pageload_timeout):
self.pageload_timeout = pageload_timeout
logging.info("Page Load Timeout Set to {}".format(self.pageload_timeout))
if ghostmode and self.validateGhostmode(ghostmode):
self.ghost = ghostmode
else:
ghostmode = config.get('ghost')
if self.validateGhostmode(ghostmode):
self.ghost = ghostmode
return 0
def __start(self, ghostmode = 0):
if ghostmode and self.validateGhostmode(ghostmode):
self.ghost = ghostmode
if self.ghost:
self.drivername = "phantomjs"
logging.info("Using WebDrver {}".format(self.drivername))
logging.info("Starting Ghost Browser")
file_directory = "{}/{}".format(self.bin_dir,self.drivername)
if (os.path.exists(file_directory)):
self.driver = webdriver.PhantomJS(executable_path=file_directory)
self.driver.set_page_load_timeout(self.pageload_timeout)
self.browser = Browser(self.driver,True)
else:
logging.critical("GhostDriver (PhantomJS) is not present!")
exit(1)
else:
self.drivername = "chromedriver"
logging.info("Using WebDrver {}".format(self.drivername))
logging.info("Starting Chrome Browser")
file_directory = "{}/{}".format(self.bin_dir,self.drivername)
if (os.path.exists(file_directory)):
chrome_options = Options()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
chrome_options.add_argument("--disable-extensions")
self.driver = webdriver.Chrome(file_directory,0,chrome_options,None,None,None)
self.driver.set_page_load_timeout(self.pageload_timeout)
self.window = Window(self.driver, config_filename=self.config_filename)
self.browser = self.window.browser
else:
logging.critical("ChromeDriver is not present!")
exit(1)
logging.info("Driver Location: {}".format(self.bin_dir))
logging.info("Driver Name: {}\n".format(self.drivername))
self.findBodyElement()
self.elements = []
time.sleep(1)
return 0
def quit(self):
logging.info("Quiting {} Session".format(self.drivername))
if self.browser:
self.browser.dealloc()
del self.browser
if self.window:
self.window.dealloc()
del self.window
if self.driver:
self.driver.stop_client()
self.driver.quit()
del self.driver
else:
logging.warning("Driver is not present! There is not Driver to deallocate")
return 1
logging.info("Quited the Session and Closed the Client")
if self.elements:
del self.elements[:]
del self.elements
if self.selectedElement:
del self.selectedElement
return 0
def switchDriverMode(self, ghostmode = 0):
if ghostmode and self.validateGhostmode(ghostmode):
logging.info("Setting Ghost Mode to {}".format(ghostmode))
self.ghost = ghostmode
self.ghostmode = self.ghost
else:
logging.info("Switching Ghost Mode: {} -> {}".format(self.ghost,self.ghost ^ True))
self.ghost = self.ghost ^ True
self.quit()
self.__init__(self.config_filename,self.ghost)
return 0
def validatePageLoadTimeout(self,pageload_timeout):
try:
pageload_timeout = int(pageload_timeout)
except ValueError:
logging.error("ValueError Exep: Page Load Timeout Invalid Format: {}".format(pageload_timeout))
return 0
if not isinstance(pageload_timeout,int):
logging.error("Page Load Timeout is not Int instance".format(pageload_timeout))
return 0
if pageload_timeout < -1:
logging.error("Page Load Timeout is < -1".format(pageload_timeout))
return 0
if pageload_timeout > 120:
logging.error("Page Load Timeout is > 120".format(pageload_timeout))
return 0
return 1
def validateGhostmode(self,ghostmode):
if not isinstance(ghostmode, bool):
logging.error("Ghost Mode is not Boolean expression".format(ghostmode))
return 0
return 1
def open(self,url):
# Invalid URL
if url == 0 or len(url) == 0:
logging.error("Couldn't Open the page {}. \nPlease check the URL".format(url))
return 1
# Search Instead
if not "www." in url and not ".com" in url and not "." in url:
err = self.search(url)
if err:
logging.error("Failed to Search")
return 1
return 0
err=self.browser.open(url)
element = self.findBodyElement()
if not element:
err = 1
return err
def getTabTitle(self):
return self.browser.getcurrentTabTitle()
def getTabLink(self):
return self.browser.getcurrentTabLink()
def close(self):
return self.browser.close()
def closeTab(self, tabnum=0):
err = self.browser.closeTab(tabnum=tabnum)
element = self.findBodyElement()
if not element:
err = 1
return err
def newTab(self,url=None):
err = self.browser.newTab(url=url)
element = self.findBodyElement()
if not element:
err = 1
return err
def rightTab(self):
err = self.browser.rightTab()
element = self.findBodyElement()
if not element:
err = 1
return err
def leftTab(self):
err = self.browser.leftTab()
element = self.findBodyElement()
if not element:
err = 1
return err
def switchTab(self, index):
err = self.browser.switchTab(index)
element = self.findBodyElement()
if not element:
err = 1
return err
def position(self, windowPosition):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.position(windowPosition)
def size(self, windowSize):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.size(windowSize)
def zoom(self, percent):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.zoom(percent)
def zoomIn(self):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.zoomIn()
def zoomOut(self):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.zoomOut()
def scrollToElement(self, element):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
err = self.switchElement(element)
return err or self.window.scrollToElement(element)
def scrol(self,scrollWin):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.scrol(scrollWin=scrollWin)
def scrollDown(self):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.scrollDown()
def scrollBottom(self,animate=0):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.scrollBottom(animate)
def scrollUp(self):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.scrollUp()
def scrollTop(self,animate=0):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.scrollTop(animate)
def scrollRight(self):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.scrollRight()
def scrollLeft(self):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.scrollLeft()
def validateSize(self, sizeWin):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.validateSize(sizeWin)
def validateZoom(self, percent):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.validateZoom(percent)
def validateScroll(self, scrollWin):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.validateScroll(scrollWin)
def validatePosition(self, positionWin):
if self.ghost:
logging.warning("When in Ghost Mode using {}, there is no Window.".format(self.drivername))
return 0
return self.window.validatePosition(positionWin)
def back(self):
return self.browser.back()
def forward(self):
return self.browser.forward()
def refresh(self):
return self.browser.refresh()
def elementDump(self,element,filename=None):
return self.browser.elementDump(element,filename=filename)
def screenshot(self, save_name=None, save_directory=None):
return self.browser.screenshot(save_name=save_name,save_directory=save_directory)
def sourceDump(self, filename=None):
return self.browser.sourceDump(filename=filename)
def record(self,element=None):
if element:
err = self.switchElement(element)
if err:
logging.error("Failed to Select Element to Record")
return 1
else:
self.switchElement()
err = self.browser.record(self.selectedElement)
return err
def storeRecorder(self,filename=None,directory=None):
err = self.browser.storeRecorder(filename=filename,directory=directory)
return err
def loadRecorder(self,filename=None,directory=None):
err = self.browser.loadRecorder(filename,directory=directory)
return err
def playback(self):
err = self.browser.playback()
return err
def recordButton(self,element=None):
err = self.browser.recordButton(element=element)
return err
def getRecordedElements(self):
return self.browser.getRecordedElements()
def deleteRecord(self):
return self.browser.deleteRecord()
def clearRecorder(self):
return self.browser.clearRecorder()
if __name__ == "__main__":
argv = sys.argv[1:]
if(argv):
filename = argv[0]
else:
filename = 'config.json'
directory = os.getcwd()
file_directory = "{}/{}".format(directory,filename)
if(not os.path.exists(file_directory)):
logging.critical("{} is not in {}".format(filename,directory))
exit(1)