|
| 1 | +package org.javaee7.jsf.http.get; |
| 2 | + |
| 3 | +import com.gargoylesoftware.htmlunit.WebClient; |
| 4 | +import com.gargoylesoftware.htmlunit.html.HtmlAnchor; |
| 5 | +import com.gargoylesoftware.htmlunit.html.HtmlButtonInput; |
| 6 | +import com.gargoylesoftware.htmlunit.html.HtmlElement; |
| 7 | +import com.gargoylesoftware.htmlunit.html.HtmlPage; |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.net.URL; |
| 11 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 12 | +import org.jboss.arquillian.junit.Arquillian; |
| 13 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 14 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 15 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.Test; |
| 18 | +import static org.junit.Assert.*; |
| 19 | +import org.junit.runner.RunWith; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Arun Gupta |
| 23 | + */ |
| 24 | +@RunWith(Arquillian.class) |
| 25 | +public class UserTest { |
| 26 | + |
| 27 | + @ArquillianResource |
| 28 | + private URL base; |
| 29 | + |
| 30 | + WebClient webClient; |
| 31 | + |
| 32 | + private static final String WEBAPP_SRC = "src/main/webapp"; |
| 33 | + HtmlPage page; |
| 34 | + |
| 35 | + @Deployment(testable = false) |
| 36 | + public static WebArchive createDeployment() { |
| 37 | + return ShrinkWrap.create(WebArchive.class). |
| 38 | + addClass(User.class) |
| 39 | + .addAsWebResource(new File(WEBAPP_SRC, "index.xhtml")) |
| 40 | + .addAsWebResource(new File(WEBAPP_SRC, "index2.xhtml")) |
| 41 | + .addAsWebResource(new File(WEBAPP_SRC, "login.xhtml")) |
| 42 | + .addAsWebInfResource(new File(WEBAPP_SRC + "/WEB-INF", "web.xml")) |
| 43 | + .addAsWebInfResource(new File(WEBAPP_SRC + "/WEB-INF", "beans.xml")); |
| 44 | + } |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setup() throws IOException { |
| 48 | + webClient = new WebClient(); |
| 49 | + page = webClient.getPage(base + "/faces/index.xhtml"); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testLink() throws IOException { |
| 54 | + HtmlAnchor anchor = (HtmlAnchor)page.getElementById("link1"); |
| 55 | + assertTrue(anchor.getHrefAttribute().contains("faces/login.xhtml")); |
| 56 | + assertEquals("Login1", anchor.asText()); |
| 57 | + |
| 58 | + HtmlPage output = anchor.click(); |
| 59 | + assertEquals("HTTP GET (Login)", output.getTitleText()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testLinkWithParam() throws IOException { |
| 64 | + HtmlAnchor anchor = (HtmlAnchor)page.getElementById("link2"); |
| 65 | + assertTrue(anchor.getHrefAttribute().contains("faces/login.xhtml")); |
| 66 | + assertTrue(anchor.getHrefAttribute().contains("?name=Jack")); |
| 67 | + assertEquals("Login2", anchor.asText()); |
| 68 | + |
| 69 | + HtmlPage output = anchor.click(); |
| 70 | + assertEquals("HTTP GET (Login)", output.getTitleText()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testLinkWithPreProcessParams() { |
| 75 | + HtmlAnchor anchor = (HtmlAnchor)page.getElementById("link3"); |
| 76 | + assertEquals("Login3", anchor.asText()); |
| 77 | + assertTrue(anchor.getHrefAttribute().contains("faces/index2.xhtml")); |
| 78 | + assertTrue(anchor.getHrefAttribute().contains("?name=Jack")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testButton() throws IOException { |
| 83 | + HtmlButtonInput button = (HtmlButtonInput)page.getElementById("button1"); |
| 84 | + assertEquals("Login4", button.asText()); |
| 85 | + |
| 86 | + HtmlPage output = button.click(); |
| 87 | + assertEquals("HTTP GET (Login)", output.getTitleText()); |
| 88 | + } |
| 89 | +} |
0 commit comments