使用
test_baidu.py
# coding=utf-8
'''
Created on 2020-04-10
@author: lee
Project:登录百度测试用例
'''
from selenium import webdriver
import unittest, time
# 创建测试用例类继承unittest.TestCase类
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_百度搜索")
@unittest.skip # 暂时跳过测试用例
def test_case1(self);
pass
def tearDown(self):
# 还原测试环境
self.driver.quit()
if __name__ == "__main__":
unittest.main() # 运行测试用例
test_youdao.py
# coding=utf-8
'''
Created on 2020-04-10
@author: lee
Project:使用有道翻译测试用例
'''
from selenium import webdriver
import unittest, time
class YoudaoTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30) #隐性等待时间为30秒
self.base_url = "http://www.youdao.com"
def test_youdao(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("translateContent").clear()
driver.find_element_by_id("translateContent").send_keys(u"你好")
driver.find_element_by_id("translateContent").submit()
time.sleep(3)
page_source=driver.page_source
self.assertIn( "hello",page_source)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
run.py
# coding=utf-8
'''
Created on 2020-04-10
@author: lee
Project:编写Web测试用例
'''
import unittest
from test_case import test_baidu
from test_case import test_youdao
# 构造测试集
suite = unittest.TestSuite()
# 将测试用例加载到测试集
suite.addTest(test_baidu.BaiduTest('test_baidu'))
suite.addTest(test_youdao.YoudaoTest('test_youdao'))
if __name__=='__main__':
# 执行测试
runner = unittest.TextTestRunner()
# 运行测试集
runner.run(suite)