Wednesday, February 16, 2011

Webdriver and flex-pilot together as flexdriver for flex automation


Good news for all those who wants to automate their flex apps using the selenium. 
If you are in love with webdriver aka selenium-2 flex pilot is the best companion you can get towards the goal of automating your flex apps.

Here is the sample code which implements a flexdriver which inturn use the webdriver and invoke the flex driver api using the javascript executor.

Before you see the flexdriver implementation and sample code which use the flex driver with the help of the sample application published on the github as part of the flex-pilot project Here is facts which you should do before thinking bat using the flexdriver.

https://github.com/mde/flex-pilot/wiki/setup-from-scratch-notes has the details if you start from scratch.
Or get started with flex-pilot here https://github.com/mde/flex-pilot/wiki/getting-started Or here is the copy of the content 

  • right click, save as for both those swfs in the download page: flex-pilot downloads
  • put them in the same directory as your application swf
  • add the lines outlined here to your application: flash-testing-setup and rebuild it
  • uninstall selenium ide, and get the latest here: selenium-ide-1.0.7.xpi
  • download the latest flex-pilot-x extension and install it from here: flex-pilot-x/flex-pilot-x.xpi
  • open firefox to the URL of your flex application, open the selenium IDE, click record and operate your flex application
  • verify that actions are appearing in the selenium IDE as you navigate around the flex application.
  • check out the API to see all of the available functionality


I have used the webdriver and implemented the "type" and "select" commands  from Flash Controller API using the javascript executor of webdriver.
the sample flexdriver class looks like this.


package com.selenium.flex;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

public class FlexWebDriver{
private  WebDriver webDriver;
private  String flashObjectId;

public FlexWebDriver(WebDriver webDriver,String flashObjectId) {
this.webDriver = webDriver;
this.flashObjectId = flashObjectId;
}
public void type(String locator, String value) {
((JavascriptExecutor)  webDriver).executeScript("document.getElementById('" + flashObjectId + "').fp_type({" + locator +", 'text':'"+ value +"'})");
}
public void select(String locator, String value) {
((JavascriptExecutor)  webDriver).executeScript("document.getElementById('" + flashObjectId + "').fp_select({" + locator +", 'label':'"+ value +"'})");
}

}


and the sample test class which uses the flexdriver mthos like "type" and "select" is quoted below

package com.selenium.flex.test;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.testng.annotations.*;

public class flexTest {

WebDriver driver;
FlexWebDriver flexDriver;

@BeforeMethod
public void startSelenium() {
driver = new FirefoxDriver();
flexDriver = new FlexWebDriver(driver"testApp");
}

@AfterMethod
public void stopSelenium() {
driver.close();

}

@Test
public void testFlex() throws InterruptedException {
driver.get("http://localhost/fp_demo");
Thread.sleep(10000); // Wait till flash loads completely.
flexDriver.type("'name':'UITextField18'""Flex Pilot Rocks.");
flexDriver.type("'name':'UITextField23'""Success.");
flexDriver.select("'name':'comboTest'""Alex");
}

}


2 comments:

  1. You made my day!
    Great and useful info.

    ReplyDelete
  2. Hi,

    I have done tha same as below. But both methods are not working.

    public void type(String locator, String value) {
    ((JavascriptExecutor) webDriver).executeScript("document.getElementById('" + flashObjectId + "').fp_type({" + locator +", 'text':'"+ value +"'})");
    }
    public void select(String locator, String value) {
    ((JavascriptExecutor) webDriver).executeScript("document.getElementById('" + flashObjectId + "').fp_select({" + locator +", 'label':'"+ value +"'})");
    }

    ITs fine woth Click only as below.

    public String click(final String objectId, final String optionalButtonLabel) {

    return call("doFlexClick", objectId, optionalButtonLabel);
    }
    Please help to fix this in select box and tyep operation using webdriver in Flash.

    Thanks,
    Gopal

    ReplyDelete