我目前有一个应用程序粗略地将信息从注册页面发布到我的管理页面。发布的信息在表格中。我想做一个测试,实际检查是否已从我的注册表中挑选任何信息并将其发布到我的管理页面中的表格
app.py代码
grp <- inverse.rle(within.list(rle(df1$term), values <- seq_along(values)))
df1$sum_slope <- with(df1, ave(slope * (NA^!term), grp, FUN = sum))
df1$sum_slope
#[1] 1.30 1.30 NA NA NA NA 5.47 5.47 5.47 NA 0.80 NA
register_user.html
@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)
manage_admin.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>
答案 0 :(得分:2)
简单的解决方案,如果你可以自己解析html:
app.test_client().post('/manage_admin', data={'name': 'John Doe', 'username': 'jdoe', ...}))
它会返回Response,您可以从中获取html,然后检查您喜欢的内容。我使用了python的built-in parser作为例子。
答案 1 :(得分:1)
我建议使用Selenium之类的内容来运行自动化测试。这将允许您针对应用程序的实时实例执行测试。
要开始使用,请安装Selenium:
pip install selenium
然后,通过从Python标准库导入Selenium Webdriver和unittest
来创建运行测试的脚本:
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()
如果您遇到任何未找到元素的问题,可能是页面加载时间问题。在这种情况下,如果重新运行测试不起作用,您可能需要强制等待。有关这样做的信息,请参阅this answer和Selenium Waits documentation。