赞
踩
篇幅较长,要耐心阅读哦~
- 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=<Your chrome driver path>
- 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
Selenium自动化测试演练
配置Allure报告
Jenkins配置
配置git 地址链接(ssh格式),添加Checkout to sub-directory
添加命令加载python库:pip install -r requirements.txt
添加运行命令:pytest -sv test/web_ut.py
其中. ~/.bash_profile是为了获取本机的环境变量
cd iSelenium_Python:切换到项目目录
添加运行参数,控制是否为有界面运行,此步骤之前可以先试运行程序,没有错误后再添加
二、接口自动化测试
接口自动化测试项目介绍
- 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
Jenkins配置
添加命令加载Python库:pip3.9 install -r requirements.txt
添加运行命令:pytest -sv test/weather_test.py -alluredir ./allure-results
配置Allure Report插件
post-build Actions
运行Jenkins
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。