The daily web site development process, in order to improve the security of the login or prevent the user through the script of the Ox operation (the universe most expensive tin celestial Vehicle License plate Auction), many websites in the time of login, added verification code verification, and the implementation of the verification code is more and more complex, the difficulty of script recognition is more and more high. This is very inconvenient for our automated scripting, so how to solve the problem of verification code at login? Often there are beginners who are writing automated scripts to ask this question.
This article is mainly aimed at how to solve the operation problem of the login state in the automated test script, that is how to reduce the verification code to analyze and solve the solution in the process of automatic script writing, and take the example demonstration (based on Yixun NetEase Xun account login). To solve the problem of verification code, there are generally the following methods:
1, Verification Code Identification Technology (OCR). Identify the verification code that appears during the login process through the Verification code identification technique. This method is not recommended to use, waste a lot of manpower and resources, not worth the candle.
2, universal Verification code. For example: The required "Aaron" is a universal verification code, if the server received this verification code, then return the correct login information. The flaw in this method is that it cannot be used online, because the security of the universal verification code cannot be guaranteed. Thus, this method is generally applied to the test environment.
3, the Cookie cross-border login. By adding the user login cookie information to the browser, directly over the login operation (indirectly skip the verification code input), the user's login state is added to the browser, the corresponding login state user operation. Although this method has certain limitations (subject to the effective time limit of the cookie), I still prefer this method.
The first method mentioned above is very simple, so we will not repeat it here; the second method, the interested little master Daniel can try. The following is a third method for example demonstration, based on Yixun NetEase Xun account login Cookie added.
Gossip less, small two on the yards ... Please refer to the small master, hope to be able to you in the daily WebUI automated scripting have some inspiration and help. If there are deficiencies or errors, please ask the great God to correct, greatly appreciated!
1 /**2 * AARON.FFP Inc.3 * Copyright (c) 2004-2015 All rights Reserved.4 */5 PackageMain.aaron.demo.cookie;6 7 ImportJava.io.BufferedReader;8 ImportJava.io.File;9 ImportJava.io.FileReader;Ten Importjava.util.Date; One A Importmain.aaron.sele.core.TestCase; - - ImportOrg.openqa.selenium.Cookie; the ImportOrg.testng.Assert; - ImportOrg.testng.annotations.AfterClass; - Importorg.testng.annotations.Test; - + /** - * ADD cookie to Web browser + * A * @authorAARON.FFP at * @versionV1.0.0:autoseleniumdemo Main.aaron.demo.cookie Usecookielogin.java, 2015-6-19 15:53:53 Exp $ - */ - Public classUsecookieloginextendstestcase{ - Private FinalString F_cookie = This. Projecthome + This. Fileseparator + "Cookie" + This. Fileseparator + "Browser.data"; - PrivateString baseUrl = "http://www.yixun.com/"; - in @Test - Public voidUsecookielogin () { to This. Webdriver.get ( This. BaseUrl); + - Try { the //Clean the invalid cookie * This. Webdriver.manage (). Deleteallcookies (); $ Panax Notoginseng //Create file Object -File f =NewFile ( This. F_cookie); the + //Read File AFileReader FR =NewFileReader (f); theBufferedReader br =NewBufferedReader (FR); + - //Store Row Cookie Info $ String Line; $ - //add cookie to Web browser - while(line = Br.readline ())! =NULL) { thestring[] Item = Line.split (";"); - Wuyi if(Item.length! = 6) { the Continue; - } Wu - for(inti = 0; i < item.length; i++) { AboutString name = Item[0]; $String value = item[1]; -String domain = item[2]; -String Path = item[3]; -Date expiry =NULL; A BooleanIssecure =NewBoolean (item[5]); + the //Create Cookie Object -Cookie cookie =NewCookie (name, value, domain, path, expiry, issecure); $ the //add cookie to Web browser the This. Webdriver.manage (). Addcookie (cookie); the } the } - in br.close (); the}Catch(Exception e) { the e.printstacktrace (); AboutAssert.fail ((e.getmessage () = =NULL)? "": E.getmessage ()); the } the the This. Webdriver.get ( This. BaseUrl); + } - the @AfterClassBayi Public voidAfterclass () { the This. Webdriver.close (); the This. Webdriver.quit (); - } -}
Yixun NetEase Quick Account Cookie Add actual case demonstration source code
For information on how to obtain and save the cookie data of the login, please refer to the SELENIUM2 Learning -017-webui Automated Combat instance -015-get cookie information in the browser
PS: Small owners in the application of the above source code, the source of the cookie file needs to be modified to a local legal path can be: Private final String F_cookie = this. Projecthome + this. Fileseparator + "Cookie" + this. Fileseparator + "Browser.data";
At this point, theWebUI automation function test Script No. 016-The automated scripting process of the login verification code problem successfully completed, I hope this article can give beginners Selenium you a reference.
Finally, very grateful to the pro-stop, I hope this article can be pro helpful. Warmly welcome the kiss to discuss together and progress together. Thank you so much! ^_^
SELENIUM2 Learning -018-webui Automation Practical Cases -016-Login Verification code during script writing