ADF - MyFaces, HttpUnit testing problem with Javascript.
503850Apr 13 2006 — edited Apr 17 2006Dear all
Does anybody know how to test ADF Faces. Any tool would be of interest to me although Opensource and Free Software is prefered?
My task is to write a set of unit tests to test an Oracle ADF and Apache MyFaces application. I have attempted to use both Cactus and HttpUnit. However I am struggling with a simple test case which should simulate a login.
The problem I have is that ADF Faces produces the following dynamic javascript file (I am guessing it is dynamic because I can not find this in either adf-faces-impl-10_1_3_0_4.jar or adf-faces-api-10_1_3_0_4.jar which are the two jars am implementing in my project.):
Common10_1_3_0_4.js
Which when parsed by js.jar (Rhino javascript engine) I get the following error:
com.meterware.httpunit.ScriptException: Event 'submitForm('maincontent:loginForm',1, {source:'maincontent:submit'});return false;' failed: org.mozilla.javascript.EcmaError: TypeError: Cannot call method "split" of undefined at
com.meterware.httpunit.javascript.JavaScript$JavaScriptEngine.handleScriptException (JavaScript.java:202)
(This file Common10_1_3_0_4.js is over 5000 lines long and so I do not want to post it as this posting is already quite long).
Downloading the file directly after it has been built and modifying it and fixing the error by ensuring the split function is called on a strongly typed String variable, then commenting out from the web.xml below I tried the HttpUnit test again.
<servlet>
<servlet-name>resources</servlet-name>
<servlet-class>oracle.adf.view.faces.webapp.ResourceServlet</servlet-class>
</servlet>
<!-- servlet-mapping>
<servlet-name>resources</servlet-name>
<url-pattern>/adf/*</url-pattern>
</servlet-mapping -->
This time I get no error, but the original login page simply gets served again.
login.jsp (which is called as login.jsf)
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib uri="http://xmlns.oracle.com/adf/faces/html" prefix="afh"%>
<%@ taglib uri="http://xmlns.oracle.com/adf/faces" prefix="af"%>
<!-- Content -->
<af:objectSeparator />
<af:panelBox text="#{msg['login.info.loginHeader']}" width="100%" background="transparent">
<af:form id="loginForm">
<af:objectSpacer height="10px" />
<af:panelGroup layout="vertical">
<af:panelForm labelWidth="5%">
<!--User Name-->
<af:panelLabelAndMessage for="username">
<af:inputText label="User Name:"
id="username"
columns="25"
required="yes"
secret="false"
value="#{userBean.userName}"
shortDesc="#{msg['global.user.userName']}" />
</af:panelLabelAndMessage>
<af:panelLabelAndMessage for="password">
<af:inputText label="Password:"
id="password"
columns="25"
required="yes"
secret="true"
value="#{userBean.password}"
shortDesc="#{msg['global.user.password']}" />
</af:panelLabelAndMessage>
<af:objectSpacer height="10px" />
<!--Login Button-->
<af:commandButton id="submit"
textAndAccessKey="#{msg['global.button.login']}"
action="#{userBean.login}" />
</af:panelForm>
</af:panelGroup>
</af:form>
</af:panelBox>
TestCase LoginTest.java:
package com.siemens.pse.wmstesting;
import java.io.IOException;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
public class LoginTest extends TestCase {
/**
* The URL of the Login page for all login tests
*/
public static final String URL = new String( "http://localhost/WMS/pages/login.jsf" );
public static void main( String args[] ) {
junit.textui.TestRunner.run( suite() );
}
public static TestSuite suite() {
return new TestSuite( LoginTest.class );
}
public LoginTest( String s ) {
super( s );
}
public void testGetLogin() throws Exception {
response = new WebConversation( ).getResponse( request );
// Test the result
assertContains( response, "Login Management" );
}
public void testLoginSuccess() throws Exception {
response = new WebConversation( ).getResponse( request );
WebForm form = response.getFormWithID( "maincontent:loginForm" );
assertNotNull( "No form found with ID 'maincontent:loginForm'", form );
form.setParameter( "maincontent:username", "admin" );
form.setParameter( "maincontent:password", "pass" );
WebLink submit = response.getLinkWithID("maincontent:submit");
response = submit.click();
System.out.println( response.getText());
// Test the result
assertContains( response, "Welcome to the Temperature Measurement System" );
}
/**
* Convenience function that asserts that a substring can be found in
* the returned HTTP response body.
*
* @param theResponse the response from the server side.
* @param s the substring to look for
*/
public void assertContains( WebResponse response, String s ) {
String target = new String("");
try {
target = response.getText(); }
catch (IOException e) {
e.printStackTrace(); }
if ( target.indexOf( s ) < 0 ) {
// Error showing which string was NOT found
fail( "Response did not contain the substring: [" + s + "]" );
}
}
private WebRequest request = new GetMethodWebRequest( LoginTest.URL );
private WebResponse response;
}