Selenium Flex JUnit

(bài viết cũ trong internal wiki cho Selenium 1.x – hiện nay đã có Selenium 2.0 với WebDriver thú vị hơn)


Selenium is a suite of tools to automate web app testing across many platforms. It is a GUI based automation tool, usually used for the acceptance testing.


Selenium IDE (IDE) : can be used only in FireFox browser (originally an add-on for FireFox). User can record the actions and can edit and debug the tests (in Selenese HTML table format). It can be used to identify IDsname andXPath of objects. Only one test at a time.

Selenium Remote Control (RC) : a test tool that allows user to write automated web application UI tests in few programming languages against any HTTP website using any mainstream JavaScript-enabled browser.

User can write the tests in Java, DotNet, Perl, Ruby, Python and PHP. Also it supports few testing frameworks.

Selenium Core (CORE) : the original Javascript-based testing system.

It is the engine of both, Selenium IDE and Selenium RC (driven mode), but it also can be deployed on the desired application server.

User can record the tests using Selenium IDE and can use the same tests to run in other browsers with minimal modifications.

Selenium Grid : Using this, user can run multiple instances of Selenium Remote Control in parallel, which alows easily to run multiple tests in parallel, on multiple machines, in an heterogeneous environment.

JUnit Tests (normal web app)

* Prerequisites:

+ FireFox with Selenium IDE :

selenium-server.jar and selenium-java-client-driver.jar :

* Configuration:

+ Get the web-app deployed and running on respective web server.

+ Start remote control server (default port is 4444) : java -jar selenium-server.jar

* Sample code:
package com.example.tests;

import com.thoughtworks.selenium.*;

import java.util.regex.Pattern;

public class NewTest extends SeleneseTestCase {

   public void setUp() throws Exception {
      setUp("", "*firefox");
      // We instantiate and start the browser

   public void testNew() throws Exception {"/");
      selenium.type("q", "selenium rc");"btnG");
      assertTrue(selenium.isTextPresent("Results * for selenium rc"));
      // These are the real test steps
* Launching the tests:

+ Make sure you have the Selenium RC server running .

+ Make sure the web-app under your test are running normally (i.e. you can access its pages from normal browser).

+ Run your Selenium test class as normal JUnit test (Eclipse: right click -> Run as JUnit Test ).

* Tips:

– Use FireFox extensions (FireBug and XPath Checker) to identify the “id” and “XPath” of HTML elements easier.

– Selenium IDE can generate some sample JUnit code from recorded tests (JUnit 3.x syntax). 

HTML Tests

(see this tutorial: )

JUnit Tests (Flex app)

* Prerequisites:

+ FireFox with Selenium IDE : (FF 3.6 requires 1.0.4+ )

selenium-server.jarselenium-java-client-driver.jar, and flashselenium-java-client-extension.jar :

+ JRE, web application server and your Flex app.
+ Selenium Flex API :
(sfapi.swc embeded in your FLex app, and respective Java classes for calling them from JUnit)

* Configuration:

+ Using Maven : edit POM file of your project as below :

– add dependencies :

   <!-- Core selenium java client driver -->
   <!-- Flex extension jar file -->
   <!-- ... more in dependencies ... -->

– add plugins :


      <!-- ... more in plugins ... -->
   <!-- ... more in build ... -->

+ Selenium Flex API :

– In the options window for the Selenium IDE, add the file }}{{{}{}user-extensions.js (which is packaged with our API) to the Selenium Core extensions. This file allows Selenium to call the API (version prior to 0.2.5) .

– Copy the file sfapi.swc into the ‘libs’ folder of your Flex application. Then add the following to FlexBuilder the Additional compiler arguments-include-libraries “..\libs\sfapi.swc” (for Windows) or -include-libraries “../libs/sfapi.swc” (for Mac or Linux) . Make sure the linkType of RSL is “Merged into code” (1) . 

+ Get the web-app running: You can use SFAPI sample projects:

  • Import project SelFlexDemoApp to Flex Builder, set its outputFolder to the deployed folder on web server (Tomcat).
  • Import project SelFlexDemoTest to your Java IDE (Eclipse), add neccesary dependencies and edit file to suit your environment

(Example: selenium.browserUrl=http://localhost:8080/my-web-app/selben/selben.html , selenium.serverPort=4444 )

  • Build and deploy web-app successfully into your web server (Tomcat) so that users can access it via browsers.

+ Start remote control server (default port is 4444) :
java -jar selenium-server.jar
(or mvn selenium:start-server or ant selenium-start )

* Sample code:

+ Can be extracted from the sample project .

+ Some thing like this:

import net.sourceforge.seleniumflexapi.AcceptanceTestBase;

import org.junit.Before;
import org.junit.Test;

public class SelbenAcceptanceTest extends AcceptanceTestBase {

   private static final String CHECKED = "checked";

   private static final String UNCHECKED = "not checked";

   private SelbenPageDriver pageDriver; //extends AbstractPageDriver

   public void setUpPageDriver() {
      pageDriver = new SelbenPageDriver(getFlashApp());

   public void testSomeComponents() {
      pageDriver.setCheckBoxState("checkBox", true);
      pageDriver.ensureCheckBoxState("checkBox", true);
      pageDriver.ensureWidgetHasText("checkBox", CHECKED);

      pageDriver.setCheckBoxState("checkBox", false);
      pageDriver.ensureCheckBoxState("checkBox", false);
      pageDriver.ensureWidgetHasText("checkBox", UNCHECKED);

      pageDriver.ensureWidgetVisibility("dataGrid", true);
      pageDriver.ensureDataGridRowCount("dataGrid", 3);


* Launching the tests:

+ Make sure you have the Selenium server running ( java -jar selenium-server.jar or mvn selenium:start-server or ant selenium-start ).

+ Make sure you have application server (Tomcat) started, and your web-app deployed and running on it.

+ Run your Selenium test class as normal JUnit test (Eclipse: right click -> Run as JUnit Test ).

* Tips:

+ Use FlexSpy ( ) to identify the “id” of Flex component easier.

. . .



A coder, husband and brother...
This entry was posted in Coding, Marketing. Bookmark the permalink.

4 Responses to Selenium Flex JUnit

  1. Khoa says:

    Hello Duc mập

  2. Pingback: Software tester objectives | DucQuoc's Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s