Search in sources :

Example 6 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project JSCover by tntim96.

the class HtmlUnitUITest method storeResult.

private static void storeResult() throws IOException {
    WebClient webClient = new WebClient();
    webClient.getOptions().setTimeout(1000);
    HtmlPage page = webClient.getPage("http://localhost:9001/jscoverage.html?index.html");
    page.getHtmlElementById("storeTab").click();
    webClient.waitForBackgroundJavaScript(500);
    page.getHtmlElementById("storeButton").click();
    webClient.waitForBackgroundJavaScript(2000);
    String result = page.getElementById("storeDiv").getTextContent();
    assertThat(result, containsString("Coverage data stored at target"));
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 7 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project crawler-jsoup-maven by bluetata.

the class htmlunitTest method main.

public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
    // 屏蔽HtmlUnit等系统 log
    LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
    java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
    java.util.logging.Logger.getLogger("org.apache.http.client").setLevel(Level.OFF);
    String url = "https://www.douyin.com/share/video/6496703951436516621/?mid=6484356820260686606";
    System.out.println("Loading page now-----------------------------------------------: " + url);
    /* HtmlUnit 模拟浏览器 */
    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    // 启用JS解释器,默认为true
    webClient.getOptions().setJavaScriptEnabled(true);
    // 禁用css支持
    webClient.getOptions().setCssEnabled(false);
    // js运行错误时,是否抛出异常
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    // 设置连接超时时间
    webClient.getOptions().setTimeout(10 * 1000);
    HtmlPage page = webClient.getPage(url);
    // 等待js后台执行30秒
    webClient.waitForBackgroundJavaScript(30 * 1000);
    String pageAsXml = page.asXml();
    /* Jsoup解析处理 */
    // Document doc = Jsoup.parse(pageAsXml, "https://bluetata.com/");
    Document doc = Jsoup.parse(pageAsXml);
    // 获取所有图片元素集
    Elements pngs = doc.select("img[src$=.png]");
    // 其他操作
    System.out.println(doc.toString());
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 8 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project core by weld.

the class Weld1280Test method testELContextOfDepedentScopeBean.

@Test
public void testELContextOfDepedentScopeBean() throws Exception {
    WebClient client = new WebClient();
    client.setThrowExceptionOnFailingStatusCode(false);
    HtmlPage main = client.getPage(url);
    assertTrue(main.getBody().asText().contains("Hello from dependent scope bean"));
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 9 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project core by weld.

the class ProbeConfigurationPropertiesTest method testEmbededInfoSnippetNOTAvailable.

@Test
public void testEmbededInfoSnippetNOTAvailable() throws IOException {
    WebClient client = new WebClient();
    HtmlPage page = client.getPage(url.toString() + "test");
    System.out.println(page.getBody().asXml());
    assertFalse(page.getBody().asXml().toString().contains("Probe Development Tool"));
    assertFalse(page.getBody().asXml().toString().contains(TEST_ARCHIVE_NAME));
    assertFalse(page.getBody().asXml().toString().contains("The following snippet was automatically added by Weld"));
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 10 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project core by weld.

the class ProbeConfigurationPropertiesTest method getChildInvocations.

private JsonArray getChildInvocations() throws IOException {
    WebClient webClient = invokeSimpleAction(url);
    JsonObject invocations = getPageAsJSONObject(INVOCATIONS_PATH, url, webClient);
    JsonArray invocationData = invocations.getJsonArray(DATA);
    int id = invocationData.getJsonObject(0).getInt(ID);
    JsonObject invocationTree = getPageAsJSONObject(INVOCATIONS_PATH + "/" + id, url, webClient);
    return invocationTree.getJsonArray(CHILDREN);
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Aggregations

WebClient (com.gargoylesoftware.htmlunit.WebClient)182 Test (org.junit.Test)110 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)55 Page (com.gargoylesoftware.htmlunit.Page)33 TextPage (com.gargoylesoftware.htmlunit.TextPage)21 Before (org.junit.Before)20 HtmlSubmitInput (com.gargoylesoftware.htmlunit.html.HtmlSubmitInput)17 URL (java.net.URL)14 JsonObject (javax.json.JsonObject)13 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)13 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)12 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)11 HtmlSpan (com.gargoylesoftware.htmlunit.html.HtmlSpan)10 ReadContext (com.jayway.jsonpath.ReadContext)10 File (java.io.File)9 IOException (java.io.IOException)8 NicelyResynchronizingAjaxController (com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController)7 Test (org.junit.jupiter.api.Test)7 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)6 DefaultCredentialsProvider (com.gargoylesoftware.htmlunit.DefaultCredentialsProvider)5