Pytest+Webdriver+Alluer的UI自动化测试框架

作为web自动化的入门学习,搭建框架练习下

一、熟悉项目的测试框架的整体目录

Pytest+Webdriver+Alluer的UI自动化测试框架_第1张图片

二、 PIP安装完所需框架

1、编写main.py

import pytest


if __name__ == '__main__':
 #pytest.main()  # 遍历相同目录下的所以test开头的用例
 #生成测试报告 
#一次执行所有接口测试用例,生成一个测试报告mix
    pytest.main(['--html=../test1/report/test.html', "testwzm.py"])

2、设计登录获取鉴权

import requests
import json
import pytest

#获取环境鉴权
def session():
    #url是固定的获取鉴权接口    
    url = 'https://XXXXXXXXXXXXXX'
    print(url)

    params = {"keywordType":"mobile","keyword":"18111111111","channel":"0","subChannel":"0"}

    headers = {'content-type': "application/json"}

    r = requests.post(url, data=json.dumps(params), headers=headers)

    dict = json.loads(r.text)
    print(dict)
    chanelurl = dict['obj']['channelUrl']
    print(type(chanelurl))
    return chanelurl

3、设计页面测试用例 testwzm.py

from selenium import webdriver
import pytest
import requests
import json
from selenium.webdriver.common.keys import Keys
from testcases import session
from time import sleep

def test_testng():
    """测试页面"""
    option = webdriver.ChromeOptions()
    option.binary_location=r'C:\Program Files\Google\Chrome\Application\chrome.exe'
    driver = webdriver.Chrome()
    url = session.session()
    print(url)
    driver.get(url)
# 测试页面
    driver.get('https://xxxxd')
    driver.maximize_window()
    sleep(1)
    elem = driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/ul/li[1]')
    elem.click()
    sleep(1)
    address = driver.find_element_by_xpath('//*[@id="app"]/div/dl[1]/dd[1]')
    address.click()
    sleep(1)
    button = driver.find_element_by_xpath('//*[@id="app"]/div/button')
    button.click()
    sleep(1)
    check = driver.find_element_by_xpath('//*[@id="app"]/div/div[3]/div[2]/div')
    check.click()
    sleep(1)
    button1 = driver.find_element_by_xpath('//*[@id="app"]/div/div[3]/button')
    button1.click()
    sleep(3)
    alert = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]')
    alert.click()
    sleep(1)
    cancel = driver.find_element_by_xpath('//*[@id="app"]/div/div[4]/input')
    cancel.click()
    sleep(1)
    yes = driver.find_element_by_xpath('//*[@id="app"]/div/div[6]/div[2]/p/a[2]')
    yes.click()
    #text_label = driver.find_element_by_xpath('//*[@id="kw"]')
    sleep(1)
    assert True ==(text_label.is_displayed())
    driver.close()
    driver.quit()

4、设计conftest.py 优化报告样式

from py.xml import html
import pytest



@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th('Description'))  # 表头添加Description
    cells.pop(-1)  # 删除link


@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(1, html.td(report.description))  #表头对应的内容
    cells.pop(-1)  # 删除link列


@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):  #description取值为用例说明__doc__
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)

@pytest.mark.optionalhook
def pytest_html_results_summary(prefix):  #添加summary内容

Pytest+Webdriver+Alluer的UI自动化测试框架_第2张图片

你可能感兴趣的:(pytest)