Accessibility

Table of Contents

Writing and running functional tests for Flash with Selenium RC

The Selenium RC client

We selected the Selenium RC Java client to trigger the tests. Analogous implementations can be made for the other available Selenium RC clients: .NET, Perl, Python, and Ruby.

We encourage you to run the following Selenium RC Java client JUnit test against the sample web application. But before doing so, please make sure the environment is properly set up and that you are familiar with executing a basic Selenium RC Java client JUnit test.

FlashSelenium is the component that enables Selenium RC Java client to talk to Flash. To use it, all you need to do is add flashselenium-java-client-extension.jar (JAR, 3.6K) to your classpath, the same way you added junit.jar and selenium-client-driver.jar.

The following is the ChangeColorsSiteTest.java, a Selenium-based JUnit test case testing the web application containing the changecolor flash component:

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.SeleniumException;
 
import junit.framework.TestCase;
 
public class ColorsTest extends TestCase {
 private FlashSelenium flashApp;
 private Selenium browser;
 
 private final static String GREEN = "GREEN";
 private final static String BLUE = "BLUE";
 private final static String RED = "RED";
 private final static String URL = "http://www.geocities.com/paulocaroli/flash/colors.html";
 
 public void setUp() {
  browser = new DefaultSelenium("localhost", 4444, "*firefox",URL);
  browser.start();
  flashApp = new FlashSelenium(browser, "clickcolors");
  browser.open(URL);
 }
 
 public void tearDown() {
  browser.stop();
 }
 
  public void testColorTransition() {
    assertEquals("Clicking Colors", browser.getTitle());
    assertEquals(GREEN, flashApp.call("getColor"));
    flashApp.call("click");
    assertEquals(BLUE, flashApp.call("getColor"));
    flashApp.call("click");
    assertEquals(RED, flashApp.call("getColor"));
    flashApp.call("click");
    assertEquals(GREEN, flashApp.call("getColor"));
  }
 
 public void testRectangleLabel() {
   assertEquals("(Click here)", flashApp.call("getSquareLabel"));
   flashApp.call("setSquareLabel", "Dummy Label");
   assertEquals("Dummy Label", flashApp.call("getSquareLabel"));
 }
}

Notice at the testColorTransition method that the following statement is testing the web page title:

assertEquals("Clicking Colors", browser.getTitle());

This exemplifies that, beyond adding Flash testing capabilities, you are able to use Selenium for testing all other web application objects. You are also able to do all the web application verifications from the same test (if appropriate). This capability is particularly interesting for validating the interoperability of web application components—for example, validating corresponding values from the Flash component and the rest of the web application.

The remaining part of the testColorTransition method validates the color transition logic. Note that the changeColor() ActionScript 3.0 code method is directly invoked by the Selenium RC Client code:

flashApp.call("click");

The testRectangleLabel method shows a scenario in which the test validates an external parameter being passed to the Flash application, the rectangle label. Figure 3 shows the ChangeColorsSiteTest executed within the Eclipse IDE.

Executing the ChangeColorSiteTest within
Eclipse

Figure 3. Executing the ChangeColorSiteTest within Eclipse

Figures 4 and 5 show the sequence (within the browser started by Selenium) when executing the testRectangleLabel() test.

testRectangleLabel() in execution: Step 1

Figure 4. testRectangleLabel() in execution: Step 1

testRectangleLabel() in execution: Step 2

Figure 5. testRectangleLabel() in execution: Step 2

Where to go from here

This article showed you how to run Selenium tests for your Flash-based web application. On the Selenium RC client side, you use FlashSelenium (for Java, available as flashselenium-java-client-extension.jar), a Selenium RC client extension that enables communication between the Selenium RC client and the Flash application. On the Flash side, you can verify the default functions of any Flash SWF (e.g., isPlaying, PercentLoaded, and TotalFrames) and/or a specific Flash application's functions externalized by Flash ExternalInterface. We walked you through testing a simple web application containing a Flash SWF object. For more examples, please refer to the Flash Selenium project page.

FlashSelenium is useful for testing both Flash and Flex applications. As we demonstrated in this article, the tests are executed against the deployed SWF. Flex originally was designed to support enterprise development for the Flash platform. For instance, Flex is geared towards application development. Testing is a fundamental practice for a successful enterprise development practice. Therefore, Flex developers should add Selenium and FlashSelenium to their test tools arsenal.

Selenium RC is the proposed solution's main component. Selenium drives the functional tests against the web application embedding the Flash SWFs. Because Selenium is an active open-source project, we can benefit from its current and future capabilities (for instance, Selenium Grid could be used for running tests in parallel and for validating Flash web applications' compatibility against a variety of browsers and operational systems). Also, as is usually the case, the Flash application executes in a web browser; and by using Selenium (and enhancing it to work with Flash), we are able to test the Flash application as well as the other web application components.

Our special thanks to Mark Striebeck, Paul Hammant, and Jason Huggins for encouraging and supporting this work.