1

I currently have an app that crudely posts information from a registration page to my admin page. The posted information is in a table. I would like to make a test that actually checks whether any information has been picked from my registration form and posted to the table in my admin page

app.py code

@app.route('/register_user')
def register_user():
    return render_template('register_user.html')

@app.route('/manage_admin', methods=['POST', 'GET'])
def manage_admin():
    if request.method == 'POST':
        manage_admin = request.form
    return render_template('manage_admin.html', manage_admin=manage_admin)

register_user.html

<form id="form-login" method="post" action="/manage_admin">

        <input type="text" class="input100" name="name" placeholder="fullname">
        <br>
        <br>
        <input type="text" class="input100" name="username" placeholder=" username">
        <br>
        <br>
        <input type="text" class="input100" name="email" placeholder="email">
        <br>
        <br>
        <input type="password" class="input100" name="password" placeholder="password">
        <br>
        <br>
        <br>
        <input type="submit" class="login-button"  value="Submit">
</form>

manage_admin.html

{% for key, value in manage_admin.items() %}
    <th>{{ value }}</th>
{% endfor %}
stamaimer
  • 6,227
  • 5
  • 34
  • 55
myrdstom
  • 156
  • 3
  • 15

2 Answers2

4

I'd recommend using something like Selenium to run automated tests. This will let you perform tests against a live instance of your application.

To get started, install Selenium:

pip install selenium

Then, create a script to run your tests, by importing the Selenium Webdriver along with unittest from the Python standard library:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class TestRegistrationPage(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()

    def tearDown(self):
        self.driver.quit()

    def test_form_posts_to_admin_page(self):
        self.driver.get('http://localhost:5000/register_user')  # Assuming default Flask port

        # Retrieve input elements
        name_input = self.driver.find_element_by_name('name')
        username_input = self.driver.find_element_by_name('username')
        email_input = self.driver.find_element_by_name('email')
        password_input = self.driver.find_element_by_name('password')

        # Populate inputs with dummy text
        name_input.send_keys('Alice Cooper')
        username_input.send_keys('acoop')
        email_input.sendkeys('acoop@schoolsoutforever.com')
        password_input.sendkeys('rockonbaby')

        # Find submit button and submit form by sending an "Enter" keypress
        submit_button = self.driver.find_element_by_class_name('login-button')
        submit_button.send_keys(Keys.ENTER)

        # Check if redirect worked. If you don't redirect from the form page the just use the driver.get method above with the target url instead
        admin_url = self.driver.current_url
        self.assertEqual(admin_url, 'http://localhost:5000/manage_admin')

        # Get table header elements and extract their text values
        table_cells = self.driver.find_elements_by_tag_name('th')
        table_contents = []
        for cell in table_cells:
            table_contents.append(cell.text)

        # Check if dummy text made it into the table contents
        self.assertIn('Alice Cooper', table_contents)
        self.assertIn('acoop', table_contents)
        self.assertIn('acoop@schoolsoutforever.com', table_contents)
        self.assertIn('rockonbaby', table_contents)

# Tell unit tests to run when you run the script
if __name__ == '__main__':
    unittest.main()

If you encounter any issues with the elements not being found, it could be a page load time issue. In this case, you may need to force a wait if rerunning the tests don't work. For info on doing so, see this answer and the Selenium Waits documentation.

Jon Badiali
  • 166
  • 6
4

Simple solution, if you can parse the html yourself:

app.test_client().post('/manage_admin', data={'name': 'John Doe', 'username': 'jdoe', ...}))

It returns a Response, from which you can get the html, and check the contents however you like. I've used python's built-in parser for example.

jsharp
  • 565
  • 2
  • 14