Tuesday, June 9, 2015

Testing Oracle ADF applications using Selenium

Selenium is a popular web based automation tool for testing web applications. It provides the capability to record and play. It has Selenium IDE component that can be installed as a plugin in Mozilla Firefox and enables developer to test their web application through the selenium. The selenium IDE is currently available only on Firefox.

In this blog post, I will be giving details about
      How to install and configure selenium IDE
      How to record script using Selenium IDE (ADF application)
      How to convert/export the recorded script to Java JUnit test case.
      How to do continuous testing with recorded scripts

Download the Selenium IDE:

1.      Selenium IDE can be downloaded from seleniumhq website.
a)      Open the below link in Firefox Browser.


b)      click on 2.9.0 release link to install formatters

c)        The link opens a popup window on left side top corner. Click on allow button.


d)      Another popup window “Software Installation” will open click on “Install Now” button.


e)      Verify all the formats are installed:
         i)  Open Selenium IDE (Firefox browser à Tools à Selenium IDE)
        ii)  Click on Options à Format
      


Configuring Selenium IDE:

Open Selenium IDE options (Selenium IDE à Options à Options)

                  1.       Turn off auto recording:
    ·         Uncheck “Start recording immediately on open”
                  2.       Enable experimental features
    ·         Check “Enable experimental features”

Recording Selenium Script:


         1.        Open Selenium IDE and click on Record button.
         2.       Selenium IDE will automatically capture all the user interactions and record it as a script                     (consists of commands, targets, and value)
         3.       After completing the recording click on stop button.
         4.       Save the recording

       Note:
             ·         Selenium scripts can be recorded only from Firefox Browser. 
             ·         Only for ADF applications:
   The script captures extra command “Selected Window” for every user interaction. Need to      remove all the “select window” commands from script before running.

Running the recording script

      1.       For Oracle ADF applications, it is recommended to run the scripts using lowest execution time.
      








        2.       Click on “Play current test case” button  for running selected script. To run all the scripts                click on “Play entire test suite” button 

Running Selenium Test Cases using Maven

  1. Create new project with maven structure in existing web application.
Example:
                my-portal
my-portal-view  (ADF Application – ViewController Project)
src
main
java
resources
test
java
resources
my-portal-test  (New Test Project for Testing ADF application using Selenium)
src
main
java
resources
test
java
resources

  1. Create a new POM file for the test project with below dependencies. Do not add the new pom as a module dependency in the parent pom of the project. This pom file must run only after the server restart or after code got deployed to the application server.
                                <dependency>
                                                <groupId>org.seleniumhq.selenium</groupId>
                                                <artifactId>selenium-server</artifactId>
                                                <version>2.46.0</version>
                                                <scope>provided</scope>
                                </dependency>
                                <dependency>
                                                <groupId>org.seleniumhq.selenium</groupId>
                                                <artifactId>selenium-java</artifactId>
                                                <version>2.46.0</version>
</dependency>
                                <dependency>
                                                <groupId>junit</groupId>
                                                <artifactId>junit</artifactId>
                                                <version>4.12</version>
                                                <scope>test</scope>
                                </dependency>                              


Note:
·         In Jenkins, a new job must be created to run the above created pom file and it must be added as a downstream job for the server restart job. This way, after restart this job will be triggered to run all the test cases.

Creating JUnit Test case

       1.   Select script in IDE and click on FileàExport Test case as à Java /JUnit4/WebDriver



        2.   Select the Folder location (project name à src/main/test/”package-name” )  and click on Save           button
      
       Note: There is a bug when exporting selenium script as a java junit test case, it doesn’t change                       the package name. You need to manually open each class you export and change the                             package name.

        3.   Exported Java junit case also adds an extra “/” to the baseURL. Remove it from the end of the             path.
      
      Example:
               @Before
               public void setUp() throws Exception {
                  driver = new FirefoxDriver();
                  baseUrl = "http://localhost:7001/";
                  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
              }
                
   If you notice, the baseURL is hardcoded with the environment where you have recorded the script.      You need to specify a property so that the value can be passed as environment property for running    the same script in different environments. Below sections explains on how it can be done.

Configuring environment variable for running tests in different environments:

        1.       In JDeveloper, Right click on JUnit class and click on run button.
        2.       A new firefox browser window will open and runs the script
Note: In order for the tests to run on Build machine, make below changes to the code
a)      Change the base URL to get the value from system property
Example:
  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl =  System.getProperty("env.url");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

b)      Running the build from local using maven, use below command:
mvn clean test –Denv.url=http://localhost:7001 (Developer machine)
mvn clean test –Denv.url=https://dev.mycompany.com (Dev Environment)

Build Machine (Jenkins Job)
mvn clean test –Denv.url=https://dev.mycompany.com

c)       For running Junit tests from JDevloper, please specify env property. Steps to specify property value in JDeveloper:
Right click on the project à Project Properties
Select Run/Debug/Profile à select Default à click on Edit button
enter “–Denv.url=http://localhsot:7001” under Java_Options.











No comments:

Post a Comment