From af6e75be99c0dd600583f1a2c42c64f9a89d7b66 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Sun, 13 Dec 2015 23:19:50 +0100 Subject: Add JUnit3 based HtmlUnit-Testsuite --- test/htmlunit/JCatalogTest.java | 101 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 test/htmlunit/JCatalogTest.java (limited to 'test/htmlunit/JCatalogTest.java') diff --git a/test/htmlunit/JCatalogTest.java b/test/htmlunit/JCatalogTest.java new file mode 100644 index 0000000..a1c8f40 --- /dev/null +++ b/test/htmlunit/JCatalogTest.java @@ -0,0 +1,101 @@ +package htmlunit; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; + +import junit.framework.TestCase; + +import org.xml.sax.SAXException; + +import com.gargoylesoftware.htmlunit.BrowserVersion; +import com.gargoylesoftware.htmlunit.BrowserVersionFeatures; +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.HtmlAnchor; +import com.gargoylesoftware.htmlunit.html.HtmlForm; +import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput; +import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; +import com.gargoylesoftware.htmlunit.html.HtmlTextInput; + +public class JCatalogTest extends TestCase +{ + public void testWelcomePage() throws Exception + { + HtmlPage page = gotoHomePage(); + assertEquals("JCatalog Application", page.getTitleText()); + + List forms = (List) page.getForms(); + assertEquals(1, forms.size()); + assertEquals("navcontainer", forms.get(0).getId()); + } + + public void testLinkToLoginPage() throws Exception + { + HtmlPage response = navigateToLoginPage(); + assertEquals("JCatalog Login", response.getTitleText()); + } + + public void testLoginPage() throws Exception + { + HtmlPage response = login(); + assertEquals("JCatalog Welcome", response.getTitleText()); + } + + /** + * Test logout link, does not work correctly. + * + * @throws Exception + */ + public void testLogout() throws Exception + { + HtmlPage response = login(); + HtmlAnchor login = response.getAnchorByText("Logout"); + //getFirstAnchorByText("Logout"); + response = (HtmlPage) login.click(); + // test fails, clicking on links with href="#" seems not to work + // unfortunately JSF generates lots of them :-( + assertEquals("JCatalog Application", response.getTitleText()); + } + + private HtmlPage gotoHomePage() throws FailingHttpStatusCodeException, + IOException + { + WebClient webClient = new WebClient(BrowserVersion.CHROME); +// webClient.setJavaScriptEnabled(true); + //webClient.setRedirectEnabled(true); // does not work :-( + URL url = new URL("http://localhost:8080/JCatalog/pages/home.jsf"); + return (HtmlPage) webClient.getPage(url); + } + + private HtmlPage navigateToLoginPage() throws MalformedURLException, + IOException, SAXException + { + // HtmlPage response = gotoHomePage(); + // HtmlAnchor login = response.getFirstAnchorByText("Login"); + // return (HtmlPage) login.click(); + WebClient webClient = new WebClient(); + //webClient.setRedirectEnabled(true); // does not work :-( + URL url = new URL("http://localhost:8080/JCatalog/pages/login.jsf"); + return (HtmlPage) webClient.getPage(url); + } + + private HtmlPage login() throws MalformedURLException, IOException, + SAXException + { + HtmlPage response = navigateToLoginPage(); + HtmlForm form = response.getFormByName("loginForm"); + HtmlTextInput username = (HtmlTextInput) form + .getInputByName("loginForm:username"); + username.setValueAttribute("admin"); + HtmlPasswordInput password = (HtmlPasswordInput) form + .getInputByName("loginForm:password"); + password.setValueAttribute("masterkey"); + + HtmlSubmitInput button = (HtmlSubmitInput) form + .getInputByName("loginForm:submit"); + return (HtmlPage) button.click(); + } +} -- cgit v1.2.3-70-g09d2