Author: ozizka(a)redhat.com
Date: 2009-03-06 18:50:15 -0500 (Fri, 06 Mar 2009)
New Revision: 191
Modified:
trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/MultipleUserLoginTest.java
Log:
* Multi-user login updated.
Modified: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/MultipleUserLoginTest.java
===================================================================
---
trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/MultipleUserLoginTest.java 2009-03-06
23:03:55 UTC (rev 190)
+++
trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/MultipleUserLoginTest.java 2009-03-06
23:50:15 UTC (rev 191)
@@ -23,13 +23,15 @@
package org.jboss.jopr.jsfunit;
import com.gargoylesoftware.htmlunit.BrowserVersion;
-import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
-import com.gargoylesoftware.htmlunit.html.HtmlDivision;
-import com.gargoylesoftware.htmlunit.html.HtmlPage;
+import com.gargoylesoftware.htmlunit.Page;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.html.*;
import java.io.IOException;
+import java.util.List;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.jboss.jsfunit.framework.WebClientSpec;
+import org.jboss.jsfunit.framework.WebConversationFactory;
import org.jboss.jsfunit.jsfsession.JSFClientSession;
import org.jboss.jsfunit.jsfsession.JSFServerSession;
import org.jboss.jsfunit.jsfsession.JSFSession;
@@ -117,11 +119,12 @@
/**
- * Logs in using several
- * @throws java.io.IOException
+ * Logs in using several
+ * @throws java.io.IOException
*
- * Fails. TODO: Add user/pass for 2nd user.
- */
+ * Fails because the page header is missing for some reason.
+ * TODO: Find out why.
+ */
public void testMultipleUsersLogin() throws IOException
{
@@ -141,52 +144,89 @@
assertEquals("JSF's '#{identity.username}' should be the user
name.",
USER_A, sessA.getServer().getManagedBeanValue("#{identity.username}") );
- HtmlAnchor logoutLink_ = (HtmlAnchor)
sessA.getClient().getElement("logoutLink");
+ DebugUtils.writeFile("target/multiuser1.html",
sessA.getClient().getPageAsText());///
// USER B.
+ final WebClient wc = new WebClient(BrowserVersion.FIREFOX_3);
+ wc.setThrowExceptionOnFailingStatusCode(false);
+ wc.setConfirmHandler(new SimpleConfirmHandler(true));
+ final WebClientTools sessBclient = new WebClientTools(wc);
- log.info("Logging in with second user, '"+USER_B+"'");
- SessionInstance sessB = new SessionInstance(USER_B, PASS_B);
+ final String userNameComponent = "login_form:name";
+ final String passwordComponent = "login_form:password";
+ final String submitComponent = "login_form:submit";
+ String url = WebConversationFactory.getWARURL() + "/";
+ final HtmlPage loginPage = wc.getPage(url);
+ setValue(loginPage, userNameComponent, USER_B);
+ setValue(loginPage, passwordComponent, PASS_B);
+ clickSubmitComponent(loginPage, submitComponent);
assertTrue("Page doesn't contain user's name.",
- sessB.getClient().getPageAsText().contains(USER_B) );
+ sessBclient.getPageAsText().contains(USER_B) );
assertTrue("Page doesn't contain '"+LABEL_LOGOUT+"'.",
- sessB.getClient().getPageAsText().contains(LABEL_LOGOUT) );
+ sessBclient.getPageAsText().contains(LABEL_LOGOUT) );
- assertEquals("JSF's '#{identity.username}' should be the user
name.",
- USER_B, sessB.getServer().getManagedBeanValue("#{identity.username}") );
+
+ // Refresh and log USER_A out.
+ ((HtmlPage) sessA.getClient().getContentPage()).refresh();
+ DebugUtils.writeFile("target/multiuser2.html",
sessA.getClient().getPageAsText());///
- // Log USER_A out.
-
- /*HtmlDivision headerDiv =
(HtmlDivision)sessA.getClient().getElement("pageheader");
- HtmlAnchor logoutLink = (HtmlAnchor) headerDiv
- .getByXPath("a[normalize-string() =
'"+LABEL_LOGOUT+"']"); /**/
-
HtmlAnchor logoutLink = (HtmlAnchor)
sessA.getClient().getElement("logoutLink");
logoutLink.click();
// Refresh sessB page Check that USER_B is still logged in.
-
- HtmlPage page = (HtmlPage) sessB.getClient().getContentPage();
- page.refresh();
-
assertTrue("Page doesn't contain user's name.",
- sessB.getClient().getPageAsText().contains(USER_B) );
+ sessBclient.getPageAsText().contains(USER_B) );
assertTrue("Page doesn't contain '"+LABEL_LOGOUT+"'.",
- sessB.getClient().getPageAsText().contains(LABEL_LOGOUT) );
+ sessBclient.getPageAsText().contains(LABEL_LOGOUT) );
+ }
- assertEquals("JSF's '#{identity.username}' should be the user
name.",
- USER_B, sessB.getServer().getManagedBeanValue("#{identity.username}") );
+
+ // These were taken from FomrAuthenticationStrategy.java; unfortunately not reusable
from there.
+
+ protected Page clickSubmitComponent(HtmlPage page, String elementName) throws
IOException
+ {
+ HtmlElement htmlElement = getElement(page, elementName);
+ if (!(htmlElement instanceof ClickableElement))
+ throw new IllegalStateException("Component with name=" + elementName +
" is not a ClickableElement.");
+
+ ClickableElement clickable = (ClickableElement)htmlElement;
+ return clickable.click();
}
+ public static void setValue(HtmlPage page, String elementName, String value)
+ {
+ HtmlElement element = getElement(page, elementName);
+ if (!(element instanceof HtmlInput))
+ {
+ throw new IllegalStateException("Component with name=" + elementName +
" is not an HtmlInput element.");
+ }
+ HtmlInput inputElement = (HtmlInput)element;
+ inputElement.setValueAttribute(value);
+ }
+ public static HtmlElement getElement(HtmlPage page, String elementName)
+ {
+ List<HtmlElement> elements = page.getHtmlElementsByName(elementName);
+ if (elements.size() == 0)
+ {
+ throw new IllegalStateException("Component with name=" + elementName +
" was not found on the login page.");
+ }
+ if (elements.size() > 1)
+ {
+ throw new IllegalStateException("More than one component with name=" +
elementName + " was found on the login page.");
+ }
+ return elements.get(0);
+ }
+
+
}// class
Show replies by date