html頁面工具-htmlUnit

來源:互聯網
上載者:User

標籤:https   官網   程式碼範例   char   system   server   效能測試   ram   mlu   

 HtmlUnit測試載入器的推出,創意非常好。是一款給java開發用的browser。說它是browser,其實它是對html建模並且提供API來訪問頁面,點選連結等等的java類庫。

    這樣的測試載入器有這樣幾個優點:運行起來沒有介面,速度非常快。由於是java類庫,有無限擴充的可能,可以構造各種功能強大的工具。包括本地化測試,多種資料來源輸入資料。跨平台,跨瀏覽器。java本身就有跨平台的特性,瀏覽器,只要簡單的設定一個參數就可以輕易模仿想要的瀏覽器了。轉化為效能測試,非常簡單,可以共用同一指令碼。 官網:http://htmlunit.sourceforge.net/ 程式碼範例:
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;     } } 

  

html頁面工具-htmlUnit

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.