casperjs
5.05 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
#!/usr/bin/env python
import os
import shlex
import sys
import logging
# Setup logger
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def resolve(path):
if os.path.islink(path):
path = os.path.join(os.path.dirname(path), os.readlink(path))
return resolve(path)
return path
SUPPORTED_ENGINES = {
'phantomjs' : {
'native_args': [
'cookies-file',
'config',
'debug',
'disk-cache',
'disk-cache-path',
'ignore-ssl-errors',
'load-images',
'load-plugins',
'local-url-access',
'local-storage-path',
'local-storage-quota',
'offline-storage-path',
'offline-storage-quota',
'local-to-remote-url-access',
'max-disk-cache-size',
'output-encoding',
'proxy',
'proxy-auth',
'proxy-type',
'remote-debugger-port',
'remote-debugger-autorun',
'script-encoding',
'script-language',
'ssl-protocol',
'ssl-ciphers',
'ssl-certificates-path',
'ssl-client-certificate-file',
'ssl-client-key-file',
'ssl-client-key-passphrase',
'web-security',
'webdriver',
'webdriver-logfile',
'webdriver-loglevel',
'webdriver-selenium-grid-hub',
'wd',
'w',
],
'env_varname': 'PHANTOMJS_EXECUTABLE',
'default_exec' : 'phantomjs'
},
'slimerjs': {
'native_args': [
'-P',
'-jsconsole',
'-CreateProfile',
'-profile',
'error-log-file',
'user-agent',
'viewport-width',
'viewport-height',
#phantomjs options
'cookies-file',
'config',
'debug',
'disk-cache',
'ignore-ssl-errors',
'load-images',
'load-plugins',
'local-storage-path',
'local-storage-quota',
'local-to-remote-url-access',
'max-disk-cache-size',
'output-encoding',
'proxy',
'proxy-auth',
'proxy-type',
'remote-debugger-port',
'remote-debugger-autorun',
'script-encoding',
'ssl-protocol',
'ssl-certificates-path',
'web-security',
'webdriver',
'webdriver-logfile',
'webdriver-loglevel'
'webdriver-selenium-grid-hub',
'wd',
'w',
],
'env_varname': 'SLIMERJS_EXECUTABLE',
'default_exec' : 'slimerjs',
'native_args_with_space': [
'-P',
'-CreateProfile',
'-profile'
]
},
}
ENGINE = os.environ.get('CASPERJS_ENGINE', 'phantomjs')
ENGINE_ARGS = shlex.split(os.environ.get('ENGINE_FLAGS', ''))
ENGINE_NATIVE_ARGS = []
ENGINE_EXECUTABLE = ''
CASPER_ARGS = []
CASPER_PATH = os.path.abspath(os.path.join(os.path.dirname(resolve(__file__)),
'..'))
SYS_ARGS = sys.argv[1:]
# retrieve the engine name
for arg in SYS_ARGS:
if arg.startswith('--engine='):
ENGINE = arg[9:].lower()
break
if not ENGINE in SUPPORTED_ENGINES:
logger.error('Bad engine name. Only phantomjs and slimerjs are supported')
sys.exit(1)
ENGINE_NATIVE_ARGS = SUPPORTED_ENGINES[ENGINE]['native_args']
ENGINE_NATIVE_ARGS_WITH_SPACE = SUPPORTED_ENGINES[ENGINE].get('native_args_with_space') or []
ENGINE_EXECUTABLE = os.environ.get(SUPPORTED_ENGINES[ENGINE]['env_varname'],
os.environ.get('ENGINE_EXECUTABLE',
SUPPORTED_ENGINES[ENGINE]['default_exec']))
def extract_arg_name(arg):
"parse out any option name"
try:
return arg.split('=', 1)[0].replace('--', '', 1)
except IndexError:
return arg
arg_iter = iter(SYS_ARGS)
for arg in arg_iter:
arg_name = extract_arg_name(arg)
found = False
if arg_name in ENGINE_NATIVE_ARGS:
ENGINE_ARGS.append(arg)
if arg_name in ENGINE_NATIVE_ARGS_WITH_SPACE:
nextArg = next(arg_iter, '')
if nextArg == '' or nextArg.startswith('--'):
logger.error('Fatal: Missing expected value for parameter %s' % (arg_name))
sys.exit(1)
else:
ENGINE_ARGS.append(nextArg)
found = True
if not found and arg_name != 'engine':
CASPER_ARGS.append(arg)
CASPER_COMMAND = [ENGINE_EXECUTABLE]
CASPER_COMMAND.extend(ENGINE_ARGS)
CASPER_COMMAND.extend([
os.path.join(CASPER_PATH, 'bin', 'bootstrap.js'),
'--casper-path=%s' % CASPER_PATH,
'--cli'
])
CASPER_COMMAND.extend(CASPER_ARGS)
try:
os.execvp(CASPER_COMMAND[0], CASPER_COMMAND)
except OSError:
err = os.strerror(2)
logger.error('Fatal: %s; did you install %s?' % (err, ENGINE))
sys.exit(1)