繁体   English   中英

在 flask 路由模板单元测试中测试 html 元素

[英]Testing the html elements in flask routing templates unittest

我有一个带有云数据库的 flask 应用程序的登录页面,我想在登录后测试结果,具体来说,我想在登录后测试 HTML 元素。我见过人们测试返回状态代码或使用assertIn来检查是否数据存在。

有没有办法让我定位一个特定的 HTML 标签,比如<h1 id="userTitle"> </h1>在 POST 用户名后渲染的模板,密码到路由 function login()

    def test_users_login(self):
        result = self.app.post('/login', data=dict(username='Nicole', password='abc123'), follow_redirects=True)

        # I want to check the HTML tag's text value data after logging in
        self.assertEqual(result.data.getTag("h1", b"Nicole") #What I imagined using <h1>
        self.assertEqual(result.data.getId("user", b"Nicole") #What I imagined using id


        #This returns true which is okay, because 'Nicole' exists in the whole page
        self.assertIn(b'Nicole', result.data) 

在我渲染的 jinja2 模板中,我有这个是登录后的。

 <h1 id="userTitle">{{ session['username'] }},Welcome!</h1>

我猜 assertIn 效果很好,但我只想知道如何在不运行浏览器测试的情况下测试 HTML 标记。

虽然我没有从这里得到正确的答案,但我只是通过检查页面内容设法用assertIn进行了单元测试。
感谢大家

暂无
暂无

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

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