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)

Overview

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.

Terms

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 : https://addons.mozilla.org/en-US/firefox/addon/2079

selenium-server.jar and selenium-java-client-driver.jar : http://seleniumhq.org/download/

* 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("http://www.google.com/", "*firefox");
      // We instantiate and start the browser
   }

   public void testNew() throws Exception {
      selenium.open("/");
      selenium.type("q", "selenium rc");
      selenium.click("btnG");
      selenium.waitForPageToLoad("30000");
      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: http://www.codediesel.com/php/selenium-ide-tutorial-part-1/ )

JUnit Tests (Flex app)

* Prerequisites:

+ FireFox with Selenium IDE : (FF 3.6 requires 1.0.4+ http://release.seleniumhq.org/selenium-ide/ )

selenium-server.jarselenium-java-client-driver.jar, and flashselenium-java-client-extension.jar : http://seleniumhq.org/download/

+ JRE, web application server and your Flex app.
+ Selenium Flex API : http://code.google.com/p/sfapi/
(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 :

<dependencies>
   <!-- Core selenium java client driver -->
   <dependency>
      <groupId>org.seleniumhq.selenium.client-drivers</groupId>
      <artifactId>selenium-java-client-driver</artifactId>
      <version>1.0.1</version>
      <scope>test</scope>
   </dependency>
   <!-- Flex extension jar file -->
   <dependency>
      <groupId>com.thoughtworks.selenium</groupId>
      <artifactId>flashselenium-java-client-extension</artifactId>
      <version>1.0</version>
   </dependency>
   <!-- ... more in dependencies ... -->
</dependencies>

– add plugins :

<build>
   <testSourceDirectory>src/test/java</testSourceDirectory>
   <plugins>
      <plugin>
         <groupId>org.mortbay.jetty</groupId>
         <artifactId>maven-jetty-plugin</artifactId>
      </plugin>

      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>selenium-maven-plugin</artifactId>
         <executions>
            <execution>
               <id>kick-off</id>
               <phase>pre-integration-test</phase>
               <goals>
                  <goal>start-server</goal>
               </goals>
               <configuration>
                  <background>true</background>
                  <logOutput>true</logOutput>
                  <multiWindow>false</multiWindow>
               </configuration>
            </execution>
            <execution>
               <id>farewell</id>
               <phase>post-integration-test</phase>
               <goals>
                  <goal>stop-server</goal>
               </goals>
            </execution>
         </executions>
      </plugin>
      <!-- ... more in plugins ... -->
   </plugins>
   <!-- ... more in build ... -->
</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: http://sfapi.googlecode.com/files/sfapi-sample.zip

  • 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 test.properties 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

   @Before
   public void setUpPageDriver() {
      pageDriver = new SelbenPageDriver(getFlashApp());
      pageDriver.navigateToPage();
   }

   @Test
   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.ensureDataGridPopulated("dataGrid");
      pageDriver.ensureDataGridRowCount("dataGrid", 3);

      pageDriver.selectTab("Fruit");
      pageDriver.ensureWidgetEnabled("Cox");
      pageDriver.clickButton("Cox");
      pageDriver.ensureAlertVisibility(true);
      pageDriver.closeAlert("OK");
      pageDriver.ensureAlertVisibility(false);
   }

}
* 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 ( http://code.google.com/p/fxspy/ ) to identify the “id” of Flex component easier.

. . .

./.

About DucQuoc.wordpress.com

A coder, brother, and player at times...
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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s