The test runner also launches an HTTP server.

The server runs on port 9180 and serves the content from test/www
directory. Any tests can use this base URL to load test content and
to exercise certain PhantomJS API.

https://github.com/ariya/phantomjs/issues/12439
2.0
Ariya Hidayat 2014-09-05 08:39:51 -07:00
parent 235d072db5
commit 784a06c3a7
3 changed files with 110 additions and 2 deletions

View File

@ -0,0 +1,13 @@
var assert = require('../../assert');
var webpage = require('webpage');
var page = webpage.create();
assert.typeOf(page, 'object');
page.open('http://localhost:9180/hello.html', function (status) {
assert.equal(status, 'success');
assert.typeOf(page.title, 'string');
assert.equal(page.title, 'Hello');
assert.typeOf(page.plainText, 'string');
assert.equal(page.plainText, 'Hello, world!');
});

View File

@ -1,20 +1,30 @@
#!/usr/bin/env python
import inspect
import optparse
import os
import posixpath
import SimpleHTTPServer
import SocketServer
import socket
import subprocess
import sys
import threading
import time
import urllib
TIMEOUT = 35 # Maximum duration of PhantomJS execution (in seconds)
HTTP_PORT = 9180
http_running = False
TESTS = [
'basics/exit.js',
'basics/global.js',
'basics/onerror.js',
'basics/stacktrace.js',
'basics/version.js',
'module/webpage/open.js',
'module/system/system.js',
'module/system/args.js',
'module/system/os.js',
@ -28,6 +38,78 @@ TESTS = [
'run-tests.js'
]
class FileHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
# silent, do not pollute stdout nor stderr.
def log_message(self, format, *args):
return
# modified version of SimpleHTTPRequestHandler's translate_path
# to resolve the URL relative to the www/ directory
# (e.g. /foo -> test/www/foo)
def translate_path(self, path):
path = path.split('?', 1)[0]
path = path.split('#', 1)[0]
path = posixpath.normpath(urllib.unquote(path))
words = path.split('/')
words = filter(None, words)
path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
path += '/www'
for word in words:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir):
continue
path = os.path.join(path, word)
return path
def run_httpd():
global http_running
handler = FileHandler
handler.extensions_map.update({
'.htm': 'text/html',
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json'
})
try:
httpd = SocketServer.TCPServer(('', HTTP_PORT), handler)
while http_running:
httpd.handle_request()
except socket.error:
print 'Fatal error: unable to launch a test server at port', HTTP_PORT
print 'Check that the port is not already used!'
http_running = False
sys.exit(1)
return
def setup_server():
global http_running
http_running = True
httpd_thread = threading.Thread(target=run_httpd)
httpd_thread.start()
time.sleep(2)
if http_running:
if options.verbose:
print 'serving at port', HTTP_PORT
def terminate_server():
global http_running
http_running = False
# ping the server once to trigger the check for http_running (after every request)
urllib.urlopen('http://localhost:{0}'.format(HTTP_PORT))
def init():
global base_path, phantomjs_exe, options
@ -79,7 +161,7 @@ def run_phantomjs(script, args=[]):
def run_test(filename):
script = os.path.normpath(base_path + '/' + filename)
args = [];
args = []
if options.verbose:
args.append('--verbose')
@ -99,6 +181,10 @@ def run_test(filename):
def run_tests():
setup_server()
if not http_running:
return
start = time.time()
if options.verbose:
print 'Starting the tests...'
@ -115,8 +201,9 @@ def run_tests():
if result == 0:
print
print 'No failure. Finished in %d seconds.' % (time.time() - start)
return result
terminate_server()
return result
init()

8
test/www/hello.html Normal file
View File

@ -0,0 +1,8 @@
<html>
<head>
<title>Hello</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>