html页面工具-htmlUnit
HtmlUnit测试工具的推出,创意非常好。是一款给java开发用的browser。说它是browser,其实它是对html建模并且提供API来访问页面,点击链接等等的java类库。
这样的测试工具有这样几个优点:
运行起来没有界面,速度非常快。
由于是java类库,有无限扩展的可能,可以构造各种功能强大的工具。包括本地化测试,多种数据源输入数据。
跨平台,跨浏览器。java本身就有跨平台的特性,浏览器,只要简单的设定一个参数就可以轻易模仿想要的浏览器了。
转化为性能测试,非常简单,可以共享同一脚本。
官网:http://htmlunit.sourceforge.net/
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | 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)) { // 登录 mySina.updatePwdBySina(password, newpassword); // 修改密码 mySina.updateNickName(nickname); // 修改帐户昵称 } else { System.out.println( "登录失败!请检查用户名和密码是否正确!" ); } } public MySina(){ client = new WebClient(BrowserVersion.INTERNET_EXPLORER_8); client.setJavaScriptEnabled( false ); } /** * 更改帐户昵称 * * @param nickname 昵称 * @return boolean * @throws FailingHttpStatusCodeException * @throws IOException */ public boolean updateNickName(String nickname) throws FailingHttpStatusCodeException, 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 ; } } /** * 修改密码 * * @param oldpassword 旧密码 * @param newpassword 新密码 * @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( "您的密码修改成功" ) > 0 ) { return true ; } else { return false ; } } /** * 登录 * * @param username 用户名 * @param password 密码 * @return boolean * @throws FailingHttpStatusCodeException * @throws MalformedURLException * @throws IOException */ public boolean mailLoginBySina(String username, String password) throws FailingHttpStatusCodeException, MalformedURLException, IOException { HtmlPage loginPage = client.getPage(sinaLoginUrl); HtmlForm loginForm = loginPage.getFormByName( "free" ); HtmlInput u = loginForm.getInputByName( "u" ); HtmlInput psw = loginForm.getInputByName( "psw" ); HtmlSubmitInput loginButton = loginForm.getInputByName( "登录" ); 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 ; } } /** * 正则匹配替换 * * @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) { result = result.replace(s, "" ); } } } return result; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架