篇幅较长,要耐心阅读哦~
import allure
import configparser
import os
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
@allure.feature('Test Baidu WebUI')
class ISelenium(unittest.TestCase):
# 读入配置文件
def get_config(self):
config = configparser.ConfigParser()
config.read(os.path.join(os.environ['HOME'], 'iselenium.ini'))
return config
def tearDown(self):
self.driver.quit()
def setUp(self):
config = self.get_config()
# 控制是否采用无界面形式运行自动化测试
try:
using_headless = os.environ["using_headless"]
except KeyError:
using_headless = None
print('没有配置环境变量 using_headless, 按照有界面方式运行自动化测试')
chrome_options = Options()
if using_headless is not None and using_headless.lower() == 'true':
print('使用无界面方式运行')
chrome_options.add_argument("--headless")
self.driver = webdriver.Chrome(executable_path=config.get('driver', 'chrome_driver'),
options=chrome_options)
@allure.story('Test key word 今日头条')
def test_webui_1(self):
""" 测试用例1,验证'今日头条'关键词在百度上的搜索结果
"""
self._test_baidu('今日头条', 'test_webui_1')
@allure.story('Test key word 王者荣耀')
def test_webui_2(self):
""" 测试用例2, 验证'王者荣耀'关键词在百度上的搜索结果
"""
self._test_baidu('王者荣耀', 'test_webui_2')
def _test_baidu(self, search_keyword, testcase_name):
""" 测试百度搜索子函数
:param search_keyword: 搜索关键词 (str)
:param testcase_name: 测试用例名 (str)
"""
self.driver.get("https://www.baidu.com")
print('打开浏览器,访问 www.baidu.com .')
time.sleep(5)
assert f'百度一下' in self.driver.title
elem = self.driver.find_element_by_name("wd")
elem.send_keys(f'{search_keyword}{Keys.RETURN}')
print(f'搜索关键词~{search_keyword}')
time.sleep(5)
self.assertTrue(f'{search_keyword}' in self.driver.title, msg=f'{testcase_name}校验点 pass')
[driver]
chrome_driver=
allure-pytest
appium-python-client
pytest
pytest-testconfig
requests
selenium
urllib3
**Selenium 自动化测试程序(Python版)**
运行环境:
- selenium web driver
- python3
- pytest
- git
配置文件:iselenium.ini
- 将配置文件复制到本地磁盘的[user.home]目录
- 填入设备的chromwebdriver文件的全路径
运行命令:
pytest -sv test/web_ut.py --alluredir ./allure-results
本机需要安装chrome浏览器
Chrome driver版本与chrome浏览器版本有支持对应关系
Chrome driver 下载参考网站:http://npm.taobao.org/mirrors/chromedriver/
添加命令加载python库:pip install -r requirements.txt
添加运行命令:pytest -sv test/web_ut.py
其中. ~/.bash_profile是为了获取本机的环境变量
添加运行参数,控制是否为有界面运行,此步骤之前可以先试运行程序,没有错误后再添加
import requests
import urllib3
class HttpClient:
"""Generic Http Client class"""
def __init__(self, disable_ssl_verify=False, timeout=60):
"""Initialize method"""
self.client = requests.session()
self.disable_ssl_verify = disable_ssl_verify
self.timeout = timeout
if self.disable_ssl_verify:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def Get(self, url, headers=None, data=None, json=None, params=None, *args, **kwargs):
"""Http get method"""
if headers is None:
headers = {}
if self.disable_ssl_verify:
response = self.client.get(url, headers=headers, data=data, json=json, params=params
, verify=False, timeout=self.timeout, *args, **kwargs)
else:
response = self.client.get(url, headers=headers, data=data, json=json, params=params
, timeout=self.timeout, *args, **kwargs)
response.encoding = 'utf-8'
print(f'{response.json()}')
return response
import allure
from unittest import TestCase
from library.httpclient import HttpClient
@allure.feature('Test Weather api')
class Weather(TestCase):
"""Weather api test cases"""
def setUp(self):
"""Setup of the test"""
self.host = 'http://www.weather.com.cn'
self.ep_path = '/data/cityinfo'
self.client = HttpClient()
@allure.story('Test of ShenZhen')
def test_1(self):
city_code = '101280601'
exp_city = '深圳'
self._test(city_code, exp_city)
@allure.story('Test of BeiJing')
def test_2(self):
city_code = '101010100'
exp_city = '北京'
self._test(city_code, exp_city)
@allure.story('Test of ShangHai')
def test_3(self):
city_code = '101020100'
exp_city = '上海'
self._test(city_code, exp_city)
def _test(self, city_code, exp_city):
url = f'{self.host}{self.ep_path}/{city_code}.html'
response = self.client.Get(url=url)
act_city = response.json()['weatherinfo']['city']
print(f'Expect city = {exp_city}, while actual city = {act_city}')
self.assertEqual(exp_city, act_city, f'Expect city = {exp_city}, while actual city = {act_city}')
allure-pytest
appium-python-client
pytest
pytest-testconfig
requests
selenium
urllib3
**接口功能自动化测试程序(Python版)**
运行环境:
- python3
- pytest
- allure report
- git
依赖准备:
pip install allure-pytest
运行命令:
pytest -sv test/weather_test.py --alluredir ./allure-results
添加命令加载Python库:pip3.9 install -r requirements.txt
添加运行命令:pytest -sv test/weather_test.py -alluredir ./allure-results
最后: 可以关注公众号:伤心的辣条 ! 进去有许多资料共享!资料都是面试时面试官必问的知识点,也包括了很多测试行业常见知识,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。
如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!
转行面试,跳槽面试,软件测试人员都必须知道的这几种面试技巧!
面试经:一线城市搬砖!又面软件测试岗,5000就知足了…
面试官:工作三年,还来面初级测试?恐怕你的软件测试工程师的头衔要加双引号…
什么样的人适合从事软件测试工作?
那个准点下班的人,比我先升职了…
测试岗反复跳槽,跳着跳着就跳没了…