简体   繁体   English

Selenium Web 驱动程序在尝试测试 Flask 应用程序时挂起

[英]Selenium web driver hangs while trying to test a Flask app

I am trying to write an automated test for my web app and I encountering a weird issue;我正在尝试为我的 Web 应用程序编写自动化测试,但遇到了一个奇怪的问题;

when trying to call driver.get('https://my-local-app-url:port') Chrome just hangs and never really opens the URL.当尝试调用driver.get('https://my-local-app-url:port') Chrome 只是挂起并且从未真正打开 URL。 However, when using the driver.get() on Google.com it works just fine.但是,在 Google.com 上使用driver.get()时它工作得很好。

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: 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: create_app 函数:

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. *在测试之外,函数 create_app 工作正常并返回 Flask 对象。 *My Chrome driver and chrome version are 86.04240.22. *我的 Chrome 驱动程序和 chrome 版本是 86.04240.22。

I suppose, that app.run() call is blocking and all code after that line will be executed only after flask server stops.我想,那个app.run()调用是阻塞的,只有在app.run()服务器停止后才会执行该行之后的所有代码。 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.您可以使用app.run(debug=True, use_reloader=False)在具有一些限制的单独线程中启动烧瓶,但更合乎逻辑的方法是在单独的类中实现 selenium 集成测试并将它们作为单独的进程启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM