Web login testing is a very common test! Manual test Everyone is familiar with, how to do automated login test it! In this paper, the author uses python+selenium combined with UnitTest Unit Test framework to conduct a simple but relatively complete cnblog automated login test, to provide you with a point of reference! The following includes the test code and each of the test cases:
The login test of the Cnblog is divided into the following situations: (1) The username and password are correct (2) The username is correct, the password is incorrect (3) The username is correct, the password is blank (4) The user name is wrong, the password is correct (5) The user name is empty, the password is correct (and the user name and password are This is not tested separately) "' import unittestfrom selenium import webdriverfrom time import Sleepclass logincase (unittest. TestCase): def setup (self): self.dr = webdriver. Chrome () self.dr.maximize_window () # Define Login Method def login (Self, username, password): self.dr.get (' Https://passport.cnblogs.com/user/signin ') #cnblog登录页面 self.dr.find_element_by_id (' input1 '). Send_keys (username) self.dr.find_element_by_id (' Input2 '). Send_keys (password) self.dr.find_element_by_id (' signin '). Click () def test_login_success (self): " Username, password correct ' ' ' self.login (' kemi_xxx ', ' kemi_xxxx ') # Correct user name and password sleep (3) link = self.dr.find_element_by_id (' Lnk_current_user ') self.asserttrue (' rookie can M ' in link.text) #用assertTrue (x) method to assert BOOL (x) is True user nickname in Lnk_current_user after successful login def test_login_pwd_ Error (self): "username correct, password incorrect" self.login (' kemi_xxx ', ' Kemi ') #正确用户名, error password sleep (2) error_message = self.dr.find_element_by_id (' tip_btn '). text self.assertin (' Username or password error ', error_message) #用assertIn (A, B) method to assert a in b ' username or password error ' in Error_message def test_ Login_pwd_null (self): "username is correct, password is empty" self.login (' kemi_xxx ', ') #密码为空 error_message = self.dr.find_element_by_id (' Tip_input2 ') .text self.assertequal (error_message, ' Please enter password ') #用assertEqual (A, B) method to assert a == b Please enter a password equal to Error_message def test_login_user_error (self): ' username wrong, password correct ' self.login (' kemixing ', ' kemi_xxx ') #密码正确, user name error sleep (2) error_message = self.dr.find_element_by_id (' tip_btn '). Text self.assertin (' The user does not exist ', error_message) # Use the Assertin (A, B) method to assert a in b def test_login_user_null (self): ' User name is blank, password is correct ' Self.login (', ' kemi_xxx ') #用户名为空, password correct error_ message = self.dr.find_element_by_id (' tip_input1 ') .text self.assertequal (error_message, ' Please enter login username ') #用assertEqual (A, B) method to assert a == B def teardown (self): sleep (2) print (' Auto test finished! ') self.dr.quit () if __name__ == ' __main__ ': unittest.main ()
The test results are as follows:
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/942023/201612/942023-20161227222759195-1658346406. PNG "style=" Border:0px;font-family:verdana, Arial, Helvetica, Sans-serif;font-size:14px;white-space:normal; Background-color:rgb (255,255,255); "/>
The test is as follows:
Correct user name and password login successful!
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://images2015.cnblogs.com/blog/942023/201612/942023-20161227214635804-1740521108.png "style=" border:0px; Font-family:verdana, Arial, Helvetica, Sans-serif;font-size:14px;white-space:normal;background-color:rgb ( 255,255,255); "/>
Correct user name, error password Login failed!
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/942023/201612/942023-20161227214901195-660984531. PNG "style=" Border:0px;font-family:verdana, Arial, Helvetica, Sans-serif;font-size:14px;white-space:normal; Background-color:rgb (255,255,255); "/>
User name is correct, password is empty, login failed!
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/942023/201612/942023-20161227214802492-326761308. PNG "style=" Border:0px;font-family:verdana, Arial, Helvetica, Sans-serif;font-size:14px;white-space:normal; Background-color:rgb (255,255,255); "/>
User name is wrong, password is correct, login failed!
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/942023/201612/942023-20161227214957632-23278223. PNG "style=" Border:0px;font-family:verdana, Arial, Helvetica, Sans-serif;font-size:14px;white-space:normal; Background-color:rgb (255,255,255); "/>
With the named empty, the correct password, login failed!
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/942023/201612/942023-20161227215036961-1582028544. PNG "style=" Border:0px;font-family:verdana, Arial, Helvetica, Sans-serif;font-size:14px;white-space:normal; Background-color:rgb (255,255,255); "/>
This article comes from "No idea, no achievement!" "Blog, be sure to keep this provenance http://kemixing.blog.51cto.com/10774787/1886713
A complete automated login test-Cnblog Automated login testing based on Python+selenium