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()

测试结果

这里写图片描述

  • 1
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值