Friday, February 22, 2008

FunFX and custom components


Hi, I have for a while now been supposed build a custom component with custom events and post how to to this here.

I am eventually finshed an example of how to create a custom component, which is not possible to act upon with the regular actions from FunFX.

(By the way, I followed this tutorial, Create custom component Flex, it is for QTP, but are in many ways the same way to create a custom component for FunFX.

Thanks alot to Jarek for pointing out the problem I had. I used the wrong variable in the call to the super class' constructor. I used a couple of hours yesterday banging my head in to the wall. But after the help from Jarek everything fell into place.

It is actually very easy to implement an custom component, and use it with FunFX. What you need to do is 4 steps:

1. Create the custom component

2. Create the custom event

3. Create the delegate that extends for instance UIComponentAutomationImpl

4. Add the component to the AutomationGenericEnv.xml file


(Does not need to do them in that order)


Create the custom component


I chose to create a simple component listing different buttons, which could easily be done with a repeater, but that is not the point. I wanted to find out and hopefully share my findings with you guys, that haven't tried it yet. What I wanted to do was to implement my own event, so that I could use the itemrenderer tag in the event to make the buttons be pushed.

Below is the code for the custom component. It just extends from a existing component and overrides the necessary methods like, createChildren etc. What is important to notice here is that you need to add you children, that you want to reach into the _renderers so that they can be used by the delagate as automationChildren. The top event sentence makes it possible for the delegate to replay that customevent, which I will create after this.


[Event(name="itemClick", type="event.CustomItemClickEvent")]
public class CustomComponent extends VBox
{
private var _dataProvider:Array;
private var _renderers:Array = [];
private var _itemRenderer:IFactory = new ClassFactory(Button);

public function CustomComponent()
{
super();
}

override protected function createChildren():void{
super.createChildren();
for(var i:int=0; i<_dataProvider.length; i++){
var inst:UIComponent = _itemRenderer.newInstance();
inst.addEventListener(MouseEvent.CLICK, itemClickHandler);
this.addChild(inst);
_renderers.push(inst);

Button(inst).label = _dataProvider[i];
}
invalidateSize();
invalidateDisplayList();
}
//You must also implement an itemClickHandler, for the buttons, but that is nothing special
}


Create the custom event


To be able to click on the button, I need to create a custom event that makes this happen. This event holds a button which is the type of item the custom component is holding. It is probably possible to do this a bit more generic, but this was just to understand how things works.


public class CustomItemClickEvent extends Event
{
public var itemRenderer:Button;
public static const ITEM_CLICK:String = "itemClick";

public function CustomItemClickEvent(type:String){
super(type, bubbles, cancelable);
}
}


Create the delegate


The delegate must be included to the application project that uses the custom component. I chose to build an swc of the delegate and add it as the FUnFXAdapter.swc file.

Below is the code from the delegate. It is important to use the [Mixin] attribute, as this forces the delegate's init methd to be run when your application starts. The numAutomationChild and getAutomationChild methods you will have to implement concerning how your custom component added items to the renders array. Here you can do what you want.


[Mixin]
public class CustomComponentDelegate extends UIComponentAutomationImpl
{
public static function init(root:DisplayObject):void
{
Automation.registerDelegateClass(CustomComponent, CustomComponentDelegate);
}

private var comp:CustomComponent;
public function CustomComponentDelegate(component:CustomComponent)
{
super(component);
comp = component;
}

override public function get numAutomationChildren():int
{
var renderers:Array = comp.getItemRenderers();
if(renderers != null)
return renderers.length;
else return 0;
}

override public function getAutomationChildAt(index:int):IAutomationObject
{
var renderers:Array = comp.getItemRenderers();
if(renderers == null) return null;
if(index >= 0 && index < renderers.length)
return renderers[index];
else
return null;
}

override public function replayAutomatableEvent(event:Event):Boolean
{
var help:IAutomationObjectHelper = Automation.automationObjectHelper;
if (event is CustomItemClickEvent)
{
var rEvent:CustomItemClickEvent = event as CustomItemClickEvent
help.replayClick(rEvent.itemRenderer);
(uiComponent as IInvalidating).validateNow();
return true;
}
else
return super.replayAutomatableEvent(event);
}


Add the component to the AutomationGenericEnv.xml file


When everything is created you can add the component to the AutomationGenericEnv.xml file. Below is what I added somewhere in the file below the Object (because it is based on inheritance and the file is parsed.)


<ClassInfo Name="CustomComponent" Extends="Object" SupportsTabularData="true">
<Implementation Class="CustomComponent"/>
<Events>
<Event Name="ItemClick">
<Implementation Class="customEvent::CustomItemClickEvent" Type="itemClick"/>
<Property Name="itemRenderer">
<PropertyType Type="String" />
</Property>
</Event>
</Events>
<Properties>
</Properties>
</ClassInfo>


Building a test application and writing a test


Now you are ready to write an application using the component and a test which interacts with the component.


<Application="" mx="http://www.adobe.com/2006/mxml" component="component.*">
<mx:VBox>
<component:CustomComponent="" id="cust" dataprovider="['Nr1','Nr2']"/>
</mx:VBox>
</mx:Application>



@ie.custom_component("cust").item_click(:item_renderer => "Nr2")



I hope this was helpfull, and possible to understand :-)

Do not forget to read the post about RSpec and Stories that I wrote just before this post. Thanks!

Thursday, February 21, 2008

FunFX and RSpec with stories

Hi again

I how now been playing around with RSpec and its story runner (rBehave). Its a fun way to write the tests as stories. This can work out nice with functional tests.

First thing you will need to do is to install RSpec (gem install rspec).

I have created the test towards the initial test application I put out, the one where you can add a product and a product item. A very simple application, but it shows how to work with FunFX in many ways.

When writing there are three main files (when writing for FunFX, addtional class files if you write for traditional Ruby classes) that must be written. And that is the story, a plain text file, the steps, the code behind the story (actual @ie.button("name").click actions) and a file to runn the stories, this file is often called all.rb and is running all tests you define in it.

When I searched the net for tips on how to organize the different files there were not created any best practice, as far as I could see. But when I asked Aslak Hellesøy a collegue and one of the creators of RSpec, he provided me with an example.

If you are to use some kind of ruby classes, they should go into a folder called lib, the step files should go into a folder called steps, and the story should go into a fodler called stories. The all.rb file and a Rake file is located at the root. This was not too spooky :-)

The first thing I did was to write the story. The story is writen as plain text, but in a certain way. There are certain words that are mapped to the steps file, and these are Given, When, And, and Then.

Below is the text that is in the story file. I think this is a nice way to write tests. This way the business side of a project may read the tests too.


Story: Add product

As an user
I want to be able to register and delete products
So that I can have control over the stock

Scenario: Add and delete product
Given the user is logged into the application
When the user click the Add product button
Then the product registration view is visible

Given the user enters Shirt into the product name textbox
And inputs Tennis into the product category textbox
And the user click the Add item button
And the user enters Pro into the item name textbox
And the user enters 9000 into the price textbox
And the user click the Ok button
Then the product registration view is closed

When the user selects Shirt from the datagrid
Then the screen shows the count of 1 item numbers

When the user drags the Shirt item from the datagrid to the trash
Then the datagrid has 0 number of products left

Then close the application


Since this was my first time implementing a rspec story I did not write entire story before I wrote the steps, but after you have written the story or in between as I did you will create the steps that will be taken at each line.

I have created the test towards the initial test application I put out, the one where you can add a product and a product item. A very simple application, but it shows how to work with FunFX in many ways.

Below is the steps file that implement the story file. One thing that you must remember is that the keyword And, is not used in the steps file. It just syas that the step is of the same type as the step above. And as I can see is only used for Given steps.


require 'funfx'

steps_for :product do
Given "the user is logged into the application" do
@ie = Funfx.instance
@ie.start(true)
@ie.speed = 1
@ie.goto("http://funfx.rubyforge.org/Flex/FlexRepeater/FlexRepeater.html", "FlexRepeater")
end

When "the user click the $button_name button" do |button_name|
@ie.button(button_name).click
end

Then "the product registration view is visible" do |balance|
@ie.title_window("Product registration").visible == "true"
end

Given "the user enters $text into the product name textbox" do |text|
@ie.text_area("tName").input(:text => text)
end

Given "inputs $text into the product category textbox" do |text|
@ie.text_area("tCategory").input(:text => text)
end

Given "the user click the $button_name button" do |button_name|
@ie.button(button_name).click
end

Given "the user enters $text into the item name textbox" do |text|
@ie.text_area("tfItemName").input(:text => text)
end

Given "the user enters $text into the price textbox" do |text|
@ie.text_area("tfPrice").input(:text => text)
end


Then "the product registration view is closed" do
@ie.title_window("Product registration") == nil
end

When "the user selects $text from the datagrid" do |text|
@ie.data_grid("dgOffer").select(:item_renderer => text)
end

Then "the screen shows the count of $item_number item numbers" do |item_number|
@ie.label("lNumberItems").text == item_number
end

When "the user drags the $item item from the datagrid to the trash" do |item|
@ie.data_grid("dgOffer").drag_start(:dragged_item => "Shirt")
@ie.box("deleteBox").drag_drop
end

Then "the datagrid has $number number of products left" do |number|
@ie.data_grid("dgOffer").num_rows == number
end

Then "close the application" do
@ie.unload
end
end


As Mark Anderson stated in the FunFX mailinglist, Rspec and stories does not provide a teardown method, the @ie.unload method must be played different. Michael Latta provided me with this solution. Add a step that might be called close the application, that will be added to all stories.

The all.rb file looks like this:


require 'rubygems'
require 'spec/story'
$:.unshift(File.dirname(__FILE__) + '/lib')
require 'steps/product'

with_steps_for :product do
run File.dirname(__FILE__) + '/stories/add_product'
end


Now it is just to run the all.rb file with ruby.

Tip: I have noticed that there are some problems with FunFX's ability to extract properties as text, automationName and so on with the Flex 3. Due to this one of the steps in the rspec example fail. But that is one more brilliant thing about rspec, it does not quit when it fails, it just states that the step failed.

Friday, February 8, 2008

Test application is out

Hi

Due to the lack of a good source to what FunFX is able to to and how to do it, I have created a test application, which is located at http://funfx.rubyforge.org/Flex/FlexObjectTest/FlexObjectTest.html
, the source is found at http://funfx.rubyforge.org/Flex/FlexObjectTest/srcview/.

This application contains many of the available display objects in the Flex language, and all display objects have related FunFX tests at http://funfx.rubyforge.org/FunFXTests/.

I hope this application will make it easier to understand FunFX and also be a knowledge base where we can exchange knowledge on how to test different controls.

I hope everybody can contribute with sample code that is either difficult to test and you have tests written for this code, so that other people can learn. Or if you simply have code that you are not able to test, then others can help test the code.

In this way, FunFX would be able to get better much faster.

The test application and tests are under svn control at Rubyforge, but there is no automatic roll out, so I have also created an email funfx.tests at gmail.com, which could be used to contribute with code or tests.

(Please add the word FunFX in the subject so I can sort the mail efficiently.)

I hope this helps!

Version 0.0.3 is out

Hi

Now a new version is released, The one big change is the support for Safari browser on the OS X, thanks to Neil Curzon for implementing this.

Other changes is that I have changed the way FunFX searches children. In the earlier versions, it used the getAutomationChild method but now I have switched to getChild method. This improves the support for repeaters.

Let me know if something does not work with these changes.

Saturday, December 22, 2007

FormItem problem

Hi!

I just recently got a notice on my last post from Neil that when using FunFX a null pointer exception is thrown when any form item's label in a form is null as shown in the first listing. Neil pointed out that it was also happening when the label was set to an empty string.

<mx:form>
<mx:formitem label="Form Label">
<mx:label id="lLabel"/>
</mx:formitem>
<mx:formitem>
<mx:textinput id="tTextInput"/>
</mx:formitem>
</mx:form>


Neil pointed out that it was something to do with the FunFX and FormItemAutomationImpl interaction. And that one need to set all the form items labels to a non empty string. And that using whitespaces as label affects the layout. He suggested a nice workaround until a fix with the FormItemAutomationImpl is done. And that is to set the label width to 0.

<mx:form>
<mx:formitem label="Form Label">
<mx:label id="lLabel"/>
</mx:formitem>
<mx:formitem label="" labelWidth="0">
<mx:textinput id="tTextInput"/>
</mx:formitem>
</mx:form>


I have created the error for my own, and the reason for the error is as Neil said. The FunFX uses the automation framework from Adobe to create string id of the display objects that it interacts with. And this method in the FormItemAutomationImpl.as as displayed in the following listing. When the label of the formitem is null, there is no check that will prevent the method to call the length method of the label, which might be null.

private function getItemAutomationName(child:IAutomationObject):String
{
//Have cut down the method
result = (label.length != 0 ? label + ":" + child.automationName : child.automationName);
// More in the real method
}

I have not yet been able to check wether it is possible to just add a null pointyer check in the method. But this will probably implie a change in th corresponding method that creates an object from the string id.

I hope this will help people in the same situation as Neil. And I will see if it can be fixed. But so long you will need to do as Neil suggested and set the labelWidth of the form item to zero.

Thanks alot Neil, for this valuable tip and solution!

Tuesday, December 4, 2007

Some problems and solutions when using FunFX

Hi everyone.

These past days I have used some time trying to set up FunFX for the project I am on at work. I have said to my self earlier (because some people have had some problems with custom components) that I would write a post about this. Because I do no know to much about this area, how FunXF actually handles, I have not had the time to do this yet, but I asure you that it will come.

So instead I will use this post to talk about some of the problems and solutions I have had the last few days.

First of all double clicking a row in a datagrid made me a bit frustrated today, because it would not replay the action that I wanted to do. The following line was what I tried to do (the datagrid contained a row with a column that held the value "Person".

@ie.data_grid("name").double_click(:item_renderer => "Person")

After some error checking it seems that you must select the row before doing a double click. The following lines works perfect. I have not yet found a good reason why this behavior or a better solution if people think it should be handled better.

@ie.data_grid("name").select(:item_renderer => "Person")
@ie.data_grid("name").double_click(:item_renderer => "Person")


Anther thing I has some trouble with was accessing elements of an repeater when there is an hierarchy of display objects within the repeater, then the number of the single element I wanted waas multiplied with the number of parents abow. In the following example I would get 4 tLabels and 4 tData.

<mx:repeater id="rep" dataprovider="{}">
<mx:vbox id="vboxen">
<mx:hbox id="hboxen">
<mx:textinput id="tLabel" text="{rep.currentItem.label}"/>
<mx:textinput id="tData" text="{rep.currentItem.data}"/>
</mx:hbox>
</mx:vbox>
</mx:repeater>

I am still testing the solution (and will probably put out a new version of FunFX in the next days), so I am not completely sure that it is the best solution. But I have up until now used the objects automationChildren when try to reach the children, but this seems to multiply children of repeaters and like. So I have switched to used the rgular children instead, and for now it seems to do the trick, but I still have to test that all other displayobjects still are supported.

With the new solution the issue where you had to write the repeater and then the object you wanted is now gone. Now the repeater will not exist as an display obejct. So you write as the last bulk says.

@ie.repeater("rep").text_area("tLabel")[0].input(:text => "Test") # This is now wrong


@ie.text_area("tLabel")[0].input(:text => "Test") # This is correct


But so far I am able to do automatic functional testing of a registration form and to ensure that the calculation response from this registration is correct. Due to low speed these tests are not run too often, but I will try later to include the tests in the nightly build.

Thursday, November 22, 2007

FunFX - How to write tests

The last post talked about howto get started and how to make the Flex application ready for testing. I am sorry about the time it took me to post this sequel.

FunFX is a framework that is composed of two parts, one adapter (the last post talked about this part) that enables the Flex application to be tested, and one Ruby framework that enables you to drive an Internet Explorer instance with the Flex application.

Some people ask me if there is an easy way to implement support for either FireFox or other browsers, and the answer to this is both yes and no. It depends, there are currently only IE that is implemented and the reason for this is IE's support for COM objects. I will explain this implementation in a bit more detail in another post.

This post will talk about the Ruby framework mentioned in the beginning. This Ruby framework is a Ruby gem. This framework creates an instance of a Internet Explorer window. Then with some initializing methods you will direct the IE window to the address of the Flex application.
This is shown in the follwoing table. The first sentance creates an instance of FunFX. When executing the second line with the start method FunFX creates an instance of IE in the background with the help of Win32OLE. The argument tells FunFX if you want the IE to be visible or not.

@ie = Funfx.instance
@ie.start(true)
@ie.speed = 1
@ie.goto("http://localhost/flexapplication.html", "flexapplication")


The last sentance in the initialization is the method that directs the browser to the Flex application and hooks on to the actual Flex object with the help from Win32OLE, and the name of the swf file. If you have an html file named index.html that contains a swf file that is named flexApp.swf and is located at http://localhost/flexapplication/index.html. The

@ie.goto("http://localhost/flexapplication/index.html", "flexApp")


After this initialization, the @ie variable (which is an instance of FunFX) is ready to interact with the Flex application.

The FunFX in itself does not test Flex applications in the sence of asserts, it just enables you to drive an Flex application programmatically and get the state and text of all display objects. To make assertions and create test scripts you will need to use FunFX together with a testing framework of your choice. A couple of examples are RSpec, and Test::Unit. FunFX together with any of these tools make up an automated functional testing tool.

Before I will show you an example of FunFX together with these tools, I will explain in short how to use FunFX in the sence of driving the Flex application programmatically.

A Flex application is built as an hierarchy of different display objects, with the Application object as the root node. With FunFX you can think of @ie the root node of the Application object.

An example of interacting with a display object is shown in the box below.

@ie.button("buttonName").click

This line of code will click the button with id or label named "buttonName". This button can be located anywhere in the Flex hierarchy. In version 0.0.1 FunFX only supported single elements, and then it stopped when it found a object matching this signature. But this was a problem with repeaters, which creates children with the same id's.

So in 0.0.2 it finds all objects that matches that type and name, and delivers them as an array, as shown below:

@ie.button("buttonName")[0].click

To interact with a display obejct you must provide the label or id.

Some flex event needs arguments as when inserting text into a textbox. This is done as follows:

@ie.text_area("price").input(:text => "This is the new text")


@ie.data_grid("gridName").drag_start(:dragged_item => "Name of item")
@ie.data_grid("gridName").drag_drop


There are many more methods. Unfortunately there are no good overview of all the methods and their arguments. But you should look at the AutomationGenericEnv.xml, which describes all the display object types and their events and arguments.

When testing the functionality of an application it does not help being just able to drive the application. It must be possible to extract some information to assert with a testing tool such as RSpec or Test::Unit.

You can get information about alot of thing, such as visibility, text, rows in a datagrid, enabled etc. These are called properties and are also described in the AutomationGenericEnv.xml.

The following displays some examples of extracting data. It is possible to extract the selected index of an data grid, making it possible to extract information about that grid line with the tabular_data property. This creates a comma separated string containing the row information. If there are images in the row, the name of the images will be displayed.

pos = @ie.data_grid("gridName").selected_index
@ie.data_grid("gridName").tabular_data(:start => pos)

@ie.label("labelName").text
@ie.application("applicationName").current_state

I have not yet tried how this works with the new upgraded grid in Flex 3.

The following is an example test that tests an flex application that adds a product to a datagrid with the use of an popup window. This test is test nr 1 at the bottom of this post.

require 'test/unit'
require 'funfx'

class TestProductOne <>

def setup
@ie = Funfx.instance
@ie.start(true)
@ie.speed = 1
@ie.goto("http://funfx.rubyforge.org/Flex/FlexRepeater.html", "FlexRepeater")
end

def teardown
@ie.unload
end

def test_insert_product

assert_equal(0, @ie.data_grid("dgOffer").num_rows)

add_product("Shirt", "Tennis")
@ie.button("bOk").click

assert_equal(1, @ie.data_grid("dgOffer").num_rows)

end

def add_product(name, category)
@ie.button("bAddProduct").click

@ie.text_area("tName").input(:text => name)
@ie.text_area("tCategory").input(:text => category)
end
end


This post is a bit messy, but i tries to explain some of the basic elements. The following post will try to explain other concepts in more detail.

I have put out an test application that set up for testing with a couple of simple actions to perform at TestApplication. At the following links I have also written a couple of tests that test this application. Please try them out and alter them to see if you get it going (the source code of the view files are available at view source, or this address source-code).
I hope this test application and test scripts help you to get going.