Issue while getting teh value of session scoped variable whe
mritunjay_nitr Nov 22, 2008 7:02 AMWhile testing login.xhtml', i am facing issue.
Code for login.xhtml is
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ig="http://www.infragistics.com/faces/netadvantage"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:fic="http://www.fairisaac.com/edm/cc/ui/components"
xmlns:cc="http://www.fairisaac.com/edm/cc/ui/components/messages"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
Debt Manager 8.0
Don't have an account?
<h2>Please sign into your account.</h2>Access to the Fair Isaac applications requires verification of your username and password.
<f:view>
<h:form id="loginForm">
<h:outputLabel value="Login ID" for="username" disabled="#{loginMBean.authenticateDisabled}"/>
<h:inputText id="username" value="#{loginMBean.username}" disabled="#{loginMBean.authenticateDisabled}"/>
<h:outputText id="invalidloginid" value="#{loginMBean.invalidLoginid}"/>
<h:outputLabel value="Password" for="password" disabled="#{loginMBean.authenticateDisabled}"/>
<h:inputSecret id="password" value="#{loginMBean.password}" disabled="#{loginMBean.authenticateDisabled}" redisplay="true"/>
<h:outputText id="invalidpassword" value="#{loginMBean.invalidPassword}"/>
<h:outputText value="Forgot your Login ID or Password?" disabled="#{loginMBean.authenticateDisabled}"/>
<h:commandButton value="Authenticate" id="authenticate" action="authenticate" actionListener="#{loginMBean.authenticate}" disabled="#{loginMBean.authenticateDisabled}"/>
<h:outputLabel value="Tenant" for="pickTenant" disabled="#{loginMBean.loginDisabled}"/>
<h:selectOneListbox id="pickTenant" value="#{loginMBean.currentTenant}" disabled="#{loginMBean.loginDisabled}" size="1">
<f:selectItems value="#{loginMBean.tenantList}" />
</h:selectOneListbox>
<h:outputLabel value="Locale" for="pickLocale" disabled="#{loginMBean.loginDisabled}"/>
<h:selectOneListbox id="pickLocale" value="#{tmsLocaleBean.localization}" disabled="#{loginMBean.loginDisabled}" size="1">
<f:selectItems value="#{loginMBean.localeList}" />
</h:selectOneListbox>
<h:commandButton value="Log In" id="logIn" action="#{loginMBean.getHomepage}" actionListener="#{loginMBean.login}" disabled="#{loginMBean.loginDisabled}" />
<h:commandButton value="Reset" id="reset" action="reset" actionListener="#{loginMBean.reset}" disabled="#{loginMBean.loginDisabled}" immediate="true"/>
<h:messages globalOnly="false" />
</h:form>
</f:view>
Copyright 2008 Fair Isaac Corporation. All rights reserved.
<!-- end container-->
The flow is:
1) When the page is first loadced the 'Authenticate' button is not disabled and 'LogIn' and 'Reset' button is disabled.
2) When the authentication is succesfull, the 'Authenticate' button is disabled and 'LogIn' and 'Reset' button is not disabled.
and control comes to 'Login.xhml'.
3) When user clicks 'Reset' button control comes to login.xhtml and 'Authenticate' button is not disabled and 'LogIn' and 'Reset' button is disabled.
4) when user clicks 'LogIn' button flow redirected to soem different page.
Scope of the LoginMbean is 'Session'
Code for my test class is
package com.fairisaac.dms.fidem.jsfunit.test;
import java.io.IOException;
import java.util.Map;
import javax.faces.component.UIComponent;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.cactus.ServletTestCase;
import org.jboss.jsfunit.framework.JSFUnitWebConnection;
import org.jboss.jsfunit.framework.WebClientSpec;
import org.jboss.jsfunit.jsfsession.JSFClientSession;
import org.jboss.jsfunit.jsfsession.JSFServerSession;
import org.jboss.jsfunit.jsfsession.JSFSession;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import com.fairisaac.dms.fidem.jsfunit.listener.JsfUnitRequestListener;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.ClickableElement;
/**
* JSFUnit test class to test login.xhtml
* @author MritunjayKumar
*
*/
public class JSFUnitTestForLoginPage extends ServletTestCase {
private JSFClientSession client;
private JSFServerSession server;
/**
* JUnit suite() method
* @return
*/
public static Test suite()
{
return new TestSuite( JSFUnitTestForLoginPage.class );
}
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
public void setUp() throws IOException, SAXException
{
//set the initial page here
WebClientSpec wcSpec = new WebClientSpec("/pages/cc/login.faces");
//This portion of code is required to run JSFUnit test in web sphere server.
InitialRequestStrategy requestStrategy = new InitialRequestStrategy();
wcSpec.setInitialRequestStrategy(requestStrategy);
WebClient webClient = wcSpec.getWebClient();
JSFUnitWebConnection webConnection = (JSFUnitWebConnection)webClient.getWebConnection();
webConnection.addListener(new JsfUnitRequestListener());
JSFSession jsfSession = new JSFSession(wcSpec);
client = jsfSession.getJSFClientSession();
server = jsfSession.getJSFServerSession();
}
/**
* method to validate input data for Login page.
* @throws IOException
* @throws SAXException
*/
public void testInputValidation() throws IOException, SAXException
{
client.setValue("loginForm:username", "");
client.setValue("loginForm:password", "");
ClickableElement authenticateButton = (ClickableElement)client.getElement("loginForm:authenticate");
authenticateButton.click();
assertEquals("/pages/cc/login.xhtml", server.getCurrentViewID());
assertEquals("Invalid login id",server.getManagedBeanValue("#{loginMBean.invalidLoginid}"));
assertEquals("Invalid password",server.getManagedBeanValue("#{loginMBean.invalidPassword}"));
checkComponentForDisabled("loginForm:pickTenant", true);
checkComponentForDisabled("loginForm:pickLocale", true);
checkComponentForDisabled("loginForm:authenticate", false);
checkComponentForDisabled("loginForm:logIn", true);
checkComponentForDisabled("loginForm:reset", true);
}
/**
* method to test authenticate button
* @throws IOException
* @throws SAXException
*/
public void testAuthenticate() throws IOException, SAXException
{
client.setValue("loginForm:username", "collector");
client.setValue("loginForm:password", "collector");
ClickableElement authenticateButton = (ClickableElement)client.getElement("loginForm:authenticate");
authenticateButton.click();
assertEquals("/pages/cc/login.xhtml", server.getCurrentViewID());
if((Boolean)server.getManagedBeanValue("#{loginMBean.authenticateDisabled}")){
checkComponentForDisabled("loginForm:authenticate", true);
checkComponentForDisabled("loginForm:logIn", false);
checkComponentForDisabled("loginForm:reset", false);
}
assertNotNull(server.getManagedBeanValue("#{loginMBean.username}"));
assertNotNull(server.getManagedBeanValue("#{loginMBean.password}"));
checkComponentForDisabled("loginForm:pickTenant", false);
checkComponentForDisabled("loginForm:pickLocale", false);
}
/**
* method to test LogIn button
* @throws IOException
* @throws SAXException
*/
public void testLogIn() throws IOException, SAXException
{
client.setValue("loginForm:username", "collector");
client.setValue("loginForm:password", "collector");
Element tenantDropDown = client.getElement("loginForm:pickTenant");
tenantDropDown.setNodeValue("tenant");
Element localeDropDown = client.getElement("loginForm:pickLocale");
localeDropDown.setNodeValue("en_US");
ClickableElement logInButton = (ClickableElement)client.getElement("loginForm:logIn");
logInButton.removeAttribute("disabled");
logInButton.click();
String userName = (String)server.getManagedBeanValue("#{loginMBean.username}");
if(userName.startsWith("admin")){
assertEquals("/pages/fidem/home/administratorHome.xhtml", server.getCurrentViewID());
}
else{
assertEquals("/pages/fidem/home/collectorHome.xhtml", server.getCurrentViewID());
}
// FacesContext facesContext = server.getFacesContext();
// Application application = facesContext.getApplication();
// LoginMBean loginBean = (LoginMBean)application.createValueBinding("#{loginMBean}").getValue(facesContext);
// assertEquals("collector", loginBean.getUsername());
// assertEquals("collector", loginBean.getPassword());
}
/**
* method to test reset button
* @throws IOException
* @throws SAXException
*/
public void testReset() throws IOException, SAXException
{
ClickableElement clickable = (ClickableElement)client.getElement("loginForm:reset");
clickable.click();
assertEquals("/pages/cc/login.xhtml", server.getCurrentViewID());
assertNull(server.getManagedBeanValue("#{loginMBean.username}"));
assertNull(server.getManagedBeanValue("#{loginMBean.password}"));
checkComponentForDisabled("loginForm:authenticate", false);
checkComponentForDisabled("loginForm:logIn", true);
checkComponentForDisabled("loginForm:reset", true);
checkComponentForDisabled("loginForm:pickTenant", true);
checkComponentForDisabled("loginForm:pickLocale", true);
}
/**
* Check any component whther eit is disable or not.
* @param componentName String name of the component to check for disable..
* @param forDisable boolean, true if component is disabled.
*/
private void checkComponentForDisabled(String componentName, boolean forDisable){
UIComponent component = server.findComponent(componentName);
Map componentAttributes = component.getAttributes();
assertEquals(forDisable, componentAttributes.get("disabled"));
}
}
Issues that i am facing is:
1) All the test methods are running properly except 'testLogIn' method. Assert is failing for this test method. It is still expecting login.xhtml.
If From the 'login.xhtm; if i removed the disabled attribute for the 'LogIn' button, i am able to run the this test method.
2) I am not able to get the values of the attributes of LoginMbean, even though 'LoginMbean in session scope.
FacesContext facesContext = server.getFacesContext();
Application application = facesContext.getApplication();
LoginMBean loginBean = (LoginMBean)application.createValueBinding("#{loginMBean}").getValue(facesContext);
assertEquals("collector", loginBean.getUsername());
assertEquals("collector", loginBean.getPassword());
Value of the loginBean.getUsername() and loginBean.getPassword() is null.
My application is deployed on web sphere process server.
Please solve me solving this issue as soon as possible.
Regards,
Mritunjay