diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-12-13 23:19:50 +0100 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-12-13 23:36:47 +0100 |
| commit | af6e75be99c0dd600583f1a2c42c64f9a89d7b66 (patch) | |
| tree | 7751668efc44de1c7be8b47d061063d18f832816 /test/htmlunit | |
| parent | 5f30a827fc84af1e54f0716a49995e98a044ebd6 (diff) | |
| download | JCatalog-testing.tar.gz JCatalog-testing.zip | |
Add JUnit3 based HtmlUnit-Testsuitetesting
Diffstat (limited to 'test/htmlunit')
| -rw-r--r-- | test/htmlunit/JCatalogTest.java | 101 |
1 files changed, 101 insertions, 0 deletions
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<HtmlForm> forms = (List<HtmlForm>) 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(); + } +} |
