[Py_21]自动化测试项目综合实战(Python+Seleni
2018-07-22 本文已影响104人
Fighting_001
一、项目背景
在Web测试站点的新闻子页面(http://localhost/news/)进行登录测试
二、功能实现
- 自动运行用例
- 自动生成测试报告
- 自动断言与截图
- PageObject+UnitTest
三、项目架构
AutoTest_Project架构四、执行过程实现
项目目录结构:
1.浏览器driver的定义
driver.py
# 浏览器的定义
from selenium import webdriver
# 启动浏览器驱动
def browser():
driver = webdriver.Chrome()
# driver = webdriver.Firefox()
# driver = webdriver.PhantomJS()
# driver.get("https://www.baidu.com")
return driver
# 调试运行
if __name__ == '__main__':
browser()
2.工具方法模块(实现截图和保存)
function.py
from selenium import webdriver
import os
def insert_img(driver,filename):
func_path = os.path.dirname(__file__) # 获取当前脚本所在目录的绝对路径
print(func_path)
base_dir = os.path.dirname(func_path) # 获取当前脚本的上一级目录的绝对路径
print(base_dir)
base_dir = str(base_dir) # 以字符串方式来处理
base_dir = base_dir.replace('\\','/') # 将'\\'替换为'/'
print(base_dir)
base = base_dir.split("/Website")[0] # 将base_dir的绝对路径以"/Website"做拆分点形成列表,分为2个元素部分
print(base)
filepath = base+"/Website/TestReport/screenshot/"+filename # filename以自定义名称作为参数
driver.get_screenshot_as_file(filepath) # 保存截图文件到指定的路径
# 内部调试
if __name__ == '__main__':
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
insert_img(driver,"111.png")
driver.quit()
PageObject页面对象的封装
3.基础页面类的模块
BasePage.py
from time import sleep
'''创建页面基础类'''
class Page():
# 初始化
def __init__(self,driver):
self.base_url = "http://localhost"
self.driver = driver
self.timeout = 10
# 定义打开不同的子页面方法
# 下划线开头的_open()方法代表私有方法,不可直接调用,防止被随意修改
def _open(self,u_para):
url = self.base_url+u_para
print("Test page is: %s" %url)
self.driver.maximize_window()
self.driver.get(url)
sleep(2)
assert self.driver.current_url==url,"Did not land on %s" %url
# 定义公开的open()方法,实现调用_pen(u_Para)方法
def open(self):
self._open(self.u_para)
# 封装元素定位的方法
def find_element(self,*loc): # 定位的方式、定位的属性可变化
return self.driver.find_element(*loc)
4.news登录页面类的模块
LoginPage.py
from BasePage import *
from selenium.webdriver.common.by import By
'''创建首页登录页面的类'''
class LoginPage(Page):
u_para = "/news/" # 当调用open()方法时相当于构造的url="http://localhost/news/"
# 设置定位器
username_loc = (By.NAME,"username")
password_loc = (By.NAME,"password")
submit_loc = (By.NAME,"Submit")
# 定义username输入框元素的定位、输入方法
def type_username(self,username):
self.find_element(*self.username_loc).clear() # 调用find_element()方法,username定位器变量作为参数传入
self.find_element(*self.username_loc).send_keys(username) # 输入username参数值
# 定义password输入框元素的定位、输入方法
def type_password(self,password):
self.find_element(*self.password_loc).clear()
self.find_element(*self.password_loc).send_keys(password)
# 定义Submit按钮元素的点击方法
def type_submit(self):
self.find_element(*self.submit_loc).click()
# 封装登录功能模块的方法
def login_action(self,username,password):
self.open()
self.type_username(username)
self.type_password(password)
self.type_submit()
# 设置登录成功和失败的定位器
loginPass_loc = (By.LINK_TEXT,"我的空间") # 登录成功后出现“我的空间”元素
loginFail_loc = (By.NAME,"username") # 登录失败后自动回返回首页未登录状态,有用户名和密码输入框
# 返回实际定位的值,作为与预期相比的判断依据
def type_loginPass_hint(self):
return self.find_element(*self.loginPass_loc).text
def type_loginFail_hint(self):
return self.find_element(*self.loginFail_loc).text
5.unittest组织测试用例
- Case01:用户名和密码正确,点击登录
- Case02:用户名正确,密码错误,点击登录
- Case03:用户名和密码为空,点击登录
test_login.py
import unittest
from time import sleep
from page_object.LoginPage import *
from model import function, myunit
class LoginTest(myunit.StartEnd):
# @unittest.skip("skp this case")
def test_login01_normal(self):
'''username is ok,password is ok'''
print("test_login01_normal is starting running...")
po = LoginPage(self.driver) # 创建LoginPage类的对象po
po.login_action("Test001","123456")
sleep(3)
# 断言、截屏
self.assertEqual(po.type_loginPass_hint(),"我的空间")
function.insert_img(self.driver,"login01_normal.png")
print("test_login01_normal's test is end !")
# @unittest.skip
def test_login02_passwordError(self):
'''username is ok,password is error'''
print("test_login02_passwordError is starting running...")
po = LoginPage(self.driver)
po.login_action("Test001","123")
sleep(3)
self.assertEqual(po.type_loginFail_hint(),"")
function.insert_img(self.driver,"login02_fail.png")
print("test_login02_passwordError's test is end !")
# @unittest.skip
def test_login03_empty(self):
'''username and password are empty'''
print("test_login03_empty is starting running...")
po = LoginPage(self.driver)
po.login_action("","")
sleep(3)
self.assertEqual(po.type_loginFail_hint(),"")
function.insert_img(self.driver,"login03_empty.png")
print("test_login03_empty's test is end !")
if __name__ == '__main__':
unittest.main()
6.执行测试用例
run_test.py
import time
import unittest
from BSTestRunner import BSTestRunner
report_dir = "./TestReport/"
test_dir = "./TestCase/"
print("Start runnig TestCase...")
discover = unittest.defaultTestLoader.discover(test_dir,pattern="test_login.py") # 设定目录,及其匹配的规则
now = time.strftime("%y-%m-%d %H_%M_%S")
report_name = report_dir+now+"_result.html"
print("Start writting report...")
# Python3运行前记得把BSTestRunner.py 120行 'unicode'-->'str' 以免报错
with open(report_name,'wb') as f:
runner = BSTestRunner(stream=f,title="Test Report",description="localhost's login test")
runner.run(discover)
f.close()
print("Test is end !")