环境搭建:
需要配置WebTours网站
代码实现:
# 导入包
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
import unittest
driver = None
class Reg(unittest.TestCase):
@classmethod
def setUpClass(cls):
global driver
driver = webdriver.Chrome()
@classmethod
def tearDownClass(cls):
driver.quit()
def setUp(self):
print("test function")
def tearDown(self):
print("function over")
def testOpenWeb(self):
driver.get("http://127.0.0.1:1080/WebTours/")
# 等待3秒
sleep(3)
def testRegister(self):
driver.switch_to.default_content()
driver.switch_to.frame("body")
driver.switch_to.frame("info")
driver.find_element_by_link_text("sign up now").click()
sleep(3)
# 跳转页面,还是需要切换框架
driver.switch_to.default_content()
driver.switch_to.frame("body")
driver.switch_to.frame("info")
driver.find_element_by_name("username").send_keys("test")
driver.find_element_by_name("password").send_keys('123456')
driver.find_element_by_name("passwordConfirm").send_keys('123456')
driver.find_element_by_name("register").click()
sleep(3)
if __name__=="__main__":
# verbosity=2 显示详细信息,可以省略,测试信息不详细
unittest.main(verbosity=2)