Selenium web driver hangs while trying to test a Flask app

Viewed 271

I am trying to write an automated test for my web app and I encountering a weird issue;

when trying to call driver.get('https://my-local-app-url:port') Chrome just hangs and never really opens the URL. However, when using the driver.get() on Google.com it works just fine.

This is how I have everything set up:

The "test":

class TestApp:
def test_app(self, chrome_browser, app):
    chrome_browser.get('https://google.com') << this line execute just fine
    app.run(host='127.0.0.1', port=8000)
    chrome_browser.get('http://127.0.0.1:8000/') << this one does not execute

conftest.py:

@pytest.fixture(scope="session")
def chrome_browser():
    browser = webdriver.Chrome()
    return browser

@pytest.fixture(scope="session")
def app():
    app = create_app()
    return app

The create_app function:

def create_app():
    app = Flask(__name__)
    app.register_blueprint(blueprint)
    return app

*Outside the test, the function create_app is working just fine and returning a Flask object. *My Chrome driver and chrome version are 86.04240.22.

1 Answers

I suppose, that app.run() call is blocking and all code after that line will be executed only after flask server stops. You could start flask in a separate thread with some restrictions using app.run(debug=True, use_reloader=False), but more logical approach would be implementing selenium integration tests in separate classes and start them as a separate processes.

Related