测试类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()