简体   繁体   中英

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.

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()

    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.

 <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.

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.
Thanks everyone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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