日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

怎样从0开始搭建一个测试框架_1

發布時間:2025/3/20 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 怎样从0开始搭建一个测试框架_1 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


這一步我們用到了selenium的基本的知識,以及一些unittest和PyYaml庫的內容,有問題的同學可以參考我之前的博客:
Python Selenium自動化測試詳解
Python必會的單元測試框架 —— unittest
自動化項目配置或用例文件格式推薦–yaml

我們先創建一個簡單的腳本吧,在test文件夾創建test_baidu.py:

import os import time from selenium import webdriver from selenium.webdriver.common.by import ByURL = "http://www.baidu.com" base_path = os.path.dirname(os.path.abspath(__file__)) + '\..' driver_path = os.path.abspath(base_path+'\drivers\chromedriver.exe')locator_kw = (By.ID, 'kw') locator_su = (By.ID, 'su') locator_result = (By.XPATH, '//div[contains(@class, "result")]/h3/a')driver = webdriver.Chrome(executable_path=driver_path) driver.get(URL) driver.find_element(*locator_kw).send_keys('selenium 灰藍') driver.find_element(*locator_su).click() time.sleep(2) links = driver.find_elements(*locator_result) for link in links:print(link.text) driver.quit()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

腳本打開chrome,輸入“selenium 灰藍”,然后把所有結果中的標題打印出來。

如果想要搜索“Python selenium”,是不是要再創建一個腳本?還是把原來的腳本修改一下?

或者我們可以用unittest來改一下,把兩次搜索分別寫一個測試方法:

import os import time import unittest from selenium import webdriver from selenium.webdriver.common.by import Byclass TestBaiDu(unittest.TestCase):URL = "http://www.baidu.com"base_path = os.path.dirname(os.path.abspath(__file__)) + '\..'driver_path = os.path.abspath(base_path+'\drivers\chromedriver.exe')locator_kw = (By.ID, 'kw')locator_su = (By.ID, 'su')locator_result = (By.XPATH, '//div[contains(@class, "result")]/h3/a')def setUp(self):self.driver = webdriver.Chrome(executable_path=self.driver_path)self.driver.get(self.URL)def tearDown(self):self.driver.quit()def test_search_0(self):self.driver.find_element(*self.locator_kw).send_keys('selenium 灰藍')self.driver.find_element(*self.locator_su).click()time.sleep(2)links = self.driver.find_elements(*self.locator_result)for link in links:print(link.text)def test_search_1(self):self.driver.find_element(*self.locator_kw).send_keys('Python selenium')self.driver.find_element(*self.locator_su).click()time.sleep(2)links = self.driver.find_elements(*self.locator_result)for link in links:print(link.text)if __name__ == '__main__':unittest.main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

現在,我們把配置抽出來放到config.yml中:

URL: http://www.baidu.com
  • 1

為了讀取yaml文件,我們需要一個封裝YamlReader類,在utils中創建file_reader.py文件:

import yaml import osclass YamlReader:def __init__(self, yamlf):if os.path.exists(yamlf):self.yamlf = yamlfelse:raise FileNotFoundError('文件不存在!')self._data = None@propertydef data(self):# 如果是第一次調用data,讀取yaml文檔,否則直接返回之前保存的數據if not self._data:with open(self.yamlf, 'rb') as f:self._data = list(yaml.safe_load_all(f)) # load后是個generator,用list組織成列表return self._data
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

而且我們需要一個Config類來讀取配置,config.py:

""" 讀取配置。這里配置文件用的yaml,也可用其他如XML,INI等,需在file_reader中添加相應的Reader進行處理。 """ import os from utils.file_reader import YamlReader# 通過當前文件的絕對路徑,其父級目錄一定是框架的base目錄,然后確定各層的絕對路徑。如果你的結構不同,可自行修改。 # 之前直接拼接的路徑,修改了一下,用現在下面這種方法,可以支持linux和windows等不同的平臺,也建議大家多用os.path.split()和os.path.join(),不要直接+'\\xxx\\ss'這樣 BASE_PATH = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0] CONFIG_FILE = os.path.join(BASE_PATH, 'config', 'config.yml') DATA_PATH = os.path.join(BASE_PATH, 'data') DRIVER_PATH = os.path.join(BASE_PATH, 'drivers') LOG_PATH = os.path.join(BASE_PATH, 'log') REPORT_PATH = os.path.join(BASE_PATH, 'report')class Config:def __init__(self, config=CONFIG_FILE):self.config = YamlReader(config).datadef get(self, element, index=0):"""yaml是可以通過'---'分節的。用YamlReader讀取返回的是一個list,第一項是默認的節,如果有多個節,可以傳入index來獲取。這樣我們其實可以把框架相關的配置放在默認節,其他的關于項目的配置放在其他節中。可以在框架中實現多個項目的測試。"""return self.config[index].get(element)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

修改test_baidu.py:

import time import unittest from selenium import webdriver from selenium.webdriver.common.by import By from utils.config import Config, DRIVER_PATHclass TestBaiDu(unittest.TestCase):URL = Config().get('URL')locator_kw = (By.ID, 'kw')locator_su = (By.ID, 'su')locator_result = (By.XPATH, '//div[contains(@class, "result")]/h3/a')def setUp(self):self.driver = webdriver.Chrome(executable_path=DRIVER_PATH + '\chromedriver.exe')self.driver.get(self.URL)def tearDown(self):self.driver.quit()def test_search_0(self):self.driver.find_element(*self.locator_kw).send_keys('selenium 灰藍')self.driver.find_element(*self.locator_su).click()time.sleep(2)links = self.driver.find_elements(*self.locator_result)for link in links:print(link.text)def test_search_1(self):self.driver.find_element(*self.locator_kw).send_keys('Python selenium')self.driver.find_element(*self.locator_su).click()time.sleep(2)links = self.driver.find_elements(*self.locator_result)for link in links:print(link.text)if __name__ == '__main__':unittest.main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

我們已經把配置分離出來了,雖然現在看起來似乎很麻煩,但是想想如果你有50個用例文件甚至更多,一旦項目URL變了,你還要一個個去修改嗎?

所有的代碼我都放到了GITHUB上傳送,可以自己下載去學習,有什么好的建議或者問題,可以留言或者加我的QQ群:455478219討論。

總結

以上是生活随笔為你收集整理的怎样从0开始搭建一个测试框架_1的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。