from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from time import sleep from selenium import webdriver import pytest class TestUserLogin(object): def setup_class(self): self.driver=webdriver.Chrome() self.driver.get('http://localhost:8888/jpress/user/login') self.driver.maximize_window() #用户名、密码错误 def test_user_logon_username_error(self): #测试数据 username='' pwd='123456' expected='账号不能为空' self.driver.find_element_by_name('user').send_keys(username) self.driver.find_element_by_name('pwd').send_keys(pwd) self.driver.find_element_by_class_name('btn').click() WebDriverWait(self.driver,5).until(EC.alert_is_present()) alert=self.driver.switch_to.alert sleep(3) assert alert.text==expected alert.accept() def test_user_logon_username_pass(self): #测试数据 username='wx1' pwd='123456' expected='用户中心' self.driver.find_element_by_name('user').send_keys(username) self.driver.find_element_by_name('pwd').clear() self.driver.find_element_by_name('pwd').send_keys(pwd) self.driver.find_element_by_class_name('btn').click() WebDriverWait(self.driver,5).until(EC.title_is(expected)) sleep(3) assert self.driver.title==expected self.driver.quit() if __name__ == '__main__': pytest.main() |
login_data=[ ('','123456','账号不能为空') , ('wx1','123456','用户中心') ] @pytest.mark.parametrize('username,pwd,expected',login_data) |
from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from time import sleep from selenium import webdriver import pytest class TestUserLogin(object): #测试数据 login_data=[ ('','123456','账号不能为空') , ('wx1','123456','用户中心') ] def setup_class(self): self.driver=webdriver.Chrome() self.driver.get('http://localhost:8888/jpress/user/login') self.driver.maximize_window() #测试用户登录 @pytest.mark.parametrize('username,pwd,expected',login_data) #实现登录 def test_user_logon_username_pass(self,username,pwd,expected): self.driver.find_element_by_name('user').clear() self.driver.find_element_by_name('user').send_keys(username) self.driver.find_element_by_name('pwd').clear() self.driver.find_element_by_name('pwd').send_keys(pwd) self.driver.find_element_by_class_name('btn').click() #断言判断 if username=='': WebDriverWait(self.driver, 5).until(EC.alert_is_present()) alert = self.driver.switch_to.alert sleep(3) assert alert.text == expected alert.accept() else: WebDriverWait(self.driver,5).until(EC.title_is(expected)) sleep(3) assert self.driver.title==expected self.driver.quit() if __name__ == '__main__': pytest.main() |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |