2

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.

AcroTom
  • 71
  • 6

1 Answers1

1

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.

Alexandra Dudkina
  • 4,302
  • 3
  • 15
  • 27
  • Thank you! What should I do if I want to the Flask server running only once executing the tests? Does it make sense to call app.run() in the conftest file? – AcroTom Oct 10 '20 at 20:34
  • You can find quite detailed tutorial for setting up flask and selenium e.g. [here](https://scotch.io/tutorials/test-a-flask-app-with-selenium-webdriver-part-1). – Alexandra Dudkina Oct 11 '20 at 06:26
  • Thank you, Alexandra. Unfortunately, it is not working, I posted about the issue here: https://stackoverflow.com/questions/64305197/getting-a-cant-pickle-local-object-liveservertestcase-error-when-trying-to If you have any ideas how to overcome this it will be great, I could not find anything online. – AcroTom Oct 12 '20 at 10:00