• htmlunit 自动化提交/获取网页数据,自动化测试


    开源组件:

    https://sourceforge.net/projects/htmlunit/

    demo

    public void post() {
            try {
                WebClient client = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
                client.setJavaScriptEnabled(false);
                HttpWebConnection httpwebconnection = new HttpWebConnection(client);
                // httpwebconnection.setUseInsecureSSL(true);
                ProxyConfig proxyConfig = new ProxyConfig();
                proxyConfig.setProxyHost("代理");
                proxyConfig.setProxyPort(8080);
    
                client.setProxyConfig(proxyConfig);
                client.setWebConnection(httpwebconnection);
                HtmlPage page = client.getPage("http://www.cmd5.com/default.aspx");
    
                HtmlElement textElemnt = page
                        .getElementById("ctl00_ContentPlaceHolder1_TextBoxInput");
                HtmlElement selectElement = page
                        .getElementById("ctl00_ContentPlaceHolder1_InputHashType");
                HtmlElement btnElement = page
                        .getElementById("ctl00_ContentPlaceHolder1_Button1");
                textElemnt.click();
                textElemnt.type("e10adc3949ba59abbe56e057f20f883e");
                selectElement.click();
                selectElement.type("MD5");
                // 点击事件,获取一个新的页面
                HtmlPage resultPage = (HtmlPage) btnElement.click();
    
                // 返回页面数据
                String result = resultPage.asText();
                // 截取字符串
                result = result.substring(result.indexOf("查询结果:") + 5).trim();
                int index = result.indexOf("[添加备注]");
                result = result.substring(0, index).trim();
                System.out.println(result);
            } catch (Exception ex) {
    
            }
        }

     自动化执行效果图

  • 相关阅读:
    总结
    webview细节注意
    对图片的处理
    介绍并提高app中WebView的性能
    工作中新接触的问题
    iOS环信
    Framework静态库制作方法
    多线程GCD
    iOS开发之地图与定位
    ARC内存管理机制详解
  • 原文地址:https://www.cnblogs.com/nidongde/p/5253726.html
Copyright © 2020-2023  润新知