Driving a Flex Application via Selenium 2 WebDriver

Posted by | November 22, 2010 | Programming | 15 Comments

A while ago my colleague Julia posted an article on acceptance testing a Flex application using Selenium. Now that Selenium 2 has been released I wondered how easy it would be to achieve the same functionality with WebDriver?

As it turns out it isn’t all that difficult. The interaction with JavaScript in the Browser has changed but that aside the changes required to make FlexSelenium operate with WebDriver are straightforward.

The basic principle still remains that the ActionScript extensions for FlexSelenium need to be compiled into your Flex application to expose access to widgets via JavaScript. The changes required are in the way the Java portion of the framework interacts with the browser JavaScript engine.

The WebDriver framework provides an interface that concrete implementations for each browser can implement in order to provide access to the browser’s JavaScript engine. Both the Firefox and Chrome drivers provide such implementations. The code for FlexWebDriver below uses this interface to execute a piece of anonymous JavaScript to call the exposed functions from FlexSelenium.

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
publicclass FlexWebDriver {    privatefinal WebDriver webDriver;privatefinal String flashObjectId;    public FlexWebDriver(final WebDriver webDriver, final String flashObjectId) {        this.webDriver = webDriver;
        this.flashObjectId = flashObjectId;
    }

    public String click(final String objectId, final String optionalButtonLabel) {        return call("doFlexClick", objectId, optionalButtonLabel);
   }

    public String click(final String objectId) {
         return click(objectId, "");
    }

//... Omitted for clarity ...

    private String call(final String functionName, final String... args) {final Object result =
            ((JavascriptExecutor)webDriver).executeScript(
                 makeJsFunction(functionName, args),
                 new Object[0]);

         return result != null ? result.toString() : null;
    }

    private String makeJsFunction(final String functionName, final String... args) {
         final StringBuffer functionArgs = new StringBuffer();

        if (args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                if (i > 0) {
                    functionArgs.append(",");
            }
                functionArgs.append(String.format("'%1$s'", args[i]));
         }
        }
        return String.format(
            "return document.%1$s.%2$s(%3$s);",
            flashObjectId,
            functionName,
            functionArgs);
    }
}

The main changes are to use org.openqa.selenium.WebDriver instead of com.thoughtworks.selenium.Selenium, and to build the correct JavaScript function to execute via the JavascriptExecutor interface.

A very trivial example of using this to drive a Flex application:

final FirefoxDriver driver = new FirefoxDriver();

final FlexWebDriver flexDriver = new FlexWebDriver(driver, “my-application”);

 

driver.get(“http://localhost:8080/application”);

 

// Wait for the application to load

 

Assert.assertEquals(“true”, flexDriver.click(“aButton”));

I’ve tested this with Selenium 2.0a7 using the Firefox and Google Chrome browser drivers running under Ubuntu 10.10.

Enjoy.

15 Comments

  • Avatar for John Eccleston William Zhang says:

    Is there any solution for the swfupload that selenium can choose the files?

  • Avatar for John Eccleston emilia says:

    Hello, i’ve been reading the post, and for some reason when i try to create the object ”flexDriver” the FlexWebDriver class doesn’t appear on my IDE (i am using NetBeans and Java)…do you have any idea why this my hapend?

  • Avatar for John Eccleston lakshmi says:

    Hi

    we are trying to do same thing. This is a new area to us . can you help us with step by step ,on setting up the environment etc

    your help is much appreciated.

  • Avatar for John Eccleston emilia says:

    Hi, i was wondering if you have any idea on a possible solution to do customize Flex components testing…somehow every time i run the flex application on my browser, i can see the buttons ids but i cant see the ids of the customize components…any ideas?

  • Avatar for John Eccleston Gregory Kleiner says:

    I have followed the instructions here and so far I am able to interact with all buttons on the Flash Flex app. I was wondering if you had any updates that will allow selenium to access selectable text? Any feedback will be greatly appreciated.

    • Avatar for John Eccleston murali says:

      Hello Klenier,

      I am finding an issue with Automating Flex objects in web page using Web Driver

      Can you please help me with how to handle the flex objects using Web Driver?

      thanks
      murali

  • Avatar for John Eccleston Rostislav Matl says:

    Nice article. The only thing I am missing is the information where I can get the “ActionScript extensions for FlexSelenium” you mentioned.

  • Avatar for John Eccleston Panchdev says:

    I dont have any idea regarding how to know variaous flex component names and ids so that I can call them from my script.

    Please help me out if you have some experience in that.

    • Avatar for John Eccleston Nathan says:

      Hi Ramon,Thanks for the comment and kind fedceabk. Glad to hear your hard work paid off.Have you tried using different browsers when signing up to Udemy.com?I have seen the bug with their input fields, when I use Opera, but it doesn’t prevent me signing in or registering.You could also use a facebook account if you have one.If that doesn’t work then you might have to contact udemy.com directlyAlan

  • Avatar for John Eccleston PN says:

    Awesome work!! Worked like a charm. Really appreciate the work here guys. Thanks

  • Avatar for John Eccleston Florian says:

    Is there any chance to get the complete sources?

  • Avatar for John Eccleston Dana Waterman says:

    Thanks for this article. I got it to work, but can’t find documentation on the different calls like “doFlexClick.” I would like to know how to sendkeys or check if an object is visible or enabled.

  • Avatar for John Eccleston Dana Waterman says:

    Thanks for this info, I think it will for me, but I’m having an issue with one of our company flash webapps. I created a sample flash project (Flash Builder 4.1, Flex sdk 4.6) with an app name of “testApp” and all works well. Our company flash app was developed with Flash Builder 4.6, Flex sdk 4.6 and the app name is “Main” So I inserted Main as the flashObjectId like the “my-application” example here: final FlexWebDriver flexDriver = new FlexWebDriver(driver, “my-application”);

    So my assignment would be final FlexWebDriver flexDriver = new FlexWebDriver(driver, “Main”);

    Am I doing something wrong?

  • Avatar for John Eccleston himanshu says:

    thanks for info.
    Could you tell me how to write function for doFlexSetFocus and to click on the basis of tooltip?

Leave a Reply

Your email address will not be published.

Get in touch to see how Black Pepper can help your business Contact Us