-
Notifications
You must be signed in to change notification settings - Fork 6
/
prepare_browser.py
66 lines (52 loc) · 2.03 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
57
58
59
60
61
62
63
64
65
66
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)
for count in range(0, 10):
cmd = "curl http://localhost:4444/grid/console > /dev/null 2>&1"
try:
subprocess.check_call(cmd, shell=True)
break
except Exception:
time.sleep(2)
else:
del_selenium_standalone()
raise RuntimeError("The selenium standalone server is not ready!")
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 in ['ie', 'edge']:
raise ValueError(
"{} mode doesn't support Windows browser".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()