Search in sources :

Example 21 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project JSCover by tntim96.

the class HtmlServerUnloadedJSProxyOnlyInstrumentRegTest method shouldIncludeUnloadJSInSavedReport.

@Test
public void shouldIncludeUnloadJSInSavedReport() throws Exception {
    File jsonFile = new File(reportDir + "/jscoverage.json");
    if (jsonFile.exists())
        jsonFile.delete();
    HtmlPage page = webClient.getPage("http://localhost:9001/jscoverage.html?index.html");
    page.getHtmlElementById("summaryTab").click();
    webClient.waitForBackgroundJavaScript(2000);
    assertEquals("75%", page.getElementById("summaryTotal").getTextContent());
    verifyCoverage(page, "/level1/level1.js", "75%", "50%", "N/A");
    page.getHtmlElementById("storeTab").click();
    webClient.waitForBackgroundJavaScript(500);
    HtmlElement storeButton = page.getHtmlElementById("storeButton");
    storeButton.click();
    webClient.waitForBackgroundJavaScript(2000);
    String result = page.getElementById("storeDiv").getTextContent();
    assertThat(result, containsString("Coverage data stored at " + new File(reportDir).getPath()));
    String json = ioUtils.toString(jsonFile);
    assertThat(json, not(containsString("/root.js")));
    assertThat(json, containsString("/level1/level1.js"));
    assertThat(json, containsString("/level1/level2/level2.js"));
    String url = "file:///" + new File(reportDir + "/jscoverage.html").getAbsolutePath();
    page = webClient.getPage(url);
    webClient.waitForBackgroundJavaScript(1000);
    assertEquals("37%", page.getElementById("summaryTotal").getTextContent());
    assertEquals("25%", page.getElementById("branchSummaryTotal").getTextContent());
    assertEquals("0%", page.getElementById("functionSummaryTotal").getTextContent());
    verifyCoverage(page, "/level1/level1.js", "75%", "50%", "N/A");
    verifyCoverage(page, "/level1/level2/level2.js", "0%", "0%", "0%");
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 22 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project tutorials by eugenp.

the class HtmlUnitAndSpringLiveTest method givenAMessage_whenSent_thenItShows.

@Test
public void givenAMessage_whenSent_thenItShows() throws Exception {
    String text = "Hello world!";
    HtmlPage page;
    String url = "http://localhost/message/showForm";
    page = webClient.getPage(url);
    HtmlTextInput messageText = page.getHtmlElementById("message");
    messageText.setValueAttribute(text);
    HtmlForm form = page.getForms().get(0);
    HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
    HtmlPage newPage = submit.click();
    String receivedText = newPage.getHtmlElementById("received").getTextContent();
    Assert.assertEquals(receivedText, text);
}
Also used : HtmlTextInput(com.gargoylesoftware.htmlunit.html.HtmlTextInput) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlSubmitInput(com.gargoylesoftware.htmlunit.html.HtmlSubmitInput) Test(org.junit.Test)

Example 23 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project tutorials by eugenp.

the class HtmlUnitWebScrapingLiveTest method givenBaeldungArchive_whenRetrievingArticle_thenHasH1.

@Test
public void givenBaeldungArchive_whenRetrievingArticle_thenHasH1() throws Exception {
    webClient.getOptions().setCssEnabled(false);
    webClient.getOptions().setJavaScriptEnabled(false);
    final String url = "http://www.baeldung.com/full_archive";
    final HtmlPage page = webClient.getPage(url);
    final String xpath = "(//ul[@class='car-monthlisting']/li)[1]/a";
    final HtmlAnchor latestPostLink = (HtmlAnchor) page.getByXPath(xpath).get(0);
    final HtmlPage postPage = latestPostLink.click();
    final List<HtmlHeading1> h1 = (List<HtmlHeading1>) postPage.getByXPath("//h1");
    Assert.assertTrue(h1.size() > 0);
}
Also used : HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) HtmlHeading1(com.gargoylesoftware.htmlunit.html.HtmlHeading1) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) List(java.util.List) Test(org.junit.Test)

Example 24 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage 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 25 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage 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)

Aggregations

HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)159 Test (org.junit.Test)114 WebClient (com.gargoylesoftware.htmlunit.WebClient)51 HtmlSubmitInput (com.gargoylesoftware.htmlunit.html.HtmlSubmitInput)25 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)23 HtmlSpan (com.gargoylesoftware.htmlunit.html.HtmlSpan)21 File (java.io.File)17 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)15 Matchers.containsString (org.hamcrest.Matchers.containsString)13 IOException (java.io.IOException)11 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)10 FreeStyleProject (hudson.model.FreeStyleProject)9 URL (java.net.URL)9 Page (com.gargoylesoftware.htmlunit.Page)8 HtmlTextInput (com.gargoylesoftware.htmlunit.html.HtmlTextInput)7 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)6 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)6 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)5 HtmlButton (com.gargoylesoftware.htmlunit.html.HtmlButton)5 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)5