-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathselenium_client.py
More file actions
65 lines (55 loc) · 2.29 KB
/
selenium_client.py
File metadata and controls
65 lines (55 loc) · 2.29 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
""" Disable user in JIRA via selenium """
from os import environ, makedirs
from os.path import isdir
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
CHROME_DRIVER = '{}/chromedriver'.format(environ['WEB_DRIVER_HOME'])
SCREENSHOT_LOCATION = '{}/selenium'.format(environ['HOME'])
ELEMENT_TIMEOUT = 10
class Selenium(object):
""" Selenium """
def __init__(self, user=None, password=None, headless=True):
if user and password:
self.user = user
self.password = password
else:
self.user = environ['USER']
self.password = environ['PASS']
# Headless option set
self.headless = headless
# Set chrome browser options
self.options = None
self.set_chrome_options()
# initialize the driver
self.driver = webdriver.Chrome(executable_path=CHROME_DRIVER, chrome_options=self.options)
def set_chrome_options(self):
""" Set chrome browser options """
options = webdriver.ChromeOptions()
# set the window size
options.add_argument('window-size=1200x600')
# set the execution to be headless or not
if self.headless:
options.set_headless()
self.options = options
def get_page(self, url):
""" Get web page at url """
self.driver.get(url)
def save_screenshot(self, filename):
""" Save screenshot to log dir """
if not isdir(SCREENSHOT_LOCATION):
makedirs(SCREENSHOT_LOCATION, exist_ok=True)
screenshot = '{}/{}.png'.format(SCREENSHOT_LOCATION, filename)
self.driver.get_screenshot_as_file(screenshot)
print('Screen shot is available here: {}'.format(screenshot))
def wait_for_element_to_click(self, name):
""" Wait for element to be clickable """
try:
WebDriverWait(self.driver, ELEMENT_TIMEOUT) \
.until(EC.element_to_be_clickable((By.ID, name)))
print('Page element "{}" is ready!'.format(name))
except TimeoutException:
print('Loading page element "{}" timed out!'.format(name))
raise