wood burning stoves 2.0*
The moose likes JSF and the fly likes ADF MyFaces and HttpUnit testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "ADF MyFaces and HttpUnit testing" Watch "ADF MyFaces and HttpUnit testing" New topic
Author

ADF MyFaces and HttpUnit testing

Andrew Och
Ranch Hand

Joined: Mar 19, 2004
Posts: 32
Dear 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 I 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 is in excess of 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 -->
<afbjectSeparator />
<af:panelBox text="#{msg['login.info.loginHeader']}" width="100%" background="transparent">
<af:form id="loginForm">
<afbjectSpacer 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>
<afbjectSpacer 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 theSubstring 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;
}
Andrew Och
Ranch Hand

Joined: Mar 19, 2004
Posts: 32
Dear All

Well I found the solution to my testing requirements. HtmlUnit is able to deal with the dynamic Javascript that is generated by the ADF Faces application. The strange part is that HtmlUnit uses the Rhino Javascript engine just as HttpUnit and Cactus, however these two could not deal with the javascript whereas HtmlUnit could.

I include two of the test cases that I wrote that now work.


public void testLoginSuccess() throws Exception {
final WebClient webClient = new WebClient( BrowserVersion.INTERNET_EXPLORER_6_0 );
final URL url = new URL("http://localhost/WMS/pages/login.jsf");
final HtmlPage page = (HtmlPage)webClient.getPage(url);

// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page.getFormByName("maincontent:loginForm");

final HtmlTextInput usernameField = (HtmlTextInput) form.getInputByName("maincontent:username");
final HtmlPasswordInput passwordField = (HtmlPasswordInput) form.getInputByName("maincontent assword");

final ClickableElement button = (ClickableElement) form.getHtmlElementById("maincontent:submit");

// Change the value of the text field
usernameField.setValueAttribute("admin");
passwordField.setValueAttribute("pass");

// Get the submitted form
final HtmlPage welcomePage = (HtmlPage) button.click();

System.out.println( welcomePage.getWebResponse().getContentAsString() );

assertEquals( "Temperature Measurement System", welcomePage.getTitleText() );
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ADF MyFaces and HttpUnit testing