
篇幅較長,要耐心閱讀哦~
基礎知識簡要回顧
- 持續集成、持續交付的好處與產生的必然性
- Jenkins服務的搭建方法
- Jenkins節點管理與用戶權限
- Jenkins插件
- Jenkins父子多任務關聯運行
- Jenkins報警機制
目錄
- SeleniumUI自動化測驗持續集成演練
- 介面自動化測驗持續集成演練
一、SeleniumUI自動化測驗持續集成演練
Selenium自動化測驗專案介紹
- 用例業務內容:測驗百度網首頁搜索關鍵詞之后,跳轉頁面標題的正確性
- python代碼實作
- Web UI 測驗框架 Selenium (WebDriver)
- 自動化測驗框架pytest
- 開發工具 PyCharm
- 原始碼位置:https://github.com/princeqjzh/iSelenium_Python
測驗程序動作:
- 訪問首頁,搜索“今日頭條”,驗證正確性
- 訪問首頁,搜索“王者榮耀”,驗證正確性
#######測驗代碼知識點: - 運行類需繼承unittest.TestCase類
- setUp()測驗準備方法,用于環境初始化
- tearDown()測驗結束方法,用于環境清理
- 所有測驗執行方法需要以test_開頭
- 兩個測驗動作執行方法 test_webui_1(),test_webui_2()
- get_config()方法讀取組態檔
- 運行程式之前需要將組態檔iselenium.ini 復制/粘貼到自己測驗執行環境的user.home目錄下,并按照自己機器的實際路徑配置 chrome_driver的路徑
Demo代碼工程講解
- 開發工具PyCharm
- 本地IDE運行測驗類可以創建py.test運行方法
測驗代碼
- 目錄樹

- web_ut.py 檔案
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')
- iselenium.ini 組態檔,組態檔需放到系統的家目錄下,并添加chromedriver檔案路徑
[driver]
chrome_driver=<Your chrome driver path>
- requirements.txt 依賴包檔案
allure-pytest
appium-python-client
pytest
pytest-testconfig
requests
selenium
urllib3
- README.md 幫助檔案
**Selenium 自動化測驗程式(Python版)**
運行環境:
- selenium web driver
- python3
- pytest
- git
組態檔:iselenium.ini
- 將組態檔復制到本地磁盤的[user.home]目錄
- 填入設備的chromwebdriver檔案的全路徑
運行命令:
pytest -sv test/web_ut.py --alluredir ./allure-results
代碼clone
- 將iSelenium_Python原始碼克隆到你的本地
- 可以先Fork然后再克隆你Fork之后的原始碼專案(原始碼修改后可以push到github)
- 也可以直接下載(原始碼修改后不能push到github)
- 克隆參考代碼:git clone git@github.com:princeqjzh/iSelenium_Python.git
額外知識點:chrome driver怎么找?
-
本機需要安裝chrome瀏覽器
-
Chrome driver版本與chrome瀏覽器版本有支持對應關系
-
Chrome driver 下載參考網站:http://npm.taobao.org/mirrors/chromedriver/


Selenium自動化測驗演練
- 運行環境可以與Jenkins同一臺機器,也可以與Jenkins不同機器
- 實體使用與Jenkins同一臺機器便于演示
- 運行環境上需要事先配置python3運行環境,保證pytest可以運行
- 確保環境配置是OK的,可以運行Selenium的web自動化測驗程式
配置Allure報告
- Allure Report -更好看一些
- 環境準備:
運行環境上需要安裝allure report運行環境
Jenkins allure report 插件 - 環境準備:
Python依賴準備:pip install allure-pytest - 添加代碼:
@allure.feature(’ feature name’)
@allure.story(‘story name’) - 運行命令:
pytest -sv test/web_ut.py --alluredir ./allure-results
Jenkins配置
- Jenkins中新建一個自由風格的專案


-
配置git 地址鏈接(ssh格式),添加Checkout to sub-directory


-
添加命令加載python庫:pip install -r requirements.txt
-
添加運行命令:pytest -sv test/web_ut.py
其中. ~/.bash_profile是為了獲取本機的環境變數
cd iSelenium_Python:切換到專案目錄

-
添加運行引數,控制是否為有界面運行,此步驟之前可以先試運行程式,沒有錯誤后再添加

- 添加Allure Report到 Post-build Actions中用于展示測驗結果


進行構建
- 查看控制臺輸出

- 查看allure報告


- 查看allure曲線圖(至少運行兩次)

本章小結
- 自動化測驗實體:Python代碼的 Selenium_Python專案
- 利用組態檔記錄環境引數,保證相同的代碼可以在不同環境上去運行
- Selenium 驅動UI測驗運行
- 利用引數控制是否帶界面運行
- 自動化測驗框架pytest控制測驗程式的生命周期
- Allure Report生成測驗報告
- Jenkins任務集成整個自動化測驗運行程序
二、介面自動化測驗
介面自動化測驗專案介紹
- 介面測驗應用:http://www.weather.com.cn/data/cityinfo/
- 介面功能:獲得對應城市的天氣預報
- 原始碼:Python
- 功能包:HttpClient
- 請求方法:Get
- 自動化測驗框架:pytest
- 開發工具:PyCharm
- 原始碼位置:https://github.com/princeqjzh/iInterface_python
業務程序
- 請求介面傳入對應引數
- 決議回傳JSON串
- 獲取對應[城市]回傳值
- 校驗結果正確性
- 輸出報告

介面自動化測驗專案原始碼講解
- 打開PyCharm
- HttpClient:網路Http請求類
- Weather():測驗用例類
- README.md:說明
- 目錄樹

- jmx是與性能測驗相關的,這里忽略
- httpclient.py 封裝和請求方法相關的函式
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
- weather_test.py 測驗檔案
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}')
- requirements.txt 依賴庫
allure-pytest
appium-python-client
pytest
pytest-testconfig
requests
selenium
urllib3
- README.md 說明檔案
**介面功能自動化測驗程式(Python版)**
運行環境:
- python3
- pytest
- allure report
- git
依賴準備:
pip install allure-pytest
運行命令:
pytest -sv test/weather_test.py --alluredir ./allure-results
- 模擬介面測驗用例通過:actual _value == expect _value
- 模擬介面測驗用例失敗:actual value != expect_ _value
- 本地代碼講解和運行演示-Demo
Jenkins配置
- 復習知識點:Slave節點配置管理演示
- 權限配置
- Known host操作:Know host 是機器的ssl的校驗機制,在機器的home目錄下一般有.ssh的目錄,該目錄下有known hosts 檔案,該檔案存放的是被當前機器所信任的服務器ip




- Jenkins中創建自由風格任務

- 添加Git地址

- 添加sub-directory

-
添加命令加載Python庫:pip3.9 install -r requirements.txt
-
添加運行命令:pytest -sv test/weather_test.py -alluredir ./allure-results

-
配置Allure Report插件

-
post-build Actions

-
運行Jenkins


本章小結
- 自動化測驗實體:Python代碼
- 利用Python常用package中的類發起介面請求、獲取介面回傳值、決議JSON欄位、校驗結果正確性
- 利用pytest框架來運行介面測驗,控制程式的生命周期
- Allure report測驗結果展示
- Jenkins任務:原始碼同步、運行任務、展示測驗報告、發送郵件
三、總結
- Web UI自動化測驗持續集成
- 介面自動化測驗持續集成
- 通過引數來控制運行方式
- 控制有界面or無界面運行
- Allure Report展示測驗結果報告
- Jenkins + python + allure

最后: 可以關注公眾號:傷心的辣條 ! 進去有許多資料共享!資料都是面試時面試官必問的知識點,也包括了很多測驗行業常見知識,其中包括了有基礎知識、Linux必備、Shell、互聯網程式原理、Mysql資料庫、抓包工具專題、介面測驗工具、測驗進階-Python編程、Web自動化測驗、APP自動化測驗、介面自動化測驗、測驗高級持續集成、測驗架構開發測驗框架、性能測驗、安全測驗等,
如果我的博客對你有幫助、如果你喜歡我的博客內容,請 “點贊” “評論” “收藏” 一鍵三連哦!
好文推薦
轉行面試,跳槽面試,軟體測驗人員都必須知道的這幾種面試技巧!
面試經:一線城市搬磚!又面軟體測驗崗,5000就知足了…
面試官:作業三年,還來面初級測驗?恐怕你的軟體測驗工程師的頭銜要加雙引號…
什么樣的人適合從事軟體測驗作業?
那個準點下班的人,比我先升職了…
測驗崗反復跳槽,跳著跳著就跳沒了…
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/299447.html
標籤:其他
上一篇:HS后端部署示例
下一篇:springboot整合~swagger~kafka~nginx~redis~mysql(在linux服務器環境下部署運行測驗)
