1、出错代码
from selenium import webdriver
import unittest
import time
class YoudaoTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
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)
print("youdao ok")
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main(verbosity=2)
2、以下代码不会报错
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30) # 隐性等待时间为30秒
if __name__=='__main__':
# #执行测试
# runner = unittest.TextTestRunner()
# runner.run(discover)
suite = unittest.TestSuite()# 1\构造用例集
suite.addTest(BaiduTest("test_baidu")) # 2、执行顺序是安加载顺序
suite.addTest(YoudaoTest("test_youdao"))
#suite.addTest(UC7TestCase("testDeleteFolder"))
# 执行测试
runner = unittest.TextTestRunner()# #3、实例化runner类
runner.run(suite) # 4、执行测试
def tearDown(self):
self.driver.quit()