简体   繁体   English

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

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

I have a login page for a flask app with cloud database, I want to test the results after logging in, specifically, I want to test the HTML elements after logging in. I have seen people test return status code or using assertIn to check if data exist.我有一个带有云数据库的 flask 应用程序的登录页面,我想在登录后测试结果,具体来说,我想在登录后测试 HTML 元素。我见过人们测试返回状态代码或使用assertIn来检查是否数据存在。

Is there a way for me to target a specific HTML tag, like <h1 id="userTitle"> </h1> from rendered templates after POST username, password to the route function login()有没有办法让我定位一个特定的 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) 

In my rendered jinja2 template I have this which is after logging in.在我渲染的 jinja2 模板中,我有这个是登录后的。

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

I guess assertIn works well, but I just want to know how to test an HTML tag without running a browser test.我猜 assertIn 效果很好,但我只想知道如何在不运行浏览器测试的情况下测试 HTML 标记。

Although I didn't get a correct answer from here, but I just managed to do the unit-test with just assertIn , by checking the contents of the page.虽然我没有从这里得到正确的答案,但我只是通过检查页面内容设法用assertIn进行了单元测试。
Thanks everyone感谢大家

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

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