自动化测试Pytest中文文档浅谈自动化测试

Selenium + Pytest + Allure UI测试过

2017-05-08  本文已影响280人  Spareribs

基本说明

安装包

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操作

模拟文库的登录和退出

代码分析

#!/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))

代码要点:

Pytest

# 用于指定代码的执行顺序
@pytest.mark.run(order=3)

# 调用pytest命令生成测试报告(临时文件)
pytest.main("-s -q test_groups.py  --alluredir report-{0}".format(now))

代码要点:

Allure

# 给测试报告加入标题
@allure.step(title='登录')
# 调用allure 命令生成html测试报告
os.system("C:\\Python27\\Lib\\allure-commandline\\bin\\allure generate report-{0}/ -o report-{0}/html".format(now))

代码要点:

上一篇下一篇

猜你喜欢

热点阅读