用例一:
- 打开http://www.126.com
- 输入正确的用户名和密码
- 登录,断言登录成功
- 关闭浏览器
用例二:
- 打开http://www.126.com
- 输入错误的用户名和密码
- 登录,断言登录失败
- 关闭浏览器
用例一,login_success.py
# -*- coding:utf-8 -*-from selenium import webdriverimport timeimport unittest# 创建Firefox驱动实例driver = webdriver.Firefox()# 最大化窗口driver.maximize_window()# 设置等待超时时间为10sdriver.implicitly_wait(10)# 访问126网站首页driver.get("http://www.126.com")# 定位到内嵌的iframe元素iframe_element = driver.find_element_by_css_selector("#loginDiv>iframe")# 切换到内嵌的iframe元素driver.switch_to.frame(iframe_element)# 定位到用户名输入框email_element = driver.find_element_by_css_selector("#account-box input[name='email']")# 清空文本框中的数据email_element.clear()# 输入存在的用户名email_element.send_keys("xxxxxx")# 定位密码输入框password_element = driver.find_element_by_css_selector("#login-form input[name='password']")#清空密码输入框的数据password_element.clear()# 输入正确的密码password_element.send_keys("xxxxx")# 定位登录按钮submit_element = driver.find_element_by_css_selector("#dologin")# 点击登录按钮submit_element.click()# 退出iframedriver.switch_to.default_content()#休息3stime.sleep(3)# 断言,如果已经登录,页面中会包含登录名信息assert "xxxxxx" in driver.page_source# 关闭浏览器driver.quit()
用例二,login_fail.py
# -*- coding:utf-8 -*-from selenium import webdriverimport timeimport unittest# 创建Firefox驱动实例driver = webdriver.Firefox()# 最大化窗口driver.maximize_window()# 设置等待超时时间为10sdriver.implicitly_wait(10)# 访问126网站首页driver.get("http://www.126.com")# 定位到内嵌的iframe元素iframe_element = driver.find_element_by_css_selector("#loginDiv>iframe")# 切换到内嵌的iframe元素driver.switch_to.frame(iframe_element)# 定位到用户名输入框email_element = driver.find_element_by_css_selector("#account-box input[name='email']")# 清空文本框中的数据email_element.clear()# 输入存在的用户名email_element.send_keys("bucunzaiemail")# 定位密码输入框password_element = driver.find_element_by_css_selector("#login-form input[name='password']")#清空密码输入框的数据password_element.clear()# 输入正确的密码password_element.send_keys("Test6530")# 定位登录按钮submit_element = driver.find_element_by_css_selector("#dologin")# 点击登录按钮submit_element.click()#休息3stime.sleep(3)# 断言登录失败,提示账号或密码错误assert "帐号或密码错误" in driver.page_source# 关闭浏览器driver.quit()
从上面的测试脚本中我们发现,如果我们在多个地方写了定位同一个元素的定位方法,一旦这个元素的信息发生改变,我们需要修改多个地方的代码,上面只是两个测试文件,如果测试文件特别多,我们的时间大部分都花费到了这里,效率非常低。
1.PO设计模式
PO设计模式为每个待测页面创建一个对象,将业务逻辑代码和测试代码分离,业务逻辑代码被封装在页面类中,下面我们把对登录页面的操作封装在页面类中,我们的测试代码只需要调用页面类的方法即可。
basepage.py
# -*- coding:utf-8 -*-from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECclass BasePage(object): """ 定义所有页面的基类,即所有页面都应该继承该类 """ def __init__(self, driver, base_url="http://www.126.com"): self.driver = driver self.base_url = base_url def open_page(self): self.driver.get(self.base_url) # 定位元素,超时时间默认为10s def find_element(self, loc, timeout=10): return WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(loc)) # 文本框中输入数据 def input_text(self, loc, text, timeout=10): self.find_element(loc,timeout).send_keys(text) # 点击按钮 def click(self, loc, timeout=10): self.find_element(loc, timeout).click() # 获取源码 def get_source(self): return self.driver.page_source # 切换到内嵌iframe def switch_to_frame(self, loc): self.driver.switch_to.frame(self.find_element(loc)) # 返回到父iframe,如果有 def switch_to_parent_frame(self): self.driver.switch_to.parent_frame() # 从iframe切换到最外层 def switch_to_default_content(self): self.driver.switch_to.default_content()
login_page.py
# -*- coding:utf-8 -*-from basepage import BasePagefrom selenium.webdriver.common.by import Byclass LoginPage(BasePage): """ 定义登录页面类 """ # 定位器 username_loc = (By.CSS_SELECTOR, "#account-box input[name='email']") password_loc = (By.CSS_SELECTOR, "#login-form input[name='password']") submit_loc =(By.CSS_SELECTOR, "#dologin") iframe_loc = (By.CSS_SELECTOR, "#loginDiv>iframe") # 操作 # 输入用户名 def input_username(self, username): self.input_text(self.username_loc, username) # 输入密码 def input_password(self, password): self.input_text(self.password_loc, password) # 点击登录 def click_login(self): self.click(self.submit_loc) # 切换到内嵌iframe def switch_to_login_frame(self): self.switch_to_frame(self.iframe_loc) # 统一登录入口 def user_login(self, username, password): self.open_page() self.switch_to_login_frame() self.input_username(username) self.input_password(password) self.click_login()
test_login_page.py
# -*- coding:utf-8 -*-import unittestfrom selenium import webdriverfrom login_page import LoginPageimport timeclass TestLoginPage(unittest.TestCase): """测试类""" def setUp(self): self.driver = webdriver.Firefox() def tearDown(self): self.driver.quit() def test_user_login_success(self): username = "xxxxxx" password = "xxxxxx" login_page = LoginPage(self.driver) login_page.user_login(username, password) login_page.switch_to_default_content() time.sleep(3) self.assertIn(username, login_page.get_source()) def test_user_login_fail(self): username = "bucunzaiemail" password = "aaaaa" login_page = LoginPage(self.driver) login_page.user_login(username, password) time.sleep(3) self.assertIn("帐号或密码错误", login_page.get_source())if __name__ == '__main__': unittest.main()