The introduction of Htmlunit test tools is very good. is a browser for Java development. Say it is browser, in fact it is a Java class library that models HTML and provides APIs to access pages, click Links, and so on.
Such a test tool has several advantages: no interface to run, very fast. Because it is a Java class library, there is the possibility of infinite expansion, you can construct various powerful tools. Includes localization testing, multiple data source input data. Cross-platform, cross-browser. Java itself has cross-platform features, the browser, as long as the simple setting of a parameter can easily imitate the desired browser. Conversion to performance testing is simple enough to share the same script. Official website: http://htmlunit.sourceforge.net/code example:
Import java.io.IOException; Import java.net.MalformedURLException; Import Java.net.URL; Import Java.util.Arrays; Import Java.util.regex.Matcher; Import Java.util.regex.Pattern; Import com.gargoylesoftware.htmlunit.BrowserVersion; Import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; Import Com.gargoylesoftware.htmlunit.HttpMethod; Import com.gargoylesoftware.htmlunit.WebClient; Import Com.gargoylesoftware.htmlunit.WebRequest; Import Com.gargoylesoftware.htmlunit.html.HtmlForm; Import Com.gargoylesoftware.htmlunit.html.HtmlInput; Import Com.gargoylesoftware.htmlunit.html.HtmlPage; Import Com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; Import Com.gargoylesoftware.htmlunit.util.NameValuePair; public class Mysina {private WebClient client; private WebRequest request; Private String Sinaloginurl = "http://mail.sina.com.cn/cgi-bin/login.php"; Private String Hostsinaurl = ""; public static void Main (string[] args) throws FAILINGHTTPSTATUSCOdeexception, Malformedurlexception, IOException {String username = "* * *"; String Password = "* * *"; String NewPassword = "* * *"; String nickname = "* * *"; Mysina Mysina = new Mysina (); if (Mysina.mailloginbysina (username, password)) {//Login Mysina.updatepwdbysina (password, newpassword);//Change Password Mysina.updatenickname (nickname); Modify account Nickname} else {System.out.println ("Login failed! Please check that your username and password are correct! "); }} public Mysina () {client = new WebClient (browserversion.internet_explorer_8); Client.setjavascriptenabled (FALSE); }/** * Change account Nickname * * @param nickname Nickname * @return Boolean * @throws failinghttpstatuscodeexce ption * @throws IOException */public boolean updatenickname (String nickname) throws Failinghttpstatuscodee Xception, IOException {String Sinaseturl = hostsinaurl + "Basic/setting_account"; Request = new WebreqUest (New URL (Sinaseturl), httpmethod.post); Request.setcharset ("Utf-8"); Request.setrequestparameters (Arrays.aslist (New Namevaluepair ("nickname", nickname), New Namevaluepair ("POP3", "On"), New Namevaluepair ("IMAP", "on")); Client.getpage (Request); HtmlPage p = client.getpage (Hostsinaurl + "classic/index.php"); if (P.getbody (). Gettextcontent (). IndexOf ("\" nickname\ ": \" "+ Nickname +" \ "") > 0) {return true; } else {return false; }}/** * Change Password * * @param oldpassword old password * @param newpassword New password * @return Boolean * @throws failinghttpstatuscodeexception * @throws IOException */public boolean Updatepwdbysina (String OldPassword, String newpassword) throws Failinghttpstatuscodeexception, IOException {String Sinaseturl = "http// Login.sina.com.cn/member/security/password.php "; Request = new WebRequest (new URL (Sinaseturl), httpmethod.post); Request.setcharset ("GBK"); Request.setrequestparameters (Arrays.aslist (New Namevaluepair ("Pass", OldPassword), New Namevaluepair ("Pass1", NewPassword), New Namevaluepair ( "Pass2", NewPassword))); HtmlPage p = client.getpage (request); if (P.getbody (). Gettextcontent (). indexOf ("Your password modified successfully") > 0) {return true; } else {return false; }}/** * login * * @param username user name * @param password Password * @return Boolean * @thr oWS failinghttpstatuscodeexception * @throws malformedurlexception * @throws ioexception * * Public Boo Lean MAIlloginbysina (string Username, string password) throws Failinghttpstatuscodeexception, Malformedurlexception, IOException {htmlpage loginpage = client.getpage (sinalog Inurl); HtmlForm loginform = Loginpage.getformbyname ("free"); Htmlinput u = loginform.getinputbyname ("U"); Htmlinput PSW = Loginform.getinputbyname ("PSW"); Htmlsubmitinput Loginbutton = loginform.getinputbyname ("login"); U.setvalueattribute (username); Psw.setvalueattribute (password); HtmlPage result = Loginbutton.click (); String Resulturl = Result.geturl (). toString (); if (Resulturl.indexof ("classic/index.php") > 0) {String regex = "http://(. *?) /"; Hostsinaurl = Myregex (resulturl, regex, NULL); if (hostsinaurl.length () > 0) {return true; } else {return false; }} else { return false; }}/** * Regular match replacement * * @param str * @param reg * @param replace * @return */ public string Myregex (String str, String Reg, string[] replace) {string result = NULL; Matcher m = Pattern.compile (reg). Matcher (str); while (M.find ()) {result = M.group (); if (replace! = null && replace.length > 0) {for (String s:replace) {res Ult = Result.replace (S, ""); }}} return result; } }
HTML page Tool-htmlunit