python+selenium自動化測試框架搭建


一個人學習不如和大家一起學習,群里有學習資料,歡迎你加入軟件測試交流群,群號:1062843742。

環境及使用軟件信息

  1. python 3
  2. selenium 3.13.0
  3. chromedriver
  4. HTMLTestRunner

說明:

selenium只需要再python環境下使用pip install 名稱即可進行對應的安裝。 安裝完成后可使用pip list查看自己的安裝列表信息。

chromedriver:版本需和自己的chrome瀏覽器對應,下載地址。 作用:對chrome瀏覽器進行驅動。

HTMLTestRunner:HTMLTestRunner是Python標准庫的unittest模塊的一個擴展。它生成易於使用的HTML測試報告。 

項目結構

項目主要包括以下幾個部分

 測試實例

import time,os
 
'''
可以配置全局參數,
'''
 

project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
#瀏覽器驅動存放路徑
chrome_driver_path = project_path + '\\driver\\chromedriver.exe'

 
# if __name__=='__main__':
# test1 = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)[0]), '.'))

testcase:演示百度搜索

# coding=utf-8
'''
Created on 2016-7-22
@author: Jennifer
Project:登錄百度測試用例
'''
from selenium import webdriver
import unittest, time

class BaiduTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30) #隱性等待時間為30秒
        self.base_url = "https://www.baidu.com"
    
    def test_baidu(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("unittest")
        driver.find_element_by_id("su").click()
        time.sleep(3)
        title=driver.title
        self.assertEqual(title, u"unittest_百度搜索") 

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

 

測試用例中的相關說明:

  1. setup():每個測試函數運行前運行

  2. teardown():每個測試函數運行完后執行

  3. setUpClass():必須使用@classmethod 裝飾器,所有test運行前運行一次

  4. tearDownClass():必須使用@classmethod裝飾器,所有test運行完后運行一次

測試用例執行runtest.py

使用HTMLTestRunner執行測試用例,並生成測試報告。
# conding :utf-8
 
import unittest
 
#構建測試集,包含src/testsuite目錄下的所有以test開頭的.py文件
suite = unittest.defaultTestLoader.discover(start_dir='測試用例存放的文件夾',pattern='test*.py')
 
if __name__=='__main__':
runner=HTMLTestRunner.HTMLTestRunner(stream=fb,title=u'郵件報告的描述',description=u'測試Team')
runner.run(suite)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM