|
QuickTutorial
FlexMonkey 0.8 Tutorial.
Important Note: This document describes FlexMonkey 0.8. To find documentation for later versions, please visit the FlexMonkey Project Home site. Running the Sample ApplicationPlease download the source and binaries. The sample consists of the following components:
MonkeyContacts.mxml contains no flexmonkey-specific code. Any application can be tested with flexmonkey without requiring any source or binary modifications. As mentioned above, FlexMonkeyLauncher.swf launches a swf to be tested. FlexMonkeyLauncher provides the Flex automation API as well as the flexmonkey API and user interface needed for creating and running tests. Prior to FlexMonkey 0.6, it was necessary to create a special version of your application swf that was linked with the FlexMonkey and Flex Automation libraries. Now however, FlexMonkeyLauncher provides these dynamically to any application it loads, so the swf you test can be the very same swf as the one you will deploy into production. To use FlexMonkeyLauncher, you must first copy FlexMonkeyLauncher.swf to the directory containing the application you want to test. In the example FlexMonkeyLauncher.swf and MonkeyContacts.swf are both located in the build directory. Open FlexMonkeyLauncher.swf in the standalone FlashPlayer. The FlexMonkey window will open. On the Setup tab, enter MonkeyContacts in the App to Test field and click the Load SWF button. The MonkeyContacts app should be displayed under the FlexMonkey window. Recording a ScriptClick the Record toggle button at the top of the screen. Now play around with the contact manager, which although primitive, offers a variety of UI gestures you can record. In addition to typing in the form, you can edit rows after they've been added to the grid, and the Phone Type field has a ComboBox itemEditor. !Flexmonkey will faithfully record all UI events (many events such as mousemoves are filtered by default). !Flexmonkey displays each recorded event as a command in the Command List. Clicking anywhere in the Flexmonkey window will stop recording.
Playing Back a ScriptClick the Play button on the Command List window. Each recorded action will be replayed. By default, !Flexmonkey pauses for a half second between commands to give each UI action time to complete. You can play subsets of commands from the list by selecting them in the grid and then hitting Run. If no rows are selected, !Flexmonkey will play all the commands. Generating a FlexUnit TestClick on the FlexUnit TestCase tab to view the generated source code. Two methods are generated, the first calls FlexMonkey.runCommands passing it an array of FlexCommands generated from the Flexmonkey Command List. The second method contains a stub body where you would write actual validation code specific to what's being tested by the command sequence. You can copy and paste this code into a FlexUnit TestCase and add it to any FlexUnit TestSuite. (With minor modifications, the source can be used with other automated testing suites besides FlexUnit).
Click the Show completeTestCase checkbox and a complete FlexUnit TestCase will be generated.
Copy and paste the generated code into an ActionScript source file and add testing validations. The example contains the file src/test/FlexUnitTests.as that provides an example: package test
{
import com.gorillalogic.flexmonkey.commands.CallCommand;
import com.gorillalogic.flexmonkey.commands.CommandRunner;
import com.gorillalogic.flexmonkey.commands.FlexCommand;
import com.gorillalogic.flexmonkey.core.MonkeyEvent;
import com.gorillalogic.flexmonkey.core.MonkeyUtils;
import flexunit.framework.Assert;
import flexunit.framework.TestCase;
import mx.collections.ArrayCollection;
import mx.controls.DataGrid;
import mx.controls.DateField;
public class FlexUnitTests extends TestCase {
// Test method
public function testSomething():void {
var cmdRunner:CommandRunner = new CommandRunner();
cmdRunner.addEventListener(MonkeyEvent.READY_FOR_VALIDATION, addAsync(verifySomething, 10000));
cmdRunner.runCommands([
new FlexCommand("inName", "SelectText", ["0", "0"], "automationName"),
new FlexCommand("inName", "Input", ["Fred"], "automationName"),
new FlexCommand("inType", "Open", ["null"], "automationName"),
new FlexCommand("inType", "Select", ["Work", "1", "0"], "automationName"),
new FlexCommand("inPhone", "SelectText", ["0", "0"], "automationName"),
new FlexCommand("inPhone", "Input", ["555 555 5555"], "automationName"),
// The following command was inserted manually to demonstrate a workaround for DateField bug
// See http://groups.google.com/group/flexmonkey/browse_thread/thread/bf4af5e1d8164608# for more info
new CallCommand(function():void {DateField(MonkeyUtils.findComponentWith("inDate")).open()}),
new FlexCommand("inDate", "Open", ["null"], "automationName"),
new FlexCommand("inDate", "Change", ["Fri Dec 26 2008"], "automationName"),
new FlexCommand("Add", "Click", ["0"], "automationName")
]);
}
// Called after commands have been run
private function verifySomething(event:MonkeyEvent):void {
var comp:DataGrid = MonkeyUtils.findComponentWith("grid","id") as DataGrid;
Assert.assertEquals("Fred", ArrayCollection(comp.dataProvider).getItemAt(0).name);
Assert.assertEquals("Work", ArrayCollection(comp.dataProvider).getItemAt(0).type);
} }
}
Packaging your tests as a SWFWe can package the tests in their own swf so that they're run by FlexMonkey without being linked directly into your application swf. In this way, no test code gets included with your deployed application. We package up the tests into their own swf by invoking them from the (trivial) application FlexMonkeyTests.mxml: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="create()">
<mx:Script>
<![CDATA[
import test.FlexUnitTests;
import com.gorillalogic.flexmonkey.ui.FlexMonkey;
public function create():void {
// Run with the FlexMonkey's built-in testrunner
FlexMonkey.addTestSuite(FlexUnitTests);
}
]]>
</mx:Script>
</mx:Application>As you can see, all this application is doing is calling: FlexMonkey.addTestSuite(FlexUnitTests); This call adds our tests contained in FlexUnitTests.as to FlexMonkey's built-in flexunit runner, which is contained in the FlexUnit Runner tab of the FlexMonkey window. Add FlexMonkey.swc (included in the lib directory of the example) to your build path to compile the testing application above. Running the TestsRestart FlexMonkeyLauncher and on the Setup Tab enter MonkeyContacts in the App to Test field and click the Load SWF button, then enter FlexMonkeyTests in the Tests to Run field and click the other Load SWF button. The FlexUnit Runner tab will be displayed and its embedded FlexUnit runner will display the test results. Learning MoreSee the user guide. |
Sign in to add a comment