Skip to content

Commit b882a23

Browse files
Create HtmlUnitAndSpringTest.java
1 parent 70ed305 commit b882a23

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.webAppContextSetup(wac).build();
37+
}
38+
39+
@Test
40+
@Ignore
41+
public void givenAMessage_whenSent_thenItShows() {
42+
final String text = "Hello world!";
43+
HtmlPage page;
44+
45+
try {
46+
47+
page = webClient.getPage("http://localhost/message/showForm");
48+
System.out.println(page.asXml());
49+
50+
final HtmlTextInput messageText = page.getHtmlElementById("message");
51+
messageText.setValueAttribute(text);
52+
53+
final HtmlForm form = page.getForms().get(0);
54+
final HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
55+
final HtmlPage newPage = submit.click();
56+
57+
final String receivedText = newPage.getHtmlElementById("received").getTextContent();
58+
59+
Assert.assertEquals(receivedText, text);
60+
System.out.println(newPage.asXml());
61+
62+
} catch (FailingHttpStatusCodeException | IOException e) {
63+
e.printStackTrace();
64+
}
65+
66+
}
67+
68+
@Test
69+
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
70+
try (final WebClient client = new WebClient()) {
71+
72+
webClient.getOptions().setThrowExceptionOnScriptError(false);
73+
74+
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
75+
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
76+
}
77+
}
78+
79+
}

0 commit comments

Comments
 (0)