一天一个关于测试知识点,5分钟内讲解你最关心的软件测试问题,今天就接着来谈谈关于软件测试中的“测试程序的批量优化及页面封装”。
对页面的封装
案例4-78:测试添加用户购物配送地址功能。
def test_Add_Addess(self):
username='cindy'
password='123456'
#登录
self.clear(self.find_element_by_id(driver,"id_username"))
self.send_keys(self.find_element_by_id(driver,"id_username"),username)
self.clear(self.find_element_by_id(driver,"id_password"))
self.send_keys(self.find_element_by_id(driver,"id_password"),password)
self.submit(self.find_element_by_class_name(driver,"form-signin"))
#在登录首页找到用户名
self.fd.click(self.fd.find_element_by_link_text(self.driver,username))
#进入用户信息列表
self.fd.click(self.fd.find_element_by_id(self.driver,"add_address"))
#进入添加送货地址信息页面,添加收货信息
self.fd.clear(self.fd.find_element_by_name(self.driver,"address"))
self.fd.send_keys(self.fd.find_element_by_name(self.driver,"address"),"北京清华")
self.fd.clear(self.fd.find_element_by_name(self.driver,"phone"))
self.fd.send_keys(self.fd.find_element_by_name(self.driver,"phone"),"13699876655")
self.fd.click(self.fd.find_element_by_xpath(self.driver,"/html/body/div/form/button"))
#验证加入的信息
self.assertIsNotNone(self.fd.find_element_by_link_text(self.driver,"删除"))
#测试完毕,清场操作
self.fd.click(self.fd.find_element_by_link_text(self.driver,"删除"))
被测对象是一个附录A电子商务网站,要测试在用户界面添加一条收货信息。所以必须先登录,登录完毕后点击当前用户名的超链,然后进入用户信息界面,接下来点击新建用户收货地址按钮,进入新建页面,建立完毕进行断言,最后为了以后仍旧可以进行这个测试用例,进行清除操作。由此可见,如果一个测试业务比较长,按照这样的写法可读性是比较差的,并不便于维护,因此采用目前比较流行的基于页面的封装方法。
先对登录页面进行封装,代码如下。
#通用信息
class Util:
def __init__(self,driver):
self.driver = driver
def logout(self):
findby.click(self.driver,findby.find_element_by_link_text(self,self.driver,"退出"))
findby.quit(self,self.driver)
#登录页面
class LoginPage:
def __init__(self,driver):
self.driver = driver
#登录操作
def login(self,username,password):
findby.clear(self.driver,findby.find_element_by_id(self,self.driver,"id_username"))#清空用户输入框
findby.send_keys(self.driver,findby.find_element_by_id(self,self.driver,"id_username"),username)#输入用户名
findby.clear(self.driver,findby.find_element_by_id(self,self.driver,"id_password")) #清空密码输入框
findby.send_keys(self.driver,findby.find_element_by_id(self,self.driver,"id_password"),password)#输入密码
findby.submit(self.driver,findby.find_element_by_class_name(self,self.driver,"form-signin"))#提交表单
产品页面封装如下。
#产品页面
class ProductPage:
def __init__(self,driver):
self.driver = driver
#进入用户信息页面
def click_username(self,username):
findby.click(self.driver,findby.find_element_by_link_text(self,self.driver,username))# 点击登录用户名链接
用户详情页面封装如下。
#用户详情页面
class UserPage:
def __init__(self,driver):
self.driver = driver
#添加购物配送地址信息
def click_add_address_button(self):
findby.click(self.driver,findby.find_element_by_id(self,self.driver,"add_address"))
#验证地址信息
def check_address(self):
return findby.find_element_by_link_text(self,self.driver,"删除")
#删除地址信息
def delete_address(self):
delete_button = findby.find_element_by_link_text(self,self.driver,"删除")
findby.click(self.driver,delete_button)
添加地址页面封装如下。
#地址
class AddressPage:
def __init__(self,driver):
self.driver = driver
#添加新地址
def create_address(self,addess,phone):
findby.clear(self.driver,findby.find_element_by_name(self,self.driver,"address"))#清空地址栏
findby.send_keys(self.driver,findby.find_element_by_name(self,self.driver,"address"),addess)#输入地址信息
findby.clear(self.driver,findby.find_element_by_name(self,self.driver,"phone"))##清空电话栏
findby.send_keys(self.driver,findby.find_element_by_name(self,self.driver,"phone"),phone)#输入电话信息
findby.submit(self.driver,findby.find_element_by_xpath(self,self.driver,"/html/body/div/form/button"))#提交表单
这样,验证添加购物地址的测试代码如下。
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest,time
from util import drivers,findby
from page import Util,LoginPage,ProductPage,UserPage,AddressPage,CartPage
class CheckEBusiness(unittest.TestCase):
def setUp(self):
driver = drivers().driver
fd = findby()
self.util = Util(driver)
self.loginpage = LoginPage(driver)
self.producepage = ProductPage(driver)
self.userpage = UserPage(driver)
self.addresspage = AddressPage(driver)
self.cartpage = CartPage(driver)
fd.implicitly_wait(driver,5)
fd.get(driver,"http://127.0.0.1:8000")
self.username = "cindy"
self.password = "123456"
self.adddress = "首体南路3号"
self.phone = "13681732596"
def test_Add_Addess(self):
self.loginpage.login(self.username,self.password)
self.producepage.click_username(self.username)
self.userpage.click_add_address_button()
self.addresspage.create_address(self.adddress,self.phone)
self.assertIsNotNone(self,self.userpage.check_address())
self.userpage.delete_address()
if __name__=="__main__":
unittest.main()
这样测试程序的代码的可读性与可维护性都得到了很好得加强。现在增加“测试购物车功能”的验证。只需在Product类中建立如下两个方法。
案例4-79:测试添加商品进购物车功能。
#放入购物车
def put_into_cart(self):
findby.click(self.driver,findby.find_element_by_link_text(self,self.driver,"放入"))# 点击“放入”链接
#查看购物车
def view_cart(self):
findby.click(self.driver,findby.find_element_by_partial_link_text(self,self.driver,"查看购物车"))# 点击“查看购物车”链接在加入一个Cart(购物车)类。
然后增加一个购物车的类。
#购物车页面
class CartPage:
def __init__(self,driver):
self.driver = driver
#验证购物车中是否存在商品
def check_goods(self):
return findby.find_element_by_xpath(self,self.driver,"//*[@id=\"id_count\"]")
#删除购物车总中的商品
def delete_goods(self):
findby.click(self.driver,findby.find_element_by_link_text(self,self.driver,"移除"))
相应的测试代码如下。
def test_Add_Goods_into_Cart(self):
self.loginpage.login(self.username,self.password)
self.producepage.put_into_cart()
self.producepage.view_cart()
self.assertIsNotNone(self.cartpage.check_goods())
self.cartpage.delete_goods()
最后再来验证一下对商品查询的功能,在Product类中加入。
案例4-80:测试商品查询功能。
#商品查询
def search_goods(self,good_name):
findby.send_keys(self.driver,findby.find_element_by_name(self,self.driver,"good"),good_name)#输入查询内容
findby.click(self.driver,findby.find_element_by_xpath(self,self.driver,"//*[@id=\"navbar\"]/form/button"))#输入查询内容
return findby.find_element_by_link_text(self,self.driver,"放入")
而在测试代码中加入如下两行测试语句就可以了。
def test_Search_Goods(self):
self.loginpage.login(self.username,self.password)
self.assertIsNotNone(self.producepage.search_goods("茶"))
案例4-81:使用pytest测试电子商务网站
现在用pytest+allure完成案例4-74、案例4-75、案例4-76测试用例,代码如下。
page.py
#!/usr/bin/env python
#coding:utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from util import findby
import time
import allure
#通用信息
class Util:
def __init__(self,driver):
self.driver = driver
def logout(self):
findby.click(self.driver,findby.find_element_by_link_text(self,self.driver,"退出"))
findby.quit(self,self.driver)
#登录页面
class LoginPage:
def __init__(self,driver):
self.driver = driver
@allure.step("登录系统")
def login(self,username,password):
findby.clear(self.driver,findby.find_element_by_id(self,self.driver,"id_username"))#清空用户输入框
findby.send_keys(self.driver,findby.find_element_by_id(self,self.driver,"id_username"),username)#输入用户名
findby.clear(self.driver,findby.find_element_by_id(self,self.driver,"id_password")) #清空密码输入框
findby.send_keys(self.driver,findby.find_element_by_id(self,self.driver,"id_password"),password)#输入密码
findby.submit(self.driver,findby.find_element_by_class_name(self,self.driver,"form-signin"))#提交表单
#产品页面
class ProductPage:
def __init__(self,driver):
self.driver = driver
@allure.step("进入用户信息页面")
def click_username(self,username):
findby.click(self.driver,findby.find_element_by_link_text(self,self.driver,username))# 点击登录用户名链接
@allure.step("放入购物车")
def put_into_cart(self):
findby.click(self.driver,findby.find_element_by_link_text(self,self.driver,"放入"))# 点击“放入”链接
@allure.step("查看购物车")
def view_cart(self):
findby.click(self.driver,findby.find_element_by_partial_link_text(self,self.driver,"查看购物车"))# 点击“查看购物车”链接
@allure.step("商品查询")
def search_goods(self,good_name):
findby.send_keys(self.driver,findby.find_element_by_name(self,self.driver,"good"),good_name)#输入查询内容
findby.click(self.driver,findby.find_element_by_xpath(self,self.driver,"//*[@id=\"navbar\"]/form/button"))#输入查询内容
return findby.find_element_by_link_text(self,self.driver,"放入")
#购物车页面
class CartPage:
def __init__(self,driver):
self.driver = driver
@allure.step("验证购物车中是否存在商品")
def check_goods(self):
return findby.find_element_by_xpath(self,self.driver,"//*[@id=\"id_count\"]")
@allure.step("删除购物车总中的商品")
def delete_goods(self):
findby.click(self.driver,findby.find_element_by_link_text(self,self.driver,"移除"))
#登录用户详情页面
class UserPage:
def __init__(self,driver):
self.driver = driver
@allure.step("添加购物配送地址信息")
def click_add_address_button(self):
findby.click(self.driver,findby.find_element_by_id(self,self.driver,"add_address"))
@allure.step("验证地址信息")
def check_address(self):
return findby.find_element_by_link_text(self,self.driver,"删除")
@allure.step("删除地址信息")
def delete_address(self):
delete_button = findby.find_element_by_link_text(self,self.driver,"删除")
findby.click(self.driver,delete_button)
#地址
class AddressPage:
def __init__(self,driver):
self.driver = driver
@allure.step("添加新地址")
def create_address(self,addess,phone):
findby.clear(self.driver,findby.find_element_by_name(self,self.driver,"address"))#清空地址栏
findby.send_keys(self.driver,findby.find_element_by_name(self,self.driver,"address"),addess)#输入地址信息
findby.clear(self.driver,findby.find_element_by_name(self,self.driver,"phone"))##清空电话栏
findby.send_keys(self.driver,findby.find_element_by_name(self,self.driver,"phone"),phone)#输入电话信息
findby.submit(self.driver,findby.find_element_by_xpath(self,self.driver,"/html/body/div/form/button"))#提交表单
test_PageObjetTest.py
#!/usr/bin/env python
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
from util import drivers,findby
from page import Util,LoginPage,ProductPage,UserPage,AddressPage,CartPage
import pytest
import allure
class TestEBusiness:
def setup(self):
driver = drivers().driver
fd = findby()
self.util = Util(driver)
self.loginpage = LoginPage(driver)
self.producepage = ProductPage(driver)
self.userpage = UserPage(driver)
self.addresspage = AddressPage(driver)
self.cartpage = CartPage(driver)
fd.implicitly_wait(driver,5)
fd.get(driver,"http://127.0.0.1:8000")
self.username = "cindy"
self.password = "123456"
self.adddress = "首体南路3号"
self.phone = "13681732596"
@allure.feature('电子商务产品')
@allure.story('添加配货地址')
@allure.severity('Normal')
@allure.issue("http://www.jila.com")
@allure.testcase("http://www.testlink.com")
def test_Add_Addess(self):
self.loginpage.login(self.username,self.password)
self.producepage.click_username(self.username)
self.userpage.click_add_address_button()
self.addresspage.create_address(self.adddress,self.phone)
try:
self.userpage.check_address()
assert 1==1
except:
assert 0==1
self.userpage.delete_address()
@allure.feature('电子商务产品')
@allure.story('添加进购物车')
@allure.severity('Normal')
@allure.issue("http://www.jila.com")
@allure.testcase("http://www.testlink.com")
def test_Add_Goods_into_Cart(self):
self.loginpage.login(self.username,self.password)
self.producepage.put_into_cart()
self.producepage.view_cart()
try:
self.cartpage.check_goods()
assert 1==1
except:
assert 0==1
self.cartpage.delete_goods()
@allure.feature('电子商务产品')
@allure.story('模糊查询商品')
@allure.severity('Normal')
@allure.issue("http://www.jila.com")
@allure.testcase("http://www.testlink.com")
def test_Search_Goods(self):
self.loginpage.login(self.username,self.password)
try:
self.producepage.search_goods("茶")
assert 1==1
except:
assert 0==1
def teardown(self):
self.util.logout()
if __name__ == '__main__':
pytest.main(['-sv', '-q', '--alluredir', './report/xml'])
建立文件report.bat。
del .\report\*
pytest .\test_PageObjetTest.py -s -q --alluredir report .\report\
allure serve .\report
allure open -h 127.0.0.1 -p 8083 .\report\report\
运行后产生如图4-40所示的测试报告。
图4-40 电子商务网站Pytest+Selenium 测试报告
批量运行
正如本书前面的章节所述,可以利用unittest自带的批量运行框架来对一系列测试代码进行运行。
import unittest
from HTMLTestRunner import HTMLTestRunner
test_dir='./'
discover=unittest.defaultTestLoader.discover(test_dir,pattern="Check*.py")
if __name__=='__main__':
runner=unittest.TextTestRunner()
#以下用于生成测试报告
fp=open("result.html","wb")
runner =HTMLTestRunner(stream=fp,title='测试报告',description='测试用例执行报告')
runner.run(discover)
fp.close()
在这里所有测试用例的文件名都定义为Check*.py,比如:CheckBaidu.py。运行这个文件,符合条件的当前目录下所有文件名以Check开始的py文件将会被执行,执行完毕形成result.html测试报告。测试报告如图4-41所示。
图4-41 Selenium unittest测试报告