简体   繁体   English

Pytest 在测试中出现警报提示

[英]Pytest in testing an alert prompt

I am trying to delete post in a test that fails with error: NameError: name 'window' is not defined.我正在尝试删除失败并出现错误的测试中的帖子:NameError: name 'window' is not defined。 How can I Implement the alert prompt and choose ok, then return to homepage(/)?如何实现警报提示并选择确定,然后返回主页(/)?

"""Delete Post"""

def test_delete(client, auth, app):
    auth.login()
    response = client.post('/1/delete')
    assert window.alert("Are you sure?");
    assert response.headers["Location"] == "/"

    with app.app_context():
        db = get_db()
        post = db.execute('SELECT * FROM post WHERE id = 1').fetchone()
        assert post is None```

Looks like you're trying to test JS window.alert in pytest.看起来您正在尝试在 pytest 中测试 JS window.alert Unfortunately this is not possible.不幸的是,这是不可能的。 JS behavior should be tested with JS tools like Jest. JS 行为应该使用 Jest 等 JS 工具进行测试。 Pytest can only strictly test backend behaviors and when testing template rendering, the JS portion of the template is not executed. Pytest 只能严格测试后端行为,在测试模板渲染时,模板的 JS 部分不执行。

It's also possible to test both, backend and frontend behavior, with end-to-end test tools like Selenium or Cypress.还可以使用 Selenium 或 Cypress 等端到端测试工具同时测试后端和前端行为。

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

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