import unittest
from selenium import webdriver class LoginTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def test_login(self): # 打开登录页面 self.driver.get("http://example.com/login") # 输入用户名和密码 username_field = self.driver.find_element_by_id("username") username_field.send_keys("testuser") password_field = self.driver.find_element_by_id("password") password_field.send_keys("testpassword") # 点击登录按钮 login_button = self.driver.find_element_by_id("login-button") login_button.click() # 检查是否成功登录 welcome_message = self.driver.find_element_by_id("welcome-message") self.assertEqual("Welcome, testuser!", welcome_message.text) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
这是一个使用Selenium库编写的简单的自动化测试用例。它打开了一个名为"http://example.com/login"的登录页面,输入了用户名和密码,并点击了登录按钮。然后,它检查欢迎消息是否正确显示,以验证是否成功登录