-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_browser.py
56 lines (42 loc) · 1.67 KB
/
prepare_browser.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
import subprocess
import os
import yaml
import time
STANDALONE_CONTAINER_NAME = "standalone-browser"
def create_selenium_grid_by_docker_compose():
subprocess.check_call(["docker-compose", "up", "-d"])
# wait for nodes to connect to hub
time.sleep(3)
def del_selenium_grid_by_docker_compose():
subprocess.check_call(["docker-compose", "down"])
def create_selenium_standalone(browser):
if browser not in ['chrome', 'firefox']:
raise ValueError(
"Do not support to create standalone server for {}".format(browser))
cmd = "docker run -d -p 4444:4444 -p 5900:5900 -v /dev/shm:/dev/shm " \
"--name {} selenium/standalone-{}-debug".format(
STANDALONE_CONTAINER_NAME, browser)
subprocess.check_call(cmd, shell=True)
def del_selenium_standalone():
subprocess.check_call(
["docker", "stop", "{}".format(STANDALONE_CONTAINER_NAME)])
subprocess.check_call(
["docker", "rm", "{}".format(STANDALONE_CONTAINER_NAME)])
def setup_browser(mode, browser):
if mode != 'manual' and browser == 'ie':
raise ValueError(
"{} mode doesn't support Internet Explorer".format(mode))
os.environ['BROWSER'] = browser
if mode == 'grid':
create_selenium_grid_by_docker_compose()
os.environ['HUB'] = 'localhost'
elif mode == 'standalone':
create_selenium_standalone(browser)
os.environ['HUB'] = 'localhost'
elif mode == 'manual':
os.environ['HUB'] = yaml.load(open('./config.yml'))['selenium_hub']
def destroy_browser(mode):
if mode == 'grid':
del_selenium_grid_by_docker_compose()
elif mode == 'standalone':
del_selenium_standalone()