Hey Guys,
I've been working on a little project for fun integrating the Stripes mock
objects and HtmlUnit so that I can test my views without a container or
browser. I'm using the new selenium 2/WebDriver interfaces, so conceivably
these tests could be converted to run with another WebDriver class also.
Have a look at the test class below. The code is thrown together at the moment,
but if this is something that others are interested in, we might be able to
work on extracting it and making it usable outside my environment. Or maybe
this is just a bad idea to start with all together! Either way, i'm curious to
hear your thoughts.
--------------------------------------------------- SignupSpec.java
---------------------------------------------------
package com.kono.action;
import com.kono.stripes.KonoWebSpec;
import net.sourceforge.stripes.validation.ValidationErrors;
import org.openqa.selenium.WebElement;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static javalite.test.jspec.JSpec.the;
public class SignupSpec extends KonoWebSpec
{
@BeforeTest
public void givenUserIsOnSignupPage()
{
get(SignupAction.class);
}
@Test
public void whenSubmittingNoData() throws Exception
{
WebElement submitButton = findElementByName("createAccount");
the(submitButton).shouldNotBeNull();
submitButton.click();
ValidationErrors errors = validationErrors();
the(errors.size()).shouldEqual(3);
}
}
--------------------------------------------------- End Class
---------------------------------------------------
What this class does is actually make a request through Stripes to the default
action on the
SignupAction bean class. It then integrates the view (a freemarker template in
my case) into
the context to be available in any following WebDriver commands.
When "submitButton.click()" is called, another request is made back through
Stripes which
in this case fails validation. The sourcePage is then integrated into the
context, including the
error messages in the html.
Notice how you can mix assertion on both the DOM and the bean class in one
location. (Is this a good idea?)
Also notice that the test class extends my own base class. That base class sets
Stripes up for my
test environment. Unfortunately it duplicates a lot of things that are declared
elsewhere, like in
my web.xml right now. So it sure could use some improvement. Here is what that
looks like
right now
--------------------------------------------------- KonoStripesSpec.java
---------------------------------------------------
package com.kono.stripes;
import com.kono.spring.KonoServletContextListener;
import net.sourceforge.stripes.controller.StripesFilter;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import javax.servlet.ServletContextEvent;
import java.util.HashMap;
import java.util.Map;
public class KonoWebSpec extends StripesSpec
{
ContextLoaderListener springContextLoader;
static
{
StripesSpecServletContext context =
StripesSpec.createContext("kono-web");
//Spring Config
context.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
"classpath:applicationContext.xml");
//JSTL config
context.addInitParameter("javax.servlet.jsp.jstl.fmt.localizationContext",
"translations/text");
//Stripes Filter
Map<String, String> params = new HashMap<String, String>();
params.put("ActionResolver.Packages", "com.kono.action");
params.put("Extension.Packages",
"net.sourceforge.stripes.integration.spring," +
"com.kono.stripes.activejdbc");
params.put("ActionResolver.Class","com.kono.stripes.KonoActionResolver");
params.put("ActionBeanContext.Class",
"com.kono.stripes.KonoActionBeanContext");
params.put("ExceptionHandler.Class",
"com.kono.stripes.KonoExceptionHandler");
params.put("LocalizationBundleFactory.FieldNameBundle","translations/fieldLabels");
params.put("LocalizationBundleFactory.ErrorMessageBundle","translations/errors");
context.addFilter(StripesFilter.class, "StripesFilter", params);
}
@BeforeSuite
public void setupContext() throws Exception
{
springContextLoader = new KonoServletContextListener();
springContextLoader.contextInitialized(new
ServletContextEvent(servletContext()));
}
@AfterSuite
public void teardownContext()
{
springContextLoader.contextDestroyed(new
ServletContextEvent(servletContext()));
}
}
--------------------------------------------------- End Class
---------------------------------------------------
To use this in your environment you'd have to create a base class like this one
to match your setup.
The base class here, StripesSpec, is where the magic starts. There's quite a
number of shortcomings at the moment.
* only freemarker is supported for rendering views
* the DispatcherServlet.class is hardcoded in place and can't be extended
* I do some wierd stuff to find your webappDirectory on disk outside of a
container.
* Some code assumes you're in a Maven project layout
* Some strange and incomplete code to copy request values between stripes mock
objects, and between the mock objects and the WebDriver classes
* No logging
I'm sure there's others that will show up quickly as well....
Looking forward to hearing what you think.
Evan
------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users