from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from time import sleep from selenium import webdriver class TestUserLogin(object): def __init__(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() #self.driver.quit() #登录成功 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() |
from test_user_login import * if __name__ == '__main__': case=TestUserLogin() case.test_user_logon_username_error() case.test_user_logon_username_pass() |
from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from time import sleep import unittest from selenium import webdriver class TestUserLogin(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver=webdriver.Chrome() cls.driver.get('http://localhost:8888/jpress/user/login') cls.driver.maximize_window() # def __init__(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 self.assertEqual(alert.text,expected) alert.accept() #self.driver.quit() 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__': unittest.main() |
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() #self.driver.quit() 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() |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |