forked from rkaippully/play-webdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
187 lines (164 loc) · 6.27 KB
/
commands.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
#
# WebDrive - Selenium 2 WebDriver support for play framework
#
# Copyright (C) 2011 Raghu Kaippully
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import subprocess
import sys
import urllib2
from play.utils import *
COMMANDS = [ 'webdrive:test', 'bambooify' ]
HELP = {
'webdrive:test': 'Run tests using Selenium 2 WebDriver',
'bambooify': 'Converts the html reuslt in JUnit-XML-Format'
}
def execute(**kargs):
command = kargs.get("command")
app = kargs.get("app")
args = kargs.get("args")
if command == 'webdrive:test':
test(app, args)
elif command == 'bambooify':
generateJUnitXML(app, args)
def generateJUnitXML(app, args):
app.check()
# If framework-id is not a valid test-id, force it to 'test'
if not isTestFrameworkId(app.play_env["id"]):
app.play_env["id"] = 'test'
print "~ Generate JUnit xml files from webdriver test module"
print "~ Ctrl+C to stop"
print "~ "
print "~ "
# Kill if exists
http_port = 9000
protocol = 'http'
if app.readConf('https.port'):
http_port = app.readConf('https.port')
protocol = 'https'
else:
http_port = app.readConf('http.port')
try:
proxy_handler = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy_handler)
opener.open('http://localhost:%s/@kill' % http_port)
except Exception, e:
pass
# Run app
test_result = os.path.join(app.path, 'test-result')
wdcp = app.getClasspath()
cp_args = ':'.join(wdcp)
if os.name == 'nt':
cp_args = ';'.join(wdcp)
java_cmd = [app.java_path(), '-classpath', cp_args, 'play.modules.webdrive.SeleniumTests2JUnitXML']
java_cmd_str = " ".join(java_cmd)
try:
play_process = subprocess.Popen(java_cmd_str, env=os.environ, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
while True:
#next_line = play_process.communicate()[0]
next_line = play_process.stdout.readline()
if next_line == '' and play_process.poll() != None:
break
sys.stdout.write(next_line)
sys.stdout.flush()
print 'bambooify is ready'
except OSError:
print "Could not execute the java executable, please make sure the JAVA_HOME environment variable is set properly (the java executable should reside at JAVA_HOME/bin/java). "
sys.exit(-1)
def test(app, args):
app.check()
# If framework-id is not a valid test-id, force it to 'test'
if not isTestFrameworkId(app.play_env["id"]):
app.play_env["id"] = 'test'
print "~ Running tests with webdriver"
print "~ Ctrl+C to stop"
print "~ "
print "~ Deleting %s" % os.path.normpath(os.path.join(app.path, 'tmp'))
if os.path.exists(os.path.join(app.path, 'tmp')):
shutil.rmtree(os.path.join(app.path, 'tmp'))
print "~"
# Kill if exists
http_port = 9000
protocol = 'http'
if app.readConf('https.port'):
http_port = app.readConf('https.port')
protocol = 'https'
else:
http_port = app.readConf('http.port')
try:
proxy_handler = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy_handler)
opener.open('http://localhost:%s/@kill' % http_port)
except Exception, e:
pass
# Run app
test_result = os.path.join(app.path, 'test-result')
if os.path.exists(test_result):
shutil.rmtree(test_result)
sout = open(os.path.join(app.log_path(), 'system.out'), 'w')
java_cmd = app.java_cmd(args)
try:
play_process = subprocess.Popen(java_cmd, env=os.environ, stdout=sout)
except OSError:
print "Could not execute the java executable, please make sure the JAVA_HOME environment variable is set properly (the java executable should reside at JAVA_HOME/bin/java). "
sys.exit(-1)
soutint = open(os.path.join(app.log_path(), 'system.out'), 'r')
while True:
if play_process.poll():
print "~"
print "~ Oops, application has not started?"
print "~"
sys.exit(-1)
line = soutint.readline().strip()
if line:
print line
if line.find('Listening for HTTP') > -1:
soutint.close()
break
# Run WebDriverRunner
print "~"
wdcp = app.getClasspath()
cp_args = ':'.join(wdcp)
if os.name == 'nt':
cp_args = ';'.join(wdcp)
seleniumStatus = ''.join(args)
if len(seleniumStatus) == 0:
seleniumStatus = '-D'
java_cmd = [app.java_path(), '-classpath', cp_args,
seleniumStatus,
'-Dwebdrive.classes=%s' % app.readConf('webdrive.classes'),
'-Dwebdrive.timeout=%s' % app.readConf('webdrive.timeout'),
'-Dapplication.url=%s://localhost:%s' % (protocol, http_port),
'play.modules.webdrive.WebDriverRunner']
try:
subprocess.call(java_cmd, env=os.environ)
except OSError:
print "Could not execute web driver."
sys.exit(-1)
print "~"
# Kill if exists
http_port = app.readConf('http.port')
try:
proxy_handler = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy_handler)
opener.open('%s://localhost:%s/@kill' % (protocol, http_port))
except Exception, e:
pass
if os.path.exists(os.path.join(app.path, 'test-result/result.passed')):
print "~ All tests passed"
print "~"
testspassed = True
if os.path.exists(os.path.join(app.path, 'test-result/result.failed')):
print "~ Some tests have failed. See file://%s for results" % test_result
print "~"
sys.exit(1)