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%");
}
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);
}
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);
}
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());
}
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"));
}
Aggregations