diff --git a/test/module/webpage/open.js b/test/module/webpage/open.js new file mode 100644 index 00000000..ac53358b --- /dev/null +++ b/test/module/webpage/open.js @@ -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!'); +}); diff --git a/test/run-tests.py b/test/run-tests.py index ea1bcc85..1c88955d 100755 --- a/test/run-tests.py +++ b/test/run-tests.py @@ -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() diff --git a/test/www/hello.html b/test/www/hello.html new file mode 100644 index 00000000..ee4bc592 --- /dev/null +++ b/test/www/hello.html @@ -0,0 +1,8 @@ + + + Hello + + +

Hello, world!

+ +