Selenium based on Python web Automation Foundation Two--Login-free, wait, and UnitTest Unit test framework

Source: Internet
Author: User

One, free login
In the process of testing will inevitably encounter the situation of login, to add workload to the test work, this article only provides some ideas for reference

Workaround: Add cookies to the manual request, Firefox profile Records information implementation, manual intervention, Universal Verification Code, remove the verification code

1. Manually add the cookie information to the request
1URL ="http://www.baidu.com"2Driver =Webdriver. Firefox ()3Driver.Get(URL)4Time.sleep (3)5 #添加cookies的方式6 7C1 = {'Domain':'. baidu.com',8       'name':'Bduss',9       'value':'DYRLBUCW9WOUHPNXDUCELPT1DLVHN1SFDFZULLTKJ-TEG5DJNUFBJCQAAAAAAAAAAAEAAAAVE6GCAGFUC2HVDWTHAQAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHA68LH2UVJYU2',Ten       'Path':'/', One       'HttpOnly': True, A       'Secure': False -       } - Driver.add_cookie (C1) theTime.sleep (3) - # Print (Driver.get_cookies ()) #得到当前cookies信息 - # driver.delete_all_cookies () #删除所有cookies信息 - Driver.refresh () +Time.sleep (5) -  +Driver.quit ()

2, Firefox's profile record information implementation

1 #利用火狐profile文件的方式 (Prerequisite: You must log in manually first)2PROFILE_FF ="C:/users/aaron/appdata/roaming/mozilla/firefox/profiles/ytw908g4.default"3 4FP =Webdriver. Firefoxprofile (PROFILE_FF)5Driver =Webdriver. Firefox (FP)6URL ="http://www.baidu.com"7Driver.Get(URL)8Time.sleep (5)9Driver.quit ()
3. Manual intervention
1Driver.find_element_by_link_text ("Login"). Click ()2DRIVER.FIND_ELEMENT_BY_ID ("Tangram__psp_8__username"). Send_keys ("Hankai")3DRIVER.FIND_ELEMENT_BY_ID ("Tangram__psp_8__password"). Send_keys ("Hankai")4Time.sleep ( the) #人工输入验证码5DRIVER.FIND_ELEMENT_BY_ID ("Tangram__psp_8__submit"). Click ()6 7Time.sleep (5)8Driver.quit ()

4, universal Verification Code, remove the verification code

Universal verification Code, eliminate the verification code needs to develop the cooperation

Second, wait

1. Time Module
1 Import Time 2 3 time.sleep (5)

2, Invisible waiting

1 driver.implicitly_wait ()   

3. Explicit waiting

1URL ="http://www.baidu.com"2Driver =Webdriver. Firefox ()3Driver.Get(URL)4 #显性等待5KK = webdriverwait (Driver,Ten). Until (Lambda driver:driver.find_element_by_id ("kw"), message="worry!")6Kk.send_keys ("Test")

Three, UnitTest Unit test framework

1  fromSelenium Import Webdriver2 Import Time3 Import UnitTest4 Import Htmltestrunner5 6 classtest_case (unittest. TestCase):7     """Test Class"""8 def setUp (self):9Self.url ="http://www.baidu.com"TenSelf.driver =Webdriver. Firefox () OneSelf.driver.implicitly_wait ( -) ASelf.verificationerrors = [] -  - def tearDown (self): the self.driver.quit () - self.assertequal ([],self.verificationerrors) -  - def test_sou1 (self): +         """Test Search Demo 1""" -Self.driver.Get(Self.url) +SELF.DRIVER.FIND_ELEMENT_BY_ID ("kw"). Send_keys ("Test") ASELF.DRIVER.FIND_ELEMENT_BY_ID ("su"). Click () at self.driver.close () -Time.sleep (5) - def test_sou2 (self): -         """Test Search Demo 2""" -Self.driver.Get(Self.url) -SELF.DRIVER.FIND_ELEMENT_BY_ID ("k"). Send_keys ("Automated Testing") inSELF.DRIVER.FIND_ELEMENT_BY_ID ("su"). Click () - self.driver.close () toTime.sleep (5) +  - #生成一个运行测试用例集合 theSuite =UnitTest. TestSuite () *Suite.addtest (Test_case ('TEST_SOU1')) $Suite.addtest (Test_case ('TEST_SOU2'))Panax Notoginseng  - " " the to generate an HTML-based test report: + 1 Defining a path to a file A 2 Opening a file in a written manner the 3 Calling the Htmltestrunner method to generate a test report + 4 running a test collection - 5 closing Files $ " " $report_file=". \\20170423_report.html" -fp = open (Report_file,"WB") -Runner = Htmltestrunner.htmltestrunner (stream=fp,title="Search", description="Test Search Results") the Runner.run (Suite) -Fp.close ()

Selenium based on Python web Automation Foundation Two--Login-free, wait, and UnitTest Unit test framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.