1.创建unitWebtours.py
将unitWebtours.py放在test文件夹下
unitWebtours.py
代码实现
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
import unittestdriver = Noneclass Webtours(unittest.TestCase):@classmethoddef setUpClass(cls):global driverdriver = webdriver.Chrome()@classmethoddef tearDownClass(cls):driver.quit()def test1OpenWeb(self):u'''打开网站'''driver.get("http://127.0.0.1:1080/WebTours/")# 等待3秒sleep(3)driver.switch_to.default_content()driver.switch_to_frame("body")driver.switch_to_frame("info")actual = driver.find_element_by_tag_name("body").text# expect = 'Welcome to the Web Tours site.'# # 精确匹配用in 否则用in # if expect in actual:# print("打开网站:成功")# else:# print("打开网站:失败") def test2Loginin(self):u'''登录'''driver.switch_to.default_content()driver.switch_to_frame("body")driver.switch_to_frame("navbar")# 登录yhm = 'admin'mm = '123456'# 输入用户名driver.find_element_by_name("username").send_keys(yhm)# 输入密码driver.find_element_by_name("password").send_keys(mm)# 点击登录driver.find_element_by_name("login").click()sleep(3)# 切换框架driver.switch_to.default_content()driver.switch_to_frame("body")driver.switch_to_frame("info")expect = "Welcome, "+yhmactual = driver.find_element_by_tag_name("body").text# # 精确匹配用in 否则用in # if expect in actual:# print("登录:成功")# else:# print("登录:失败")def test3Search(self):u'''搜票'''driver.switch_to.default_content()driver.switch_to_frame("body")driver.switch_to_frame("navbar")driver.find_element_by_xpath('//img[@alt="Search Flights Button"]').click()sleep(3)def test4Book(self):u'''订票'''# 预定票driver.switch_to.default_content()driver.switch_to_frame("body")driver.switch_to_frame("info")Select(driver.find_element_by_name("depart")).select_by_visible_text="London"# select_by_value 值是看不见的Select(driver.find_element_by_name("arrive")).select_by_visible_text="Paris"driver.find_element_by_name("findFlights").click()sleep(1)# 查看预定的航班信息driver.find_elements_by_name("outboundFlight")[0].click()driver.find_element_by_name("reserveFlights").click()sleep(1)# 支付信息driver.find_element_by_name("buyFlights").click()sleep(1)# 切换框架expect = "Flight 240 leaves London for Paris"actual = driver.find_element_by_tag_name("body").text# 精确匹配用in 否则用in # if expect in actual:# print("订票:成功")# else:# print("订票:失败")#预定下一个driver.find_element_by_name("Book Another").click()sleep(1)def test5Logoff(self):u'''注销'''# 切换框架driver.switch_to.default_content()driver.switch_to_frame("body")driver.switch_to_frame("navbar")driver.find_element_by_xpath('//img[@alt="SignOff Button"]').click()sleep(1)# 切换框架driver.switch_to.default_content()driver.switch_to_frame("body")driver.switch_to_frame("info")expect = "sign up now"actual = driver.find_element_by_tag_name("body").text# 精确匹配用in 否则用in # if expect in actual:# print("注销:成功")# else:# print("注销:失败")if __name__=="__main__":# verbosity=2 显示详细信息,可以省略,测试信息不详细unittest.main(verbosity=2)
2.使用测试套件运行测试函数
代码实现
import unittestsuite = unittest.TestSuite()
tests = unittest.defaultTestLoader.discover('./test',pattern='unit*.py')
suite.addTests(tests)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)