今天咱们继续接上上一篇所讲,开始用项目来练手:给出以下图:
测这个登录窗口:其实比较好测,拿最简单容易想到的,就是什么账号密码均正确;账号为空;密码为空;账号未授权;密码错误(账号已授权)等等,那么我们怎么来用selenium来实现呢?
import unittest from selenium.webdriver import Chrome from selenium.webdriver.common.by import By class TestLogin(unittest.TestCase): def setUp(self): """登录的前置:启动浏览器,访问登录页面""" self.driver = Chrome() self.driver.implicitly_wait(10) # 打开前程贷的登录页面 self.driver.get("http://8.129.91.152:8765/Index/login.html") def test_login_pass(self): """登录成功""" # 输入账号 self.driver.find_element(By.XPATH, '//input[@placeholder="手机号"]').send_keys('18684720553') # 输入密码 self.driver.find_element(By.XPATH, '//input[@placeholder="密码"]').send_keys('python') # 点击登录 self.driver.find_element(By.XPATH, "//button[text()='登录']").click() # 断言是否成功 try: ele = self.driver.find_element(By.XPATH, '//a[text()="我的帐户[python]"]') except: ele = False self.assertTrue(ele)def test_login_pwd_is_none(self): """密码为空""" # 输入账号和密码 self.driver.find_element(By.XPATH, '//input[@placeholder="手机号"]').send_keys('18684720553') self.driver.find_element(By.XPATH, '//input[@placeholder="密码"]').send_keys('') # 点击登录 self.driver.find_element(By.XPATH, "//button[text()='登录']").click() # 断言是否成功 excepted = "请输入密码" # 获取报错信息的文本 res = self.driver.find_element(By.XPATH, '//div[@class="form-error-info"]').text self.assertEqual(excepted, res) def test_login_phone_is_none(self): """手机号为空""" # 输入账号和密码 self.driver.find_element(By.XPATH, '//input[@placeholder="手机号"]').send_keys('') self.driver.find_element(By.XPATH, '//input[@placeholder="密码"]').send_keys('python') # 点击登录 self.driver.find_element(By.XPATH, "//button[text()='登录']").click() # 断言是否成功 excepted = "请输入手机号" # 获取报错信息的文本 res = self.driver.find_element(By.XPATH, '//div[@class="form-error-info"]').text self.assertEqual(excepted, res)
一开始简单的列出了三种场景,也能跑通,但是,有个问题,下面两个方法好像差不多,咱们能不能优化一下呢?
如何优化?附上代码,并截图解析
import unittest from selenium.webdriver import Chrome from selenium.webdriver.common.by import By from unittestreport import ddt, list_data login_data_is_none = [ {"title": "密码为空", "phone": "18684720553", "pwd": "", "expected": "请输入密码"}, {"title": "手机号为空", "phone": "", "pwd": "python", "expected": "请输入手机号"}, ] @ddt class TestLogin(unittest.TestCase): def setUp(self): """登录的前置:启动浏览器,访问登录页面""" self.driver = Chrome() self.driver.implicitly_wait(10) # 打开前程贷的登录页面 self.driver.get("http://8.129.91.152:8765/Index/login.html") def test_login_pass(self): """登录成功""" # 输入账号 self.driver.find_element(By.XPATH, '//input[@placeholder="手机号"]').send_keys('18684720553') # 输入密码 self.driver.find_element(By.XPATH, '//input[@placeholder="密码"]').send_keys('python') # 点击登录 self.driver.find_element(By.XPATH, "//button[text()='登录']").click() # 断言是否成功 try: ele = self.driver.find_element(By.XPATH, '//a[text()="我的帐户[python]"]') except: ele = False self.assertTrue(ele) @list_data(login_data_is_none) def test_login_data_is_none(self, case): # 输入账号和密码 self.driver.find_element(By.XPATH, '//input[@placeholder="手机号"]').send_keys(case['phone']) self.driver.find_element(By.XPATH, '//input[@placeholder="密码"]').send_keys(case['pwd']) # 点击登录 self.driver.find_element(By.XPATH, "//button[text()='登录']").click() # 断言是否成功 expected = case["expected"] # 获取报错信息的文本 res = self.driver.find_element(By.XPATH, '//div[@class="form-error-info"]').text self.assertEqual(expected, res)
然后运行run.py文件,也可以跑的通
然后,这个登录有一个场景,就是账号未授权时,会给出toast弹窗提示,这种的又如何来展开呢?
这种,第一步就是要正确写出toast弹窗的xpath定位表达式,怎么来具体操作呢?
然后获取文本信息,写出定位表达式
然后直接上代码
def test_login_toast_error(self): """账号未授权""" # 输入账号 self.driver.find_element(By.XPATH, '//input[@placeholder="手机号"]').send_keys('18684720552') # 输入密码 self.driver.find_element(By.XPATH, '//input[@placeholder="密码"]').send_keys('python') # 点击登录 self.driver.find_element(By.XPATH, "//button[text()='登录']").click() # 获取toast弹窗的内容 expected = "此账号没有经过授权,请联系管理员!" WebDriverWait(self.driver, 15, 0.5).until( EC.visibility_of_element_located((By.XPATH, '//div[@class="layui-layer-content"]')) ) res = self.driver.find_element(By.XPATH, '//div[@class="layui-layer-content"]').text self.assertEqual(expected, res)
注意:记得要导入显示等待和EC
因此,这种出现弹窗的,咱们也可以合理的处理好。