summaryrefslogtreecommitdiffstats
path: root/test/htmlunit/JCatalogTest.java
blob: a1c8f406357256b00427fb9ff8188776815879ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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();
   }
}