一、免登录
在进行测试的过程中难免会遇到登录的情况,给测试工作添加了工作量,本文仅提供一些思路供参考
解决方式:手动请求中添加cookies、火狐的profile文件记录信息实现、人工介入、万能验证码、去掉验证码
1、手动在请求中添加cookies信息
1 url = "http://www.baidu.com" 2 driver = webdriver.Firefox() 3 driver.get(url) 4 time.sleep(3) 5 #添加cookies的方式 6 7 c1 = {'domain': '.baidu.com', 8 'name':'BDUSS', 9 'value': 'dyRlBucW9WOUhpNXducElPT1dlVHN1SFdFZUllTkJ-TEg5djNAAVe6gcaGFuc2hvdWthaQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHa68lh2uvJYU2', 10 'path': '/', 11 'httpOnly': True, 12 'secure':False 13 } 14 driver.add_cookie(c1) 15 time.sleep(3) 16 # print(driver.get_cookies()) #得到当前cookies信息 17 # driver.delete_all_cookies() #删除所有cookies信息 18 driver.refresh() 19 time.sleep(5) 20 21 driver.quit()
2、火狐的profile文件记录信息实现
1 #利用火狐profile文件的方式(前提:必须先手动登录一下,且需要勾选记住信息) 2 profile_ff = "C:/Users/Hanxiaobei/AppData/Roaming/Mozilla/Firefox/Profiles/ytw908g4.default" 3 4 fp = webdriver.FirefoxProfile(profile_ff) 5 driver = webdriver.Firefox(fp) 6 url = "http://www.baidu.com" 7 driver.get(url) 8 time.sleep(5) 9 driver.quit()
3、人工介入
1 driver.find_element_by_link_text("登录").click() 2 driver.find_element_by_id("TANGRAM__PSP_8__userName").send_keys("hankai") 3 driver.find_element_by_id("TANGRAM__PSP_8__password").send_keys("hankai") 4 time.sleep(15) #导入等待时间人工输入验证码 5 driver.find_element_by_id("TANGRAM__PSP_8__submit").click() 6 7 time.sleep(5) 8 driver.quit()
4、万能验证码、去掉验证码
万能验证码、去掉验证码需要开发的配合
二、等待
1、time模块
1 import time 2 3 time.sleep(5)
2、隐形等待
1 driver.implicitly_wait(20)
3、显性等待
1 url = "http://www.baidu.com" 2 driver = webdriver.Firefox() 3 driver.get(url) 4 #显性等待 5 kk = WebDriverWait(driver,10).until(lambda driver:driver.find_element_by_id("kw"),message="worry!") 6 kk.send_keys("测试")
三、unittest单元测试框架
简单的unittest框架代码如下:
1 import unittest,time 2 from selenium import webdriver 3 from selenium.webdriver.common.by import By 4 class Baidu(unittest.TestCase): 5 def setUp(self): 6 self.driver = webdriver.Chrome() 7 url = "http://www.baidu.com" 8 self.driver.get(url) 9 self.driver.implicitly_wait(20) 10 self.verificationErrors = [] 11 12 def tearDown(self): 13 self.driver.quit() 14 self.assertEqual([],self.verificationErrors,msg="验证失败") 15 16 def test_search(self): 17 self.driver.find_element(By.ID,"kw").send_keys("hanxiaobei") 18 self.driver.find_element(By.ID,"su").click() 19 time.sleep(5) 20 21 if __name__ == "__main__":
第一种形式: 22 #unittest.main()
第二种形式:
1 suite = unittest.TestSuite() 2 suite.addTest(Baidu("test_search")) 3 4 runner = unittest.TextTestRunner() 5 runner.run(suite)
可生成html报告的unittest框架代码如下:
1 from selenium import webdriver 2 import time 3 import unittest 4 import HTMLTestRunner 5 6 class Test_case(unittest.TestCase): 7 """测试类""" 8 def setUp(self): 9 self.url = "http://www.baidu.com" 10 self.driver = webdriver.Firefox() 11 self.driver.implicitly_wait(20) 12 self.verificationErrors = [] 13 14 def tearDown(self): 15 self.driver.quit() 16 self.assertEqual([],self.verificationErrors) 17 18 def test_sou1(self): 19 """测试搜索演示1""" 20 self.driver.get(self.url) 21 self.driver.find_element_by_id("kw").send_keys("测试") 22 self.driver.find_element_by_id("su").click() 23 self.driver.close() 24 time.sleep(5) 25 def test_sou2(self): 26 """测试搜索演示2""" 27 self.driver.get(self.url) 28 self.driver.find_element_by_id("k").send_keys("自动化测试") 29 self.driver.find_element_by_id("su").click() 30 self.driver.close() 31 time.sleep(5) 32 33 #生成一个运行测试用例集合 34 suite = unittest.TestSuite() 35 suite.addTest(Test_case('test_sou1')) 36 suite.addTest(Test_case('test_sou2')) 37 38 ''' 39 生成基于html的测试报告: 40 1定义一个文件的路径 41 2以写的方式进行打开文件 42 3调用htmltestrunner的方法生成测试报告 43 4运行测试集合 44 5关闭文件 45 ''' 46 report_file=".\\20170423_report.html" 47 fp = open(report_file,"wb") 48 runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title="搜索",description="测试搜索结果") 49 runner.run(suite) 50 fp.close()