1. 程式人生 > 其它 >同時執行不同的測試用了,生成報告併發送至郵箱

同時執行不同的測試用了,生成報告併發送至郵箱

技術標籤:python+seleniumseleniumpython

這個專案執行成功,設計到的功能點:生成測試報告,並自動已郵件的形式傳送到qq郵箱,
利用模組化模式批量執行兩個不同的測試用例

遇到的坑:測試用例的報告一定要存放在其他路徑,不能存放在專案的相關路徑,
這樣自動傳送郵件時會提取的到,不然就會一直無法自動傳送郵件

執行的檔案:all_tests.py

all_tests.py 檔案內容


#coding=utf-8
import os
import unittest
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from datetime import datetime
import time
from BSTestRunner import BSTestRunner #引入BSTestRunner包

#自動傳送郵件
def send_email(new_report):
    #讀取測試報告中的內容作為郵件的內容
    with open(new_report,'r',encoding='utf-8') as f:
        mail_body = f.read()
    #發件人地址
    from_addr = '
[email protected]
' #收件人地址 to_addr = '[email protected]' #傳送郵箱的伺服器地址 mail_server = 'smtp.qq.com' #郵件的標題 subject =u'qq登入測試報告' #發件人的郵箱地址 username = '[email protected]' password = 'puiqzotbyjirjdia' #郵箱的內容和標題 message = MIMEText(mail_body,'html','utf-8') message['Subject'] = Header(subject,charset='utf-8') #傳送郵件 smtp = smtplib.SMTP() smtp.connect(mail_server) smtp.login(username,password) smtp.sendmail(from_addr,to_addr.split(','),message.as_string()) smtp.quit() #獲取最新報告的地址 def acquire_report_address(reports_address): #測試報告資料夾中的所有檔案加入到列表 test_reports_list = os.listdir(reports_address) #按照升序排序生成新的列表 new_test_reports_list = sorted(test_reports_list) #獲取最新的測試報告 the_last_report = new_test_reports_list[-1] #最新的測試報告的地址 the_last_report_address = os.path.join(reports_address,the_last_report) return the_last_report_address if __name__ == '__main__': # 生成測試報告併發送郵件 #測試報告資料夾地址 test_reports_address = u'C:\\Users\\Admin\\Desktop\\測試報告' #測試用例的資料夾地址 test_cases_dir = r'C:\\Users\\Admin\\PycharmProjects\\pythonProject3\\lianxi\\test' #獲取所有的測試用例 test_cases = unittest.defaultTestLoader.discover(test_cases_dir,pattern='*.py') #獲取當前時間 now = datetime.now().strftime('%Y%m%d%H%MM%f') #生成以當前時間命名的測試報告檔名 test_report_name = r'{}\report_{}.html'.format(test_reports_address,datetime.now().strftime('%Y%m%d%H%M%f')) #生成以當前時間命名的測試報告檔案 file_report = open(test_report_name,'w',encoding='utf-8') #生成html形式的報告 runner = BSTestRunner(stream=file_report,title=u'測試報告',description=u'QQ登入測試報告結果:') #執行 runner.run(test_cases) #關閉開啟的測試報告檔案 file_report.close() time.sleep(5) #查詢最新生成的測試報告地址 new_report_addr = acquire_report_address(test_reports_address) #自動傳送郵件 send_email(new_report_addr)

baidu.py 檔案內容

#coding=utf-8  
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class Baidu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.baidu.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
    #百度搜索用例
    def test_baidu_search(self):
        u"""百度搜索"""
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("kw").send_keys("selenium webdriver")
        driver.find_element_by_id("su").click()
        time.sleep(2)
        driver.close()
    #百度設定用例
    def test_baidu_set(self):
        u"""百度設定"""
        driver = self.driver
        #進入搜尋設定頁
        driver.get(self.base_url + "/gaoji/preferences.html")
        #設定每頁搜尋結果為50條
        m=driver.find_element_by_name("NR")
        m.find_element_by_xpath("//option[@value='50']").click()
        time.sleep(2)
        #儲存設定的資訊
        driver.find_element_by_xpath("/html/body/form/div/input").click()
        time.sleep(2)
        driver.switch_to_alert().accept()
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

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

Login.py 檔案內容

# coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class Login(unittest.TestCase):
    def setUp(self):
        # 初始化驅動
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        # 設定要開啟的網頁連結
        self.url = 'http://192.168.96.207:8087/t6/'

        self.verificationErrors = []
        self.accept_next_alert = True

        # 登入用例

    def test_Login(self):
        u"""登入"""
        driver = self.driver
        driver.get(self.url + "/")
        # 取ID為elem_user的網頁元素(使用者名稱輸入元素)
        elem_user1 = driver.find_element_by_id('showAccount')
        elem_user1.click()
        elem_user = driver.find_element_by_id('userAccount')
        # 清空輸入
        elem_user.clear()
        # 鍵入使用者名稱
        elem_user.send_keys('ctt')
        # 取ID為elem_pass的網頁元素(密碼輸入元素)
        elem_pass1 = driver.find_element_by_id('showPassword')
        elem_pass1.click()
        elem_pass = driver.find_element_by_id('userPassword')
        # 清空輸入
        elem_pass.clear()
        # 鍵入密碼
        elem_pass.send_keys('123456')
        # 取ID為elem_login的登入按鈕
        elem_login = driver.find_element_by_id('dengluBtn')
        # 點選登入按鈕
        elem_login.click()
        time.sleep(5)
        # 通過frame方式定位
        driver.switch_to.frame("id_iframe")
        # 獲取需要斷言的文字'yanzheng=蔡婷婷'
        yanzheng = driver.find_element_by_id('userName').text
        print(yanzheng)
        # 點選使用者
        driver.find_element_by_id('id_login_user').click()
        # 點選退出
        driver.find_element_by_id('id_a_exit').click()
        time.sleep(2)
        # 退出frame方式定位
        driver.switch_to.default_content()
        # 點選確定
        driver.find_element_by_class_name('layui-layer-btn0').click()
        time.sleep(3)

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

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

結構目錄