基本说明
安装包
colorama==0.3.9
enum34==1.1.6
lxml==3.7.3
namedlist==1.7
py==1.4.33
pytest==3.0.7
pytest-allure-adaptor==1.7.7
selenium==3.4.1
six==1.10.0
wheel==0.24.0
allure-commandline工具
windows下面生成html文件必备的命令行
allure-commandline
Webdriver工具
ChromeDriver下载地址
geckodriver下载地址
用到的Webdriver方法
# 元素定位(像这种定位方法还有很多,此处列举部分)
find_element_by_id()
find_element_by_name()
find_element_by_css_selector()
driver.find_element_by_class_name()
# 鼠标操作
click():点击
move_to_element():移动到
# 键盘操作
send_keys():键盘输入
# 下拉列表处理
select_by_visible_text()
分析UI操作
模拟文库的登录和退出
- 明确业务流程的操作(浏览器、键盘、鼠标操作的顺序):这一步可以通过SeleniumIDE录制功能快速分析
- 明确每一个步骤需要核对的内容(assert):数量增减、数据库字段比对、页面返回值
代码分析
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
# @Time : 2017/5/8 22:38
# @Author : Spareribs
# @File : test_wenku.py
"""
import os
import time
import pytest
import allure
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
class TestGroupsSample(object):
u"""
The Pytest Class for wenku testing~~~~
"""
@pytest.mark.run(order=1)
@allure.step(title='登录')
def test_login(self):
u"""登录到百度文库"""
print "login"
global driver
driver = webdriver.Chrome()
driver.get("https://passport.baidu.com/v2/?login&u=http://wenku.baidu.com/user/mydocs")
time.sleep(1)
# 输入帐号密码并登录文库
driver.find_element_by_id("TANGRAM__PSP_3__userName").click()
driver.find_element_by_id("TANGRAM__PSP_3__userName").clear()
driver.find_element_by_id("TANGRAM__PSP_3__userName").send_keys("13129321271")
driver.find_element_by_id("TANGRAM__PSP_3__password").click()
driver.find_element_by_id("TANGRAM__PSP_3__password").clear()
driver.find_element_by_id("TANGRAM__PSP_3__password").send_keys("123456z")
time.sleep(15) # 输入验证码
driver.find_element_by_id("TANGRAM__PSP_3__submit").click()
time.sleep(3)
# 初次登录需要清理掉提醒信息
driver.find_element_by_css_selector("div.btn").click()
assert driver.title == u"个人中心_百度文库"
@pytest.mark.run(order=2)
@allure.step(title='关闭浏览器')
def test_logout(self):
u"""退出百度文库"""
print "logout"
global driver
time.sleep(2)
# 移动鼠标到对应的位置
above = driver.find_element_by_class_name("text-dec-under")
time.sleep(5)
ActionChains(driver).move_to_element(above).perform()
# 无法直接定位到退出按钮,先move_to_element到设置按钮上
driver.find_element_by_id("logout").click()
assert driver.title == u"登录百度帐号"
@pytest.mark.run(order=3)
@allure.step(title='关闭浏览器')
def test_groups_close(self):
u"""关闭浏览器"""
print "close"
global driver
driver.quit()
if __name__ == '__main__':
# pytest.main("-s -q test_groups.py")
# 取当前时间
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
pytest.main("-s -q test_wenku.py --alluredir report-{0}".format(now))
# 调用命令生成报告(Windows下面的方法)----------需要下载allure.exe
os.system("C:\\Python27\\Lib\\allure-commandline\\bin\\allure generate report-{0}/ -o report-{0}/html".format(now))
代码要点:
- global:inti函数无法初始化一个全局变量,只能用这个方法实现
- time.sleep(5):等待浏览器操作完成,避免网络卡顿或者机器卡顿导致assert结果无法比对
- cookies:如果driver没有成为全局变量,那么所有的求情都要登录一次
- move_to_element:退出按钮不能直接定位,鼠标需要先移动到个人设置的位置
Pytest
# 用于指定代码的执行顺序
@pytest.mark.run(order=3)
# 调用pytest命令生成测试报告(临时文件)
pytest.main("-s -q test_groups.py --alluredir report-{0}".format(now))
代码要点:
- 顺序是为了确保按照流程(依赖关系)来执行
- -s和-q是减少屏幕输出用的,可以不加
- --alluredir后面跟的是报告的路径名字
Allure
# 给测试报告加入标题
@allure.step(title='登录')
# 调用allure 命令生成html测试报告
os.system("C:\\Python27\\Lib\\allure-commandline\\bin\\allure generate report-{0}/ -o report-{0}/html".format(now))
代码要点:
- C:\Python27\Lib\allure-commandline\bin\allure:这个是allure commandline的exe执行文件的路径
- generate report-{0}/ :report-{0}/就是pytest中定义的路径
- -o report-{0}/html : 是指输出的html文件的路径