in web automation test, PO mode is Page Object (operation flow is separated from Page elements); The test case does not contain any element positioning, only the business logic, and the page element positioning is written as a method encapsulated in the object.
PO mode basic steps are divided into 3 steps:
1. Prepare the base class BasePage, which contains all the page’s element operation methods
2. Define A class A, which inherits the BasePage class, which contains all the element positioning and action that the test page needs to use encapsulated as A method
3. Define A class B to inherit from unittest, write the test case (just call the corresponding wrapper method in A to complete the business logic writing)
follow the above three steps to operate the login of suning:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
class BasePage(object):
def __init__(self, br, url):
self.br = br
self.url = url
# 打开网页
def open_url(self):
self.br.get(self.url)
# 查找元素
def find_element(self, *loc):
return self.br.find_element(*loc)
# 输入
def input_txt(self, loc, text):
self.find_element(*loc).send_keys(text)
# 清空文本
def clear(self, loc):
self.find_element(*loc).clear()
# 点击
def click(self, loc):
self.find_element(*loc).click()
# 下拉列表选择
def select_term(self, loc, index):
# 实例化Select
Select(self.find_element(*loc)).select_by_index(int(index))
# s1.select_by_index(1) # 选择第二项选项:o1
# s1.select_by_value("o2") # 选择value="o2"的项
# s1.select_by_visible_text("o3") # 选择text="o3"的值,即在下拉时我们可以看到的文本
# 鼠标悬浮
def mouse_hover(self, loc):
move = self.find_element(*loc)
ActionChains(self.br).move_to_element(move).perform()
# 获取title
def get_title(self):
return self.br.title
# 获取文本信息
def get_txt(self, loc):
return self.find_element(*loc).text
# 获取当前页面url
def get_page_url(self):
return self.br.current_url
web_login.py
# !/usr/bin/env python
# -*- coding: utf-8 -*-
from selenium.webdriver.common.by import By
from bs_page import BasePage
class LoginPage(BasePage):
def __init__(self, br, url):
BasePage.__init__(self, br, url)
# 切换登录方式
def switch_login_type(self):
login_type = (By.XPATH, '/html/body/div[2]/div/div/div[1]/a[2]')
self.click(login_type)
# 输入用户名
def send_user_name(self, username):
name_input = (By.ID, "userName")
self.clear(name_input)
self.input_txt(name_input, username)
# 输入密码
def send_user_pwd(self, userpwd):
pwd_input = (By.ID, "password")
self.clear(pwd_input)
self.input_txt(pwd_input, userpwd)
# 点击 登录按钮
def click_login_btn(self):
login_btn = (By.ID, 'submit')
self.click(login_btn)
# 获取提示信息
def get_emg_txt(self):
emg_txt = (By.XPATH, '/html/body/div[2]/div/div/div[2]/div[1]/div[1]/div[2]/span')
return self.get_txt(emg_txt)
ex_login.py
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import ddt
import unittest
from pages.web_login import LoginPage
from common import html_report, read_yaml, journal, set_browser
@ddt.ddt
class TestBiliLogin(unittest.TestCase):
# 日志
log = journal.bs_log()
# 读取用例
login_data = read_yaml.get_yaml_data("login_data.yaml")
# 浏览器初始化配置
br = set_browser.base_browser()
# 网站首页地址
web_url = set_browser.base_url()
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
# 退出浏览器
cls.br.quit()
# 初始化工作
def setUp(self):
time.sleep(1)
# 退出清理工作
def tearDown(self):
time.sleep(1)
@ddt.data(*login_data)
def test_login(self, user_data):
br = self.br
p_url = self.web_url+"/login"
b_page = LoginPage(br, p_url)
# 打开登录页面
b_page.open_url()
# 切换登录方式
b_page.switch_login_type()
# 输入用户名
b_page.send_user_name(user_data[0])
# 输入密码
b_page.send_user_pwd(user_data[1])
# 点击 登录
b_page.click_login_btn()
# 获取当前页面地址
page_url = b_page.get_page_url()
# 判断页面地址是否发生变化
if page_url == p_url:
# 获取提示信息
emg = b_page.get_emg_txt()
self.assertEqual(user_data[2], emg)
else:
# 获取页面title
p_title = b_page.get_title()
self.assertEqual(user_data[2], p_title)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestBiliLogin)
# 调用定义HTMLTestRunner的方法
runner = html_report.re_html_cn()
# 执行测试套件
runner.run(suite)
p>
div>