# Path: viceroy/api.py
# class ViceroyTestCase(unittest.TestCase):
#     viceroy_url = '/'
#     viceroy_timeout = 30
#     viceroy_driver_class = None
# 
#     @classmethod
#     @contextlib.contextmanager
#     def viceroy_server(cls):
#         raise NotImplementedError()
# 
#     @classmethod
#     @contextlib.contextmanager
#     def viceroy_driver(cls):
#         if isinstance(cls.viceroy_driver_class, webdriver.Remote):
#             driver_class = cls.viceroy_driver_class
#         elif isinstance(cls.viceroy_driver_class, str):
#             driver_class = getattr(webdriver, cls.viceroy_driver_class)
#         elif 'SELENIUM_WEBDRIVER' in os.environ:
#             driver_class = getattr(webdriver, os.environ['SELENIUM_WEBDRIVER'])
#         else:
#             driver_class = webdriver.Firefox
#         driver = driver_class()
#         try:
# 
#             yield driver
#         finally:
#             driver.quit()
# 
#     @classmethod
#     def viceroy_get_results(cls):
#         with cls.viceroy_server() as port:
#             with cls.viceroy_driver() as driver:
#                 driver.get(
#                     'http://localhost:{}{}'.format(port, cls.viceroy_url)
#                 )
#                 WebDriverWait(
#                     driver,
#                     cls.viceroy_timeout
#                 ).until(
#                     lambda _: driver.execute_script(
#                         'return window.VICEROY && window.VICEROY.DONE;'
#                     )
#                 )
#                 return driver.execute_script('return window.VICEROY.RESULTS;')
# 
#     @classmethod
#     def setUpClass(cls):
#         super(ViceroyTestCase, cls).setUpClass()
#         cls.viceroy_cache = cls.viceroy_get_results()
# 
#     @classmethod
#     def tearDownClass(cls):
#         if hasattr(cls, 'viceroy_cache'):
#             del cls.viceroy_cache
#         super(ViceroyTestCase, cls).tearDownClass()


import contextlib
import logging
import multiprocessing
import socket
import time
from flask.ext.testing import TestCase
from viceroy.api import ViceroyTestCase

    @classmethod
    def viceroy_get_flask_app(cls):
        return cls.viceroy_flask_app

    @classmethod
    @contextlib.contextmanager
    def viceroy_server(cls):
        app = cls.viceroy_get_flask_app()
        app.config['TESTING'] = True
        app.config['VICEROY_TESTING'] = True
        port = cls.viceroy_flask_port
        app.config['LIVESERVER_PORT'] = port
        cls.viceroy_flask_process = None
        try:
            cls._start_flask_server(app)
            yield port
        finally:
            cls._stop_flask_server()

    @classmethod
    def _start_flask_server(cls, app):
        cls.viceroy_flask_process = multiprocessing.Process(
            target=run_app, kwargs={
                'app': app,
                'port': cls.viceroy_flask_port,
                'silent': cls.viceroy_flask_silent
            }
        )
        cls.viceroy_flask_process.start()

# Path: viceroy/api.py
# class ViceroyTestCase(unittest.TestCase):
#     viceroy_url = '/'
#     viceroy_timeout = 30
#     viceroy_driver_class = None
# 
#     @classmethod
#     @contextlib.contextmanager
#     def viceroy_server(cls):
#         raise NotImplementedError()
# 
#     @classmethod
#     @contextlib.contextmanager
#     def viceroy_driver(cls):
#         if isinstance(cls.viceroy_driver_class, webdriver.Remote):
#             driver_class = cls.viceroy_driver_class
#         elif isinstance(cls.viceroy_driver_class, str):
#             driver_class = getattr(webdriver, cls.viceroy_driver_class)
#         elif 'SELENIUM_WEBDRIVER' in os.environ:
#             driver_class = getattr(webdriver, os.environ['SELENIUM_WEBDRIVER'])
#         else:
#             driver_class = webdriver.Firefox
#         driver = driver_class()
#         try:
# 
#             yield driver
#         finally:
#             driver.quit()
# 
#     @classmethod
#     def viceroy_get_results(cls):
#         with cls.viceroy_server() as port:
#             with cls.viceroy_driver() as driver:
#                 driver.get(
#                     'http://localhost:{}{}'.format(port, cls.viceroy_url)
#                 )
#                 WebDriverWait(
#                     driver,
#                     cls.viceroy_timeout
#                 ).until(
#                     lambda _: driver.execute_script(
#                         'return window.VICEROY && window.VICEROY.DONE;'
#                     )
#                 )
#                 return driver.execute_script('return window.VICEROY.RESULTS;')
# 
#     @classmethod
#     def setUpClass(cls):
#         super(ViceroyTestCase, cls).setUpClass()
#         cls.viceroy_cache = cls.viceroy_get_results()
# 
#     @classmethod
#     def tearDownClass(cls):
#         if hasattr(cls, 'viceroy_cache'):
#             del cls.viceroy_cache
#         super(ViceroyTestCase, cls).tearDownClass()


# Path: viceroy/contrib/flask.py
import contextlib
import logging
import multiprocessing
import socket
import time
from flask.ext.testing import TestCase
from viceroy.api import ViceroyTestCase


    @classmethod
    def viceroy_get_flask_app(cls):
        return cls.viceroy_flask_app

    @classmethod
    @contextlib.contextmanager
    def viceroy_server(cls):
        app = cls.viceroy_get_flask_app()
        app.config['TESTING'] = True
        app.config['VICEROY_TESTING'] = True
        port = cls.viceroy_flask_port
        app.config['LIVESERVER_PORT'] = port
        cls.viceroy_flask_process = None
        try:
            cls._start_flask_server(app)
            yield port
        finally:
            cls._stop_flask_server()

    @classmethod
    def _start_flask_server(cls, app):
        cls.viceroy_flask_process = multiprocessing.Process(
            target=run_app, kwargs={
                'app': app,
                'port': cls.viceroy_flask_port,
                'silent': cls.viceroy_flask_silent
            }
        )
        cls.viceroy_flask_process.start()

        while not _server_started(cls.viceroy_flask_port):