BAEL-228 (#863)
* Update HtmlUnitAndJUnitTest.java * Update HtmlUnitWebScraping.java * Update HtmlUnitAndSpringTest.java * Create message.html * Update HtmlUnitAndJUnitTest.java * Delete HtmlUnitAndSpringIntegrationTest.java * Delete HtmlUnitTest.java
This commit is contained in:
parent
678e47dc1a
commit
977333c9a3
@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="#" th:action="@{/message/processForm}" method="post">
|
||||||
|
Message: <input type="text" value="message" id="message" name="message"/>
|
||||||
|
<input type="submit" />
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<span th:text="${message}" id="received"></span>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.htmlunit;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||||
|
|
||||||
|
public class HtmlUnitAndJUnitTest {
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() throws Exception {
|
||||||
|
webClient = new WebClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void close() throws Exception {
|
||||||
|
webClient.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsOk()
|
||||||
|
throws Exception {
|
||||||
|
webClient.getOptions().setThrowExceptionOnScriptError(false);
|
||||||
|
HtmlPage page = webClient.getPage("http://www.baeldung.com/");
|
||||||
|
Assert.assertEquals(
|
||||||
|
"Baeldung | Java, Spring and Web Development tutorials",
|
||||||
|
page.getTitleText());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,72 +0,0 @@
|
|||||||
package com.baeldung.htmlunit;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.MalformedURLException;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
|
||||||
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
|
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
|
||||||
|
|
||||||
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
|
|
||||||
import com.gargoylesoftware.htmlunit.WebClient;
|
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
|
||||||
@WebAppConfiguration
|
|
||||||
@ContextConfiguration(classes = { TestConfig.class })
|
|
||||||
public class HtmlUnitAndSpringIntegrationTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private WebApplicationContext wac;
|
|
||||||
|
|
||||||
private WebClient webClient;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setup() {
|
|
||||||
webClient = MockMvcWebClientBuilder.webAppContextSetup(wac).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Ignore("Related view message.html does not exist check MessageController")
|
|
||||||
public void givenAMessage_whenSent_thenItShows() throws FailingHttpStatusCodeException, MalformedURLException, IOException {
|
|
||||||
final String text = "Hello world!";
|
|
||||||
final HtmlPage page = webClient.getPage("http://localhost/message/showForm");
|
|
||||||
System.out.println(page.asXml());
|
|
||||||
|
|
||||||
final HtmlTextInput messageText = page.getHtmlElementById("message");
|
|
||||||
messageText.setValueAttribute(text);
|
|
||||||
|
|
||||||
final HtmlForm form = page.getForms().get(0);
|
|
||||||
final HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
|
|
||||||
final HtmlPage newPage = submit.click();
|
|
||||||
|
|
||||||
final String receivedText = newPage.getHtmlElementById("received").getTextContent();
|
|
||||||
|
|
||||||
Assert.assertEquals(receivedText, text);
|
|
||||||
System.out.println(newPage.asXml());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
|
|
||||||
try (final WebClient client = new WebClient()) {
|
|
||||||
webClient.getOptions().setThrowExceptionOnScriptError(false);
|
|
||||||
|
|
||||||
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
|
|
||||||
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.baeldung.htmlunit;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
|
||||||
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
@ContextConfiguration(classes = { TestConfig.class })
|
||||||
|
public class HtmlUnitAndSpringTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebApplicationContext wac;
|
||||||
|
|
||||||
|
private WebClient webClient;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
webClient = MockMvcWebClientBuilder
|
||||||
|
.webAppContextSetup(wac).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,21 +0,0 @@
|
|||||||
package com.baeldung.htmlunit;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.gargoylesoftware.htmlunit.WebClient;
|
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
|
||||||
|
|
||||||
public class HtmlUnitTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
|
|
||||||
try (final WebClient webClient = new WebClient()) {
|
|
||||||
webClient.getOptions().setThrowExceptionOnScriptError(false);
|
|
||||||
|
|
||||||
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
|
|
||||||
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -5,36 +5,38 @@ import java.util.List;
|
|||||||
import com.gargoylesoftware.htmlunit.WebClient;
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
|
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
|
import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlHeading2;
|
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||||
|
|
||||||
public class HtmlUnitWebScraping {
|
public class HtmlUnitWebScraping {
|
||||||
|
|
||||||
public static void main(final String[] args) throws Exception {
|
private WebClient webClient;
|
||||||
try (final WebClient webClient = new WebClient()) {
|
|
||||||
|
|
||||||
webClient.getOptions().setCssEnabled(false);
|
@Before
|
||||||
webClient.getOptions().setJavaScriptEnabled(false);
|
public void init() throws Exception {
|
||||||
|
webClient = new WebClient();
|
||||||
|
}
|
||||||
|
|
||||||
final HtmlPage page = webClient.getPage("http://www.baeldung.com/full_archive");
|
@After
|
||||||
final HtmlAnchor latestPostLink = (HtmlAnchor) page.getByXPath("(//ul[@class='car-monthlisting']/li)[1]/a").get(0);
|
public void close() throws Exception {
|
||||||
|
webClient.close();
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println("Entering: " + latestPostLink.getHrefAttribute());
|
@Test
|
||||||
|
public void givenBaeldungArchive_whenRetrievingArticle_thenHasH1()
|
||||||
|
throws Exception {
|
||||||
|
webClient.getOptions().setCssEnabled(false);
|
||||||
|
webClient.getOptions().setJavaScriptEnabled(false);
|
||||||
|
|
||||||
final HtmlPage postPage = latestPostLink.click();
|
String url = "http://www.baeldung.com/full_archive";
|
||||||
|
HtmlPage page = webClient.getPage(url);
|
||||||
|
String xpath = "(//ul[@class='car-monthlisting']/li)[1]/a";
|
||||||
|
HtmlAnchor latestPostLink
|
||||||
|
= (HtmlAnchor) page.getByXPath(xpath).get(0);
|
||||||
|
HtmlPage postPage = latestPostLink.click();
|
||||||
|
|
||||||
final HtmlHeading1 heading1 = (HtmlHeading1) postPage.getByXPath("//h1").get(0);
|
List<HtmlHeading1> h1
|
||||||
System.out.println("Title: " + heading1.getTextContent());
|
= (List<HtmlHeading1>) postPage.getByXPath("//h1");
|
||||||
|
|
||||||
final List<HtmlHeading2> headings2 = (List<HtmlHeading2>) postPage.getByXPath("//h2");
|
|
||||||
|
|
||||||
final StringBuilder sb = new StringBuilder(heading1.getTextContent());
|
|
||||||
for (final HtmlHeading2 h2 : headings2) {
|
|
||||||
sb.append("\n").append(h2.getTextContent());
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println(sb.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Assert.assertTrue(h1.size() > 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user