Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
isolated doctest setup/teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
agroszer committed May 9, 2013
1 parent 856bdfc commit ecc94ee
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 54 deletions.
68 changes: 27 additions & 41 deletions src/z3c/webdriver/testing.py
Expand Up @@ -14,13 +14,7 @@
import time
import logging
import re
import sys
import os
import pprint
import subprocess
import urllib
from tempfile import mkdtemp
import shutil

UPLOAD_FILE_TEMPFOLDER = ''

Expand Down Expand Up @@ -236,38 +230,30 @@ def setUpDoctest(test):
driver.delete_all_cookies()


#def setUpDoctest(test):
# if WSGISERVER is None:
# start_server(test)
# test.stopWSGIServerOnTearDown = True
# else:
# test.stopWSGIServerOnTearDown = False
#
# if WEBDRIVER is None:
# args = ['--remote-debugger-port=9011']
# driver = test.globs['driver'] = create_phantom_driver(args)
# test.stopWebDriverOnTearDown = True
# else:
# driver = test.globs['driver'] = WEBDRIVER
# # XXX, big one:
# # phantomjs behaves as ONE browser instance
# # that means cookies and ANY state is kept between invocations
# # unless the driver is stopped and started again
# # here we're trying to clear at least the cookies
# # OTOH, starting and stopping phantomjs takes around 1.5 secs
# driver.delete_all_cookies()
# test.stopWebDriverOnTearDown = False
# # test.globs['browser'] = browser.SeleniumBrowser(test.globs['driver'])
#
# def wait():
# while driver.execute_script("return (document.ajaxRequestsInProcess | 0)") != 0:
# time.sleep(0.01)
#
# test.globs['waitForAjax'] = wait
#
#
#def tearDownDoctest(test):
# if test.stopWebDriverOnTearDown:
# test.globs['driver'].quit()
# if test.stopWSGIServerOnTearDown:
# stop_server(test)
def setUpIsolatedDoctest(test, wsgiServerFactory=None, driverFactory=None):
if wsgiServerFactory is not None:
test.globs['wsgi'] = wsgiServerFactory(test)
test.stopWSGIServerOnTearDown = True
else:
test.wsgi = None
test.stopWSGIServerOnTearDown = False

if driverFactory is not None:
driver = test.globs['driver'] = driverFactory(test)
test.stopWebDriverOnTearDown = True
else:
driver = test.globs['driver'] = WEBDRIVER
test.stopWebDriverOnTearDown = False

def wait():
while driver.execute_script("return (document.ajaxRequestsInProcess | 0)") != 0:
time.sleep(0.01)

test.globs['waitForAjax'] = wait


def tearDownIsolatedDoctest(test):
if test.stopWebDriverOnTearDown:
test.globs['driver'].quit()
if test.stopWSGIServerOnTearDown:
test.globs['wsgi'].quit()
37 changes: 24 additions & 13 deletions src/z3c/webdriver/tests/tests.py
Expand Up @@ -15,9 +15,9 @@
import os
import os.path
import doctest
import unittest
import sys
import time
import unittest
from zope.testing import renormalizing

from selenium.webdriver import PhantomJS
Expand Down Expand Up @@ -350,13 +350,16 @@ def wsgiServerFactory(layer):

PHANTOM_RE = re.compile("'phantomjs': '(.*?)'", re.I+re.M+re.S)

def driverFactory(layer):

def phantomJSdriverFactory(layer):
args = ['--remote-debugger-port=9010']
args = []
here = os.path.dirname(__file__)
if sys.platform.startswith('win'):
# bloody windows cannot shut child processes
# and neither supports subprocess.execve
# so we need to directly execute phantomjs.exe
# that we can dig out of the phantomjs-script.py
binrunner = os.path.join(here, '..', '..', '..', '..', 'bin', 'phantomjs-script.py')
else:
binrunner = os.path.join(here, '..', '..', '..', '..', 'bin', 'phantomjs')
Expand All @@ -365,44 +368,42 @@ def driverFactory(layer):
target = m.group(1)
if sys.platform.startswith('win'):
target = target.replace('\\\\', '\\')

rv = PhantomJS(target, service_args=args)
time.sleep(0.5)
time.sleep(0.1)
return rv


def setUpIsolatedReadme(test):
z3c.webdriver.testing.setUpIsolatedDoctest(
test, wsgiServerFactory=wsgiServerFactory,
driverFactory=phantomJSdriverFactory)


def test_suite():
optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
layer = z3c.webdriver.testing.SeleniumTestLayer(
z3c.webdriver,
wsgiServerFactory=wsgiServerFactory,
driverFactory=driverFactory)
driverFactory=phantomJSdriverFactory)

readme = doctest.DocFileSuite('../README.txt',
#setUp=setUpWSGITestApplication,
#tearDown=tearDownWSGITestApplication,
setUp=z3c.webdriver.testing.setUpDoctest,
optionflags=optionflags,
checker=checker)
readme.layer = layer

ajax = doctest.DocFileSuite('ajax.txt',
#setUp=setUpWSGITestApplication,
#tearDown=tearDownWSGITestApplication,
setUp=z3c.webdriver.testing.setUpDoctest,
optionflags=optionflags,
checker=checker)
ajax.layer = layer
browsert = doctest.DocFileSuite('browser.txt',
#setUp=setUpWSGITestApplication,
#tearDown=tearDownWSGITestApplication,
setUp=z3c.webdriver.testing.setUpDoctest,
optionflags=optionflags,
checker=checker)
browsert.layer = layer
controlst = doctest.DocFileSuite('controls.txt',
#setUp=setUpWSGITestApplication,
#tearDown=tearDownWSGITestApplication,
setUp=z3c.webdriver.testing.setUpDoctest,
optionflags=optionflags,
checker=checker)
Expand All @@ -412,12 +413,22 @@ def test_suite():
optionflags=optionflags,
checker=checker)

# isolated readme
readmeiso = doctest.DocFileSuite(
'../README.txt',
setUp=setUpIsolatedReadme,
tearDown=z3c.webdriver.testing.tearDownIsolatedDoctest,
optionflags=optionflags,
checker=checker)


return unittest.TestSuite((
ajax,
readme,
browsert,
servert,
controlst,
readmeiso
))


Expand Down

0 comments on commit ecc94ee

Please sign in to comment.