From 977333c9a312759d1dd537434001bef6f4ed869a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Garc=C3=ADa=20Heredero?= Date: Mon, 28 Nov 2016 18:43:10 +0100 Subject: [PATCH] BAEL-228 (#863) * Update HtmlUnitAndJUnitTest.java * Update HtmlUnitWebScraping.java * Update HtmlUnitAndSpringTest.java * Create message.html * Update HtmlUnitAndJUnitTest.java * Delete HtmlUnitAndSpringIntegrationTest.java * Delete HtmlUnitTest.java --- .../webapp/WEB-INF/templates/message.html | 15 ++++ .../htmlunit/HtmlUnitAndJUnitTest.java | 31 ++++++++ .../HtmlUnitAndSpringIntegrationTest.java | 72 ------------------- .../htmlunit/HtmlUnitAndSpringTest.java | 61 ++++++++++++++++ .../com/baeldung/htmlunit/HtmlUnitTest.java | 21 ------ .../htmlunit/HtmlUnitWebScraping.java | 46 ++++++------ 6 files changed, 131 insertions(+), 115 deletions(-) create mode 100644 spring-mvc-java/src/main/webapp/WEB-INF/templates/message.html create mode 100644 spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndJUnitTest.java delete mode 100644 spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndSpringIntegrationTest.java create mode 100644 spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndSpringTest.java delete mode 100644 spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitTest.java diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/templates/message.html b/spring-mvc-java/src/main/webapp/WEB-INF/templates/message.html new file mode 100644 index 0000000000..291e8312ae --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/templates/message.html @@ -0,0 +1,15 @@ + + + + + + +
+ Message: + +
+ + + + diff --git a/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndJUnitTest.java b/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndJUnitTest.java new file mode 100644 index 0000000000..8395a49581 --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndJUnitTest.java @@ -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()); + } + +} diff --git a/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndSpringIntegrationTest.java b/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndSpringIntegrationTest.java deleted file mode 100644 index 406975b6cc..0000000000 --- a/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndSpringIntegrationTest.java +++ /dev/null @@ -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()); - } - } - -} diff --git a/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndSpringTest.java b/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndSpringTest.java new file mode 100644 index 0000000000..45e441f47f --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitAndSpringTest.java @@ -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); + } +} diff --git a/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitTest.java b/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitTest.java deleted file mode 100644 index 6a7e961eb1..0000000000 --- a/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitTest.java +++ /dev/null @@ -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()); - } - } - -} diff --git a/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitWebScraping.java b/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitWebScraping.java index 9919d7571d..f97bedddef 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitWebScraping.java +++ b/spring-mvc-java/src/test/java/com/baeldung/htmlunit/HtmlUnitWebScraping.java @@ -5,36 +5,38 @@ import java.util.List; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlAnchor; import com.gargoylesoftware.htmlunit.html.HtmlHeading1; -import com.gargoylesoftware.htmlunit.html.HtmlHeading2; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class HtmlUnitWebScraping { - public static void main(final String[] args) throws Exception { - try (final WebClient webClient = new WebClient()) { + private WebClient webClient; - webClient.getOptions().setCssEnabled(false); - webClient.getOptions().setJavaScriptEnabled(false); + @Before + public void init() throws Exception { + webClient = new WebClient(); + } - final HtmlPage page = webClient.getPage("http://www.baeldung.com/full_archive"); - final HtmlAnchor latestPostLink = (HtmlAnchor) page.getByXPath("(//ul[@class='car-monthlisting']/li)[1]/a").get(0); + @After + 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); - System.out.println("Title: " + heading1.getTextContent()); - - final List headings2 = (List) 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()); - } - } + List h1 + = (List) postPage.getByXPath("//h1"); + Assert.assertTrue(h1.size() > 0); + } }