Skip to content

Commit 977333c

Browse files
jgarciaherederopivovarit
authored andcommitted
BAEL-228 (eugenp#863)
* Update HtmlUnitAndJUnitTest.java * Update HtmlUnitWebScraping.java * Update HtmlUnitAndSpringTest.java * Create message.html * Update HtmlUnitAndJUnitTest.java * Delete HtmlUnitAndSpringIntegrationTest.java * Delete HtmlUnitTest.java
1 parent 678e47d commit 977333c

6 files changed

Lines changed: 131 additions & 115 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
4+
<head>
5+
<meta charset="UTF-8" />
6+
</head>
7+
<body>
8+
<form action="#" th:action="@{/message/processForm}" method="post">
9+
Message: <input type="text" value="message" id="message" name="message"/>
10+
<input type="submit" />
11+
</form>
12+
13+
<span th:text="${message}" id="received"></span>
14+
</body>
15+
</html>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.htmlunit;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import com.gargoylesoftware.htmlunit.WebClient;
7+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
8+
9+
public class HtmlUnitAndJUnitTest {
10+
11+
@Before
12+
public void init() throws Exception {
13+
webClient = new WebClient();
14+
}
15+
16+
@After
17+
public void close() throws Exception {
18+
webClient.close();
19+
}
20+
21+
@Test
22+
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsOk()
23+
throws Exception {
24+
webClient.getOptions().setThrowExceptionOnScriptError(false);
25+
HtmlPage page = webClient.getPage("http://www.baeldung.com/");
26+
Assert.assertEquals(
27+
"Baeldung | Java, Spring and Web Development tutorials",
28+
page.getTitleText());
29+
}
30+
31+
}

‎spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndSpringIntegrationTest.java‎

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.htmlunit;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Ignore;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13+
import org.springframework.test.context.web.WebAppConfiguration;
14+
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
15+
import org.springframework.web.context.WebApplicationContext;
16+
17+
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
18+
import com.gargoylesoftware.htmlunit.WebClient;
19+
import com.gargoylesoftware.htmlunit.html.HtmlForm;
20+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
21+
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
22+
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
23+
24+
@RunWith(SpringJUnit4ClassRunner.class)
25+
@WebAppConfiguration
26+
@ContextConfiguration(classes = { TestConfig.class })
27+
public class HtmlUnitAndSpringTest {
28+
29+
@Autowired
30+
private WebApplicationContext wac;
31+
32+
private WebClient webClient;
33+
34+
@Before
35+
public void setup() {
36+
webClient = MockMvcWebClientBuilder
37+
.webAppContextSetup(wac).build();
38+
}
39+
40+
@Test
41+
public void givenAMessage_whenSent_thenItShows() throws Exception {
42+
String text = "Hello world!";
43+
HtmlPage page;
44+
45+
String url = "http://localhost/message/showForm";
46+
page = webClient.getPage(url);
47+
48+
HtmlTextInput messageText = page.getHtmlElementById("message");
49+
messageText.setValueAttribute(text);
50+
51+
HtmlForm form = page.getForms().get(0);
52+
HtmlSubmitInput submit = form.getOneHtmlElementByAttribute(
53+
"input", "type", "submit");
54+
HtmlPage newPage = submit.click();
55+
56+
String receivedText = newPage.getHtmlElementById("received")
57+
.getTextContent();
58+
59+
Assert.assertEquals(receivedText, text);
60+
}
61+
}

‎spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitTest.java‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitWebScraping.java‎

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,38 @@
55
import com.gargoylesoftware.htmlunit.WebClient;
66
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
77
import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
8-
import com.gargoylesoftware.htmlunit.html.HtmlHeading2;
98
import com.gargoylesoftware.htmlunit.html.HtmlPage;
109

1110
public class HtmlUnitWebScraping {
1211

13-
public static void main(final String[] args) throws Exception {
14-
try (final WebClient webClient = new WebClient()) {
12+
private WebClient webClient;
1513

16-
webClient.getOptions().setCssEnabled(false);
17-
webClient.getOptions().setJavaScriptEnabled(false);
14+
@Before
15+
public void init() throws Exception {
16+
webClient = new WebClient();
17+
}
1818

19-
final HtmlPage page = webClient.getPage("http://www.baeldung.com/full_archive");
20-
final HtmlAnchor latestPostLink = (HtmlAnchor) page.getByXPath("(//ul[@class='car-monthlisting']/li)[1]/a").get(0);
19+
@After
20+
public void close() throws Exception {
21+
webClient.close();
22+
}
2123

22-
System.out.println("Entering: " + latestPostLink.getHrefAttribute());
24+
@Test
25+
public void givenBaeldungArchive_whenRetrievingArticle_thenHasH1()
26+
throws Exception {
27+
webClient.getOptions().setCssEnabled(false);
28+
webClient.getOptions().setJavaScriptEnabled(false);
2329

24-
final HtmlPage postPage = latestPostLink.click();
30+
String url = "http://www.baeldung.com/full_archive";
31+
HtmlPage page = webClient.getPage(url);
32+
String xpath = "(//ul[@class='car-monthlisting']/li)[1]/a";
33+
HtmlAnchor latestPostLink
34+
= (HtmlAnchor) page.getByXPath(xpath).get(0);
35+
HtmlPage postPage = latestPostLink.click();
2536

26-
final HtmlHeading1 heading1 = (HtmlHeading1) postPage.getByXPath("//h1").get(0);
27-
System.out.println("Title: " + heading1.getTextContent());
28-
29-
final List<HtmlHeading2> headings2 = (List<HtmlHeading2>) postPage.getByXPath("//h2");
30-
31-
final StringBuilder sb = new StringBuilder(heading1.getTextContent());
32-
for (final HtmlHeading2 h2 : headings2) {
33-
sb.append("\n").append(h2.getTextContent());
34-
}
35-
36-
System.out.println(sb.toString());
37-
}
38-
}
37+
List<HtmlHeading1> h1
38+
= (List<HtmlHeading1>) postPage.getByXPath("//h1");
3939

40+
Assert.assertTrue(h1.size() > 0);
41+
}
4042
}

0 commit comments

Comments
 (0)