From the blog: Shanghai-leisurely
Original address: http://www.cnblogs.com/yoyoketang/tag/unittest/
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
# Coding:utf-8
From selenium import Webdriver
Import UnitTest
Import time
Class Bolg (UnitTest. TestCase):
U ' Login blog '
def setUp (self):
Self.driver = Webdriver. Firefox ()
url = "Https://passport.cnblogs.com/user/signin"
Self.driver.get (URL)
Self.driver.implicitly_wait (30)
def login (self, username, PSW):
U ' ' here wrote a login method, account number and password parameterization '
self.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)
def is_login_sucess (self):
U "" to determine whether to get to the login account name '
Try
Text = self.driver.find_element_by_id ("Lnk_current_user"). Text
Print text
Return True
Except
Return False
def test_01 (self):
U "Login Case Reference: Account number, password set"
Self.login (U "Shanghai-Yo", U "xxxx") # Invoke Login method
# Judging Results
result = Self.is_login_sucess ()
Self.asserttrue (Result)
def test_02 (self):
U "Login Case Reference: Account number, password set"
Self.login (U "Shanghai-Yo", U "xxxx") # Invoke Login method
# judgment Result # QQ Group: 232607095
result = Self.is_login_sucess ()
Self.asserttrue (Result)
# def test_01 (self):
# u "Login Case reference: Account, password Set" "
# self.login (u "Shanghai-Yo", U "xxxx") # Call the login method
# # Get the account name after login
# text = self.driver.find_element_by_id ("Lnk_current_user"). Text
# Print Text
# # assert that actual results are consistent with expected results
# self.assertequal (Text, u "Shanghai-Yo")
#
# def test_02 (self):
# u "Login Case reference: Account, password Set" "
# self.login (u "Shanghai-Yo", U "oooo") # Call the login method
# # Get the account name after login
# text = self.driver.find_element_by_id ("Lnk_current_user"). Text
# Print Text # AC QQ Group: 232607095
# # assert that actual results are consistent with expected results
# self.assertequal (Text, u "Shanghai-Yo")
def tearDown (self):
Self.driver.quit ()
if __name__ = = "__main__":
Unittest.main ()
Selenium2+python Automation 48-Login method (parametric) "Reprint"