selenium+python測試全部用例

測試類1:163郵箱

文件名:simulate163.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  WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

class Login():
    def __init__(self):
        self.driver=webdriver.Chrome()
        self.driver.get("http://mail.163.com/")
    def login(self,username,pw):
        element=WebDriverWait(self.driver,30,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[@id='x-URS-iframe']")))
        self.driver.switch_to.frame("x-URS-iframe")
        inputText=self.driver.find_element(By.XPATH,"//*[@id='account-box']//div[2]//input")
        inputText.send_keys(username)
        password=self.driver.find_element(By.XPATH,"//*[@id='login-form']//div//div[3]//div[2]//input[2]")
        password.send_keys(pw)
        password.send_keys(Keys.ENTER)

    def logout(self):
        self.driver.find_element_by_link_text('退出').click()
        time.sleep(5)

測試用例1:測試163郵箱

文件名:test_163.py

# -*- coding: utf-8 -*-
import unittest
from simulate163 import Login
from HTMLTestRunner import HTMLTestRunner
import time

class testLogin(unittest.TestCase):
    '''163郵箱測試'''
    @classmethod
    def setUpClass(cls):
        print('開始測試')
        testLogin.login = Login()

    @classmethod
    def tearDownClass(cls):
        print('結束測試')
        testLogin.login.driver.quit()

    def setUp(self):
        print('開始單個測試')

    def test_login(self):
        '''163郵箱登錄'''
        self.login.login(username='你的賬號',pw='你的密碼')
        title=self.login.driver.title
        self.assertEqual(title,'163網易免費郵--中文郵箱第一品牌')
        time.sleep(5)
    def test_logout(self):
        '''163郵箱退出登陸'''
        self.login.logout()
        title = self.login.driver.title
        self.assertEqual(title,'網易郵箱 - 您已成功退出郵箱')
        time.sleep(5)

    def tearDown(self):
        print('結束單個測試')

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

測試類2:測試嗶哩嗶哩視頻播放

文件名:simulateBili.py

'''
自動播放bilibili視頻
'''
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
from  selenium.webdriver.support.ui import  WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

class biliVideo():
    def __init__(self):
        self.driver=webdriver.Chrome()
        self.driver.get("https://www.bilibili.com/video/av16041375/")
    def playPause(self):
        video=WebDriverWait(self.driver,30,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[@id='bilibiliPlayer']/div[1]/div[2]/div[7]/video")))  # 找到視頻
        url=self.driver.execute_script("return arguments[0].currentSrc;",video)  # 打印視頻地址
        print(url)
        print("start")
        self.driver.execute_script("return arguments[0].play()",video)  # 開始播放
        time.sleep(15)
        print("stop")
        self.driver.execute_script("return arguments[0].pause()",video) # 暫停

測試用例2:測試嗶哩嗶哩

文件名:test_Bili.py

# -*- coding: utf-8 -*-
import unittest
from simulateBili import biliVideo
from HTMLTestRunner import HTMLTestRunner
import time

class testBili(unittest.TestCase):
    '''嗶哩嗶哩播放測試'''
    @classmethod
    def setUpClass(cls):
        testBili.bili=biliVideo()
        print('開始測試')

    @classmethod
    def tearDownClass(cls):
        print('結束測試')
        testBili.bili.driver.quit()

    def setUp(self):
        print('開始單個測試')

    def test_playPause(self):
        '''播放video和暫停測試'''
        self.bili.playPause()
        print(self.bili.driver.title)
        time.sleep(5)

    def tearDown(self):
        print('結束單個測試')
if __name__ == '__main__':
    unittest.main()

合成兩個測試並生成測試結果

# -*- coding: utf-8 -*-
import unittest,time
from HTMLTestRunner import HTMLTestRunner
from email.mime.text import MIMEText
from email.header import Header
import smtplib
test_dir='./'
discover=unittest.defaultTestLoader.discover(test_dir,pattern='test_*.py')

if __name__=='__main__':
    now=time.strftime('%Y-%m-%d %H_%M_%S')
    filename=test_dir+now+'result.html'
    fp=open(filename,'wb')
    runner=HTMLTestRunner(stream=fp,title='測試報告',description='用例執行情況')
    runner.run(discover)
    fp.close()

測試結果

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章