SelfTechy

A portal for self learning

java program


   1:  package SugarTests;
   2:   
   3:  import com.thoughtworks.selenium.*;
   4:  import org.junit.AfterClass;
   5:  import org.junit.BeforeClass;
   6:  import org.junit.Test;
   7:   
   8:  public class SugarTestScripts extends SeleneseTestCase{
   9:      
  10:      @BeforeClass
  11:      public static void setUpBeforeClass() throws Exception {
  12:          DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://127.0.0.1/");
  13:          selenium.start();
  14:      }
  15:   
  16:      @Test
  17:      public void testSugarLogin() throws Exception {
  18:          selenium.open("http://127.0.0.1/sugarcrm/index.php?action=Login&module=Users");
  19:          selenium.type("user_name", "admin");
  20:          selenium.type("user_password", "admin123");
  21:          selenium.click("login_button");
  22:          selenium.waitForPageToLoad("30000");
  23:          verifyTrue(selenium.isTextPresent("Home"));
  24:          selenium.click("link=Log Out");
  25:          selenium.waitForPageToLoad("30000");
  26:      }
  27:      
  28:      @AfterClass
  29:      public static void tearDownAfterClass() throws Exception {
  30:          DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://127.0.0.1/");
  31:          selenium.stop();
  32:      }
  33:   
  34:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Selenium – How Ant is used with JUnit 4 to generate reports?


Apache Ant is a build tool that uses build.xml file which contains several Ant Tasks & Targets necessary for the build process.  I am not going to explain Ant in detail here.  For more details you can visit Javaworld or Apache Ant user manual

We will use the same Selenium tests whatever we have created in the previous post () and create build.xml needed to execute the tests and generate reports.

Pre-requisites:

  1. Apache Ant has to be downloaded and installed
    • Download Ant from http://ant.apache.org/bindownload.cgi and then click on .zip archive (.zip is used for windows installation) or click directly on “Ant Download
    • Create “Ant” directory in C: drive and unzip the downloaded “.zip” into the “C:\Ant
    • Add directory path to system “Path” –>
      • Go to Desktop
      • Right Click My Computer icon
      • mycomputer_properties
      • Click Properties
      • Click Advanced tab
      • Click Environment Variables button
      • Double Click “Path” variable in the list and the “C:\Ant\bin” (or the exact path of Ant\bin in your system)
      • Click OK and Restart the system (without restarting the system –> class path will not be considered)
      • addClassPath
      • Open command prompt (Click start –> Run –> cmd & Enter)
      • Type “ant” and press Enter –> it should throw error “Build failed” as shown below
      • runAntCommand
  2. As described in the previous there should be a folder structure as shown below (Drive letter & path should be changed according to the folder structure in the system)
    • C:\Eclipse-Workspace\SeleniumTests\SugarCRMJunitTests (SugarCRMJunitTests is the Java project created)
    • Create “lib” folder under this folder structure
    • Copy “selenium-java-client-driver.jar”, “junit-4.8.1.jar”,”ant-junit.jar” into the “lib” folder (Go to C:\Ant folder\lib folder and locate ant-junit.jar & junit jar file needs to be downloaded from the junit website
    • Also create a “build” folder under the folder structure described in the first step
    • Now create a build.xml file and copy the code exactly given below and save, then copy the file into build folder

<?xml version=”1.0″ encoding=”UTF-8″?>
<project name=”test” basedir=”.”>

    <property name=”RELEASE_ROOT” value=”..” />
    <property name=”SRC” value=”${RELEASE_ROOT}/src” />
    <property name=”LIB” value=”${RELEASE_ROOT}/lib” />
    <property name=”BIN” value=”${RELEASE_ROOT}/bin1″ />
    <property name=”REPORT” value=”${RELEASE_ROOT}/report” />
    <path id=”test.classpath”>
        <pathelement location=”${BIN}” />
        <fileset dir=”${LIB}”>
            <include name=”**/*.jar” />
        </fileset>
    </path>
    <target name=”init”>
        <delete dir=”${BIN}” />
        <mkdir dir=”${BIN}” />
    </target>
    <target name=”compile” depends=”init”>
        <javac source=”1.6″ srcdir=”${SRC}” fork=”true” destdir=”${BIN}” >
            <classpath>
                <pathelement path=”${BIN}”>
                </pathelement>
                <fileset dir=”${LIB}”>
                    <include name=”**/*.jar” />
                </fileset>
            </classpath>
        </javac>

    </target>
    <target name=”run-single-test” depends=”compile”>
        <delete dir=”${REPORT}” />
        <mkdir dir=”${REPORT}” />
            <mkdir dir=”${REPORT}/xml” />
        <junit printsummary=”yes” haltonfailure=”no”>
            <classpath>
                <pathelement location=”${BIN}” />
                <fileset dir=”${LIB}”>
                    <include name=”**/*.jar” />
                </fileset>
            </classpath>

            <test name=”SugarTests.SugarTestScripts” haltonfailure=”no” todir=”${REPORT}/xml” outfile=”TEST-result”>
                <formatter type=”xml” />
            </test>
        </junit>
        <junitreport todir=”${REPORT}”>
            <fileset dir=”${REPORT}/xml”>
                <include name=”TEST*.xml” />
            </fileset>
            <report format=”frames” todir=”${REPORT}/html” />
        </junitreport>

    </target>

</project>

  1. Start the Selenium Server
  2. Go to the command prompt and navigate to build folder (in my example it is –> C:\Eclipse-Workspace\SeleniumTests\SugarCRMJunitTests\build)
  3. Type the command as “ant run-single-test” and press the Enter key
  4. Now, execution of the above command opens up the browser and the application and runs the test
  5. We can see the execution as shown in the below figure
  6. runAntBuild
  7. After the execution, there is a new folder “report” is created under Java project (SugarCRMJunitTests)
  8. Open report folder –> open HTML folder –> double click “overview-summary” file that should be as shown below
  9. TestResult
  10. Ant has also created XML report.  We can open XML folder and view the results

I hope this will help the people who are searching for “how to generate report with ant” with JUnit 4

Running Selenium Tests with JUnit 4


Step 1: Create a new project Java SugarCRMJunitTests in Eclipse as shown in the below figure.

CreateNewJavaProject

Step 2: Right Click newly created Java Project (SugarCRMJunitTests) –> New –> Package –> Give name “SugarTests”

Step 3: Right Click SugarTests package –> New –> JUnit Test Case (Select checkboxes SetupBeforeClass, tearDownAfterClass)    –> Give the name as “SugarTestScripts”

createJunitTestCase

Step 4: Record “SugarCRM” Login test with Selenium IDE and export that to JUnit 4 format and copy the code into Eclipse JUnit Test Case.  Code should be as shown below:

package SugarTests;

import com.thoughtworks.selenium.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class SugarTestScripts extends SeleneseTestCase{
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        DefaultSelenium selenium = new DefaultSelenium(“localhost”, 4444, “*firefox”, “http://127.0.0.1/”);
        selenium.start();
    }

    @Test
    public void testSugarLogin() throws Exception {
        selenium.open(“http://127.0.0.1/sugarcrm/index.php?action=Login&module=Users”);
        selenium.type(“user_name”, “admin”);
        selenium.type(“user_password”, “admin123”);
        selenium.click(“login_button”);
        selenium.waitForPageToLoad(“30000”);
        verifyTrue(selenium.isTextPresent(“Home”));
        selenium.click(“link=Log Out”);
        selenium.waitForPageToLoad(“30000”);
    }
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        DefaultSelenium selenium = new DefaultSelenium(“localhost”, 4444, “*firefox”, “http://127.0.0.1/”);
        selenium.stop();
    }

}

Now try to run the JUnit Test Case:

  1. Run Selenium Server by double clicking the Selenium-server.bat file.  Selenium server should be running in the command prompt.
  2. Right click SugarTestScripts.java –> Run As –> JUnit Test
  3. This should open the browser and open the SugarCRM login screen and login.  Then logout.
  4. Browser should be closed

But execution does not generate any results since we have not either written any code to display the results or configured any other tool to generate report.

In the next post we will discuss about Ant and how it helps in generating nice HTML / XML reports

Execute Selenium Test Using Eclipse


We already have configured Selenium Client Driver with Eclipse.  Now, copy the Selenium server into the SeleniumTests folder inside Eclipse-Workspace folder and then create “Selenium-server.bat file with the following code in it.  Below code should not be copied and pasted into the notepad as “-” (hyphen) is getting saved as a special character and does not get executed when you double click the .bat file.

cd F:\Eclipse-Workspace\SeleniumTests
java -jar selenium-server.jar –multiWindow

Type the above code into a text file (e.g. open notepad –> copy the above code) and then save it as Selenium-server.bat

Double Click and run the Selenium-server.bat.  Selenium server will start running in the command prompt.  Following figure shows the Selenium server execution.

executeSeleniumServer

Open Eclipse and create a Java Class as described below:

  1. Create a Java Project (e.g. SugarCRMTests – this already should have been created as it is described in the previous post)
  2. Under src –> create new package (Right click on src –> click package –> Give a name –> SugarCRMTests – this name can be anything as per your choice)
  3. Create new Java Class (Right click Package –> Click New –> Click Class –> give a name preferably SugarCRM)
  4. Copy and paste the below code into SugarCRM.java file and save (Overwrite the self-generated code when SugarCRM.java is created)

package sugarCRMTests;
import com.thoughtworks.selenium.*;

public class SugarCRM {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DefaultSelenium selenium = new DefaultSelenium(“localhost”,4444,”*chrome”,”http://www.google.com”);
selenium.start();
selenium.windowMaximize();
SugarLogin(selenium);
SugarLogout(selenium);
}
public static void SugarLogin(DefaultSelenium selenium){
selenium.open(“http://127.0.0.1/sugarcrm/index.php?action=Login&module=Users”);
selenium.type(“user_name”, “admin”);
selenium.type(“user_password”, “admin123”);
selenium.click(“login_button”);
selenium.waitForPageToLoad(“30000”);
selenium.isTextPresent(“Home”);
selenium.waitForPageToLoad(“30000”);
}
public static void SugarLogout(DefaultSelenium selenium){
selenium.click(“link=Log Out”);
}

}

Above code is recorded using Selenium IDE and then exported into JUnit format as explained in the earlier post.

Click on Run menu –> Run As –> Java Application as shown in the following figure.

executeSeleniumTest

Execution of the above Selenium Test will open up two more firefox browser and starts the execution.

Note: Please provide me feedback through your valuable comments

Selenium – Setting up Selenium & Eclipse


Now its time to learn how to configure Selenium RC client driver with Eclipse IDE and how to run Selenium RC server instance.  We will try to understand this step by step.

Note: Download JDK ( from the Oracle / Sun website and install prior to installing Eclipse)

Download Eclipse

  1. Go to URL – http://www.eclipse.org/downloads/
  2. Select Eclipse IDE for Java Developers (Click on Windows 32 bit platform)
  3. Click on OK button and save to a local drive (i.e. C: or D:, etc)
  4. Unzip the downloaded zip file and rename that to Eclipse
  5. Create one more folder “Eclipse-Workspace” (i.e. C:\Eclipse-Workspace)in the same drive where Eclipse is unzipped and renamed.
  6. Create Eclipse desktop shortcut (go to C:\Eclipse folder –> right click Eclipse.exe and then click on “desktop create shortcut”) as demonstrated in the below pictures.

Eclipse - download

create Eclipse shortcut

  1. Now we need to create a workspace folder –> C:\Eclipse-Workspace\SeleniumTests
  2. Double click on “Eclipse shortcut on Desktop”
  3. This opens the Eclipse
  4. Close Eclipse welcome screen
  5. Click File menu –> Switch Worspace –> other
  6. Now Select the C:\Eclipse-Workspace\SeleniumTests folder (These steps are demonstrated in the following figure)

Eclipse-settingupWorkspace

We have finished setting up the eclipse.  Now, we need to download Selenium RC server / client driver and configure that to Eclipse

  1. Download Selenium server:  http://seleniumhq.org/download/ 
  2. Download Selenium Client driver for Java (from Selenium Client Drivers section)
  3. Create “Selenium” folder in C: drive and copy the Selenium-server.jar as well as unzip the Selenium Client driver (C:\Selenium)

Downloading and unzipping the files into a folder is done.  We need to configure the appropriate Selenium Client driver Jar file to the Eclipse.

  1. Go to Eclipse –> Click  File –> New –> Project (from various options need to select just “project”)
  2. In Select Wizard –> Click Java –> “Java Project” (demonstrated in the below figure)
  3. SelectJavaProject
  4. Give the project name (e.g. SugarCRMTests)
  5. Click Finish – Click Yes
  6. Now we are done with creation of project and need to configure the Selenium Client driver to this Project
  7. Right Click “SugarCRMTests” project
  8. settingExternalLibrary 
  9. Click “Java Build Path”
  10. Click Libraries tab
  11. Click “Add External JARs” button
  12. Select “Selenium Client Drivers” unzipped in C:\Selenium folder (Selenium Server JAR file should not be added)
  13. Click OK
  14. Referenced libraries –> contains both the Selenium Client driver jar files as shown in the below picture.
  15. ReferencedLibraries

So far we have learnt the following:

  1. Download latest JDK and install
  2. Download Selenium Server as well as Client drivers and unzip into a folder
  3. Download Eclipse and configure

In the next post we will learn how to execute selenium tests using Eclipse

Introduction to Quick Test Professional


QTP is a widely used Test Automation tool in the present IT industry.  Since, there are software products which serve Enterprise Customers with wide variety of features.  It is very difficult for the Manual Testing Engineers to execute all the test cases in the timely manner.  Hence Test Automation tools became imperative in the software industry.

There is a wide base of QTP users as it supports variety of technologies.  We can have a glimpse of the type of applications QTP supports by looking at the number of Add-ins QTP has (There are around 14 different add-ins present).  This is a record and playback tool, which records user actions on the Application Under Test and then the same test can be played back.  Also, it supports a Scripting language called VB Scripting.

I’ll be writing on QTP’s features as well as VB Scripting as much as possible in my future posts.

Selenium – Understanding Object Identification


In previous two posts I have explained how to setup a sample web application, recording and playing back tests with Selenium IDE.  Now, we will understand how Selenium identifies the objects on the Application Under Test.

To identify the objects such as Links, Buttons, Edit boxes, Drop downs, etc on the application Selenium uses a concept called “Locators”.  There are around 8 different types of locators.  Out of them, I will be explaining only four as they are widely used while automating the test cases using Selenium.

  1. id = id – By default Selenium IDE takes “id” as Locator

Every Web Page will be having some HTML Source code.  This can be viewed using “View –> Page Source / View source” on the browser. In the following picture we can see “id” attribute of a HTML tag is used as a locator to identify an object.

Locator-id

2.  name = name (Second locator)

We will do the following steps to understand how exactly Selenium identifies the objects:

  1. Start Selenium IDE from Tools menu of the browser
  2. Login to sample application – Sugar CRM
  3. Click on Accounts tab
  4. Click on Create Account link
  5. Now Create Account Page gets displayed

Selenium IDE has recorded all these steps.  In the following pictures all the steps above can be seen.

viewsource-1

viewsource-3

In the following picture we can see the “name” property of the object is used as Locator to identify the object.

viewsource-2

3. xpath = xpathExpression.  Xpath is used as a Locator to identify an object.  This is an expression which is formed by combining various HTML tags.  For example: //a[contains(text(),’VRL travels’)], //div[@id=’shortcuts’]/span/span[2]/a/span

xpath

4.  link=link text (in HTML source we can located this using “href” tag)

linkLocator

I hope you have understood how Selenium identifies objects using different Locators with all of the above screenshots & explanation.

Please provide me your valuable suggestions through comments.

Recording tests with Selenium


Let us record some tests with Selenium IDE:

Test – 1:

  1. Open the browser and navigate to SugarCRM Login page
  2. Login to application with username – admin and password
  3. Click on Accounts tab
  4. Click on Create Account link
  5. Fill all the necessary fields
  6. Click Save button
  7. Click View Accounts link
  8. Verify the Account created

Selenium Recording

Following are the Selenese commands we recorded in this test:

  1. open
  2. type
  3. clickAndWait
  4. select
  5. verifyTextPresent

Each command does a particular action on the Application Under Test (AUT).  In Selenium IDE, recording table has three columns:  Command, Target, and Value.  As you can see, some of the commands do have Value but all of them have Target.   While recording actions on the AUT, Selenium does record Action, Object, and its Value.  Because to play back the whole test it needs to do the same action whichever recorded during the “recording session”.

In the recorded test for the sixth command “type”, we need to change the value to some other name as it should be unique.

Now, we will try to play back the recorded test.

Yes!! it executed all the commands and created an account.  We have tried recording and running our first test scenario using Selenium IDE.

Note: Please refer to my previous post for Installation of SugarCRM & Selenium IDE

Introduction to Selenium


Selenium is a Test Automation tool used for automating Web Applications testing.  Before starting to learn this tool, I strongly recommend downloading any of the Open Source web application from sourceforge.net.  I will be using Sugar CRM for demonstration purposes.  I have chosen this application mainly because this has a free edition.  We can just download the community edition and start using it.

You can download Sugar CRM from the following location: Sugar CRM Community Edition – Open Source

But this web application requires a web server (Apache), PHP, and a database (MySQL/SQL server).  Either we can download them individually and configure them to run the SugarCRM or we can download the Stack Installers for Sugar Community Edition 

I suggest installing FastStack Windows Installer for MySQL.  This will install Apache, MySQL, and PHP and configures the SugarCRM and we can directly access the application from browser.

SugarCRM installation

Installation of SugarCRM community edition:

  1. Download the SugarCRM Community edition
  2. Double Click the installer
  3. Click next and just provide credentials for MySQL and initial login into SugarCRM.

Access the application using the URL – http://127.0.0.1/sugarcrm/index.php?action=Login&module=Users&login_module=Home&login_action=index

sugarLogin

Login with default username – admin and password which you have given during installation.

Now, we need to download Selenium and this can be downloaded from Seleniumhq.org.  If you are new to Selenium, you may wonder which of the application in “Downloads” section needs to be downloaded.  Don’t bother, initially just download Selenium IDE and Selenium RC.  I will explain both of these in detail in the subsequent posts.

Selenium IDE is an extension to Mozilla Firefox.  When you download this it will get installed on the Mozilla as an Add-On.

Selenium IDE

To start accessing Selenium IDE:

1. Open a new Firefox browser

2. Click on tools

3. Click on Selenium IDE

You are done!!

Now we can start automating the tests with Selenium IDE.

Follow

Get every new post delivered to your Inbox.