Selenium2+python Automation 48-Login method (parametric)

Source: Internet
Author: User

Objective

Log In this scenario is often used when writing a use case, we can encapsulate the login as a method, and then the account and password parameterization, so that after the use of the login, just call this method on the line

First, login method

1. Enter the account number, enter the password, click the login button three steps to write a method

2. Then parameterize the input account and password

Second, use cases

1. The following use case can invoke the previously written login method, so that you do not have to go to the login process

2. To determine if the login is successful, I am here to take the account name after successful login

Third, the judgment method encapsulation

1. If you use the above method to judge, there is a flaw, when the login is unsuccessful, the page will not jump, so the search element will report an exception:

NoSuchElementException:Message:Unable to locate element: {"method": "id", "selector": "Lnk_current_user"}

2. This is the simple encapsulation of the judgment method: get to the account name return ture; not get to return false

Iv. Post-optimization cases

1. The optimized login case is as follows, so it looks clearer

V. Reference code

1 #Coding:utf-82  fromSeleniumImportWebdriver3 ImportUnitTest4 Import Time5 classBolg (unittest. TestCase):6U" "Login Blog" "7     defsetUp (self):8Self.driver =Webdriver. Firefox ()9URL ="Https://passport.cnblogs.com/user/signin"Ten self.driver.get (URL) OneSelf.driver.implicitly_wait (30) A  -     deflogin (self, username, PSW): -U" "Here is a login method, account number and password parameterization" " theSELF.DRIVER.FIND_ELEMENT_BY_ID ("INPUT1"). Send_keys (username) -SELF.DRIVER.FIND_ELEMENT_BY_ID ("Input2"). Send_keys (PSW) -SELF.DRIVER.FIND_ELEMENT_BY_ID ("signin"). Click () -Time.sleep (3) +  -     defis_login_sucess (self): +U" "determine whether to get the login account name" " A         Try: atText = self.driver.find_element_by_id ("Lnk_current_user"). Text -             Printtext -             returnTrue -         except: -             returnFalse -  in     deftest_01 (self): -U" "Login Case Reference: Account number, password settings" " toSelf.login (U"Shanghai-leisurely", u"xxxx")#Call the Login method +         #Judging Results -result =self.is_login_sucess () the self.asserttrue (Result) *  $     deftest_02 (self):Panax NotoginsengU" "Login Case Reference: Account number, password settings" " -Self.login (U"Shanghai-leisurely", u"xxxx")#Call the Login method the         #Judging Results # QQ Group: 232607095 +result =self.is_login_sucess () A self.asserttrue (Result) the  +     #def test_01 (self): -     #U "Login Case Reference: Account number, password set" $     #self.login (U "Shanghai-Yo", U "xxxx") # Invoke Login method $     ## Get the account name after login -     #Text = self.driver.find_element_by_id ("Lnk_current_user"). Text -     #Print Text the     ## assert actual results consistent with expected results -     #self.assertequal (text, U "Shanghai-leisurely")Wuyi     # the     #def test_02 (self): -     #U "Login Case Reference: Account number, password set" Wu     #self.login (U "Shanghai-Yo", U "oooo") # Call the login method -     ## Get the account name after login About     #Text = self.driver.find_element_by_id ("Lnk_current_user"). Text $     #Print Text # AC QQ Group: 232607095 -     ## assert actual results consistent with expected results -     #self.assertequal (text, U "Shanghai-leisurely") -  A     defTearDown (self): + self.driver.quit () the  - if __name__=="__main__": $Unittest.main ()

Selenium2+python Automation 48-Login method (parametric)

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.