Thursday, February 14, 2013

Ant task for generating Cobertura report

The post describes how to use coburtura in the ant task. First of all you must tell ant about the Cobertura Ant tasks using a taskdef statement. The best practice is to place this before any target statements.
 
 
  
  
   
   
   
   
   
  
 

 
 
We must specify for Cobertura which class files to instrument by passing in ant filesets. You need to specify the directory location in "todir" to place the instrumented classes. You can exclude classes by using "excludeclasses" tag or using "ignore". Specify the name of the coverage data file to use in the "datafile" attribute. The default file is "cobertura.ser".

Cobertura-Instrument Task:
 
 
  
   
   
  
 

Running tests(Here i used TestNG to run tests):
The following components are the most important to get cobertura code coverage to work with testng.
This is a static variable that contains the location of the datafile. If the datafile moves, this lets the JVM know that the datafile has moved and is not in the current(or present) working directory.


Adding the instrumented classes before the actual classes guarantees that cobertura source code gets used first. This is required because we want to use the cobertura instrumented code instead of the regular compiled code, however we keep both versions just in case there's an issue with instrumentation.

Creating a reference id that contains just the cobertura.jar is important to guarantee that there is no NoClassDefFound exception with cobertura. When executing test units, the only dependency cobertura requires is the cobertura.jar file. All the other files are necessary for instrumentation, reporting and merging of tasks.
  
 
  
  
  
  
  
  
  
 

Cobertura-Report Task:
For generating cobertura report we need to use "cobertura-report" tag with the source file director specified in "srcdir" and destination report directory in "destdir" attribute.
 
 
 
  
   
   
   
   
   
   
   
  

  
  
 

The report generated by the cobertura looks like as the below snapshot:


Ant task to print all the test environment information

Below are the list of ant properties that gives the information about test environment. For example, if you want to know which java version is used, we can use "ant.java.version" property in the "echo" tag to print in the ant console. Similarly, for 

ant version - ant.version 
current project name - ant.project.name 
current user name - user.name 
current user directory - user.dir 

Below is the ant code to do this:
 
 
 
  
  
  
  
  
 
You will get the following message in your console:
Buildfile: /Users/aksahu/MySpace/Web_Workspace/AutoTest/build.xml
info:
     [echo] Java Version : 1.6
     [echo] Ant Version : Apache Ant version 1.7.1 compiled on June 27 2008
     [echo] Project Name : autoTest
     [echo] User Name : aksahu
     [echo] Project Location : /Users/aksahu/MySpace/Web_Workspace/AutoTest
BUILD SUCCESSFUL
Total time: 753 milliseconds

Saturday, September 22, 2012

Dealing with web elements by different locators

Let’s consider the following sample HTML code snippet as a reference point.
  1. <!DOCTYPE HTML>
  2. <html>
  3.     <head>
  4.         <script type="text/javascript">
  5.             function jsFunction(){
  6.                 alert("Welcome to Selenium UI Automation Testing !");
  7.             }
  8.         </script>
  9.     </head>
  10.     <body>
  11.         <input type="submit" id="webButton" onclick="jsFunction()" value="Click It" />
  12.     <a href="http://aksahu.blogspot.in/">Go Home</a>
  13.     </body>
  14. </html>
Lets start by taking example of any of the selenium command say "click"
You can use same fashion for any of the selenium command like i am using for click.
The click command emulates a click operation for a link, button, checkbox or radio button. It takes a locator (an identifier for which HTML element the command refers to) as an argument.

Using Selectors with Click Command:

xpath

This allows clicking on an element using an XPath expression. Example

selenium.click("xpath=//input[@name=webButton' and @type='submit']");

css

CSS locator is used with Selenium commands to uniquely identify an object or element on a web page. Example,

selenium.click("css=input[name=webButton]”);

name

The name selector is used to click the first element with the specified @name attribute. Example,

selenium.click("name=webButton”);
or
selenium.click("webButton");

id

This allows click on an element with the specified @id attribute. Example,

selenium.click("id=webButton”);

link

This allows clicking on a link element which contains text matching the specified pattern. Example,

selenium.click("link=Go Home”);


Check if selenium server is running in background

When we run selenium RC test cases sometimes we faces this issue saying "java.net.BindException: Selenium is already running on port 4444. Or some other service is." 
When you check the port 4444 no service is running. We change the port and run the program even that too is not working. 
In these cases we need to shutdown the selenium server on this port.
Use below command to shut down the server.

http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer

If selenium server is already running on port 4444 then it will shut down the server and says
OKOK
if selenium is not running on this port 4444 then by hitting above url will give you 
"Unable to connect"
Now you can run your test cases i am sure will run smoothing.

You can also do an alternative way to handle this i.e you can and some code to check whether the selenium server in a specific port is running or not before starting the server like below:

//ServerControl.java
  1. package test.example;
  2. import java.net.HttpURLConnection;
  3. import java.net.URL;
  4. import org.openqa.selenium.server.RemoteControlConfiguration;
  5. import org.openqa.selenium.server.SeleniumServer;
  6. import org.testng.annotations.AfterClass;
  7. import org.testng.annotations.AfterSuite;
  8. import org.testng.annotations.BeforeClass;
  9. import org.testng.annotations.BeforeSuite;
  10. import com.thoughtworks.selenium.DefaultSelenium;
  11. import com.thoughtworks.selenium.Selenium;
  12. /**
  13.  * This class contains methods to control selenium server
  14.  *
  15.  * @author aksahu
  16.  */
  17. public class ServerControl {
  18.     protected Selenium selenium;
  19.     protected SeleniumServer seleniumServer;
  20.     private static String host = "localhost";
  21.     private static int port = 4444;
  22.     @BeforeSuite
  23.     public void startSeleniumServer() {
  24.         if (!isSeleniumServerRunning()) {
  25.             try {
  26.                 RemoteControlConfiguration rc = new RemoteControlConfiguration();
  27.                 seleniumServer = new SeleniumServer(rc);
  28.                 seleniumServer.start();
  29.             } catch (Exception e) {
  30.                 e.printStackTrace();
  31.             }
  32.         }
  33.     }
  34.     @BeforeClass
  35.     public void startSelenium() {
  36.         selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
  37.         selenium.start();
  38.         selenium.windowMaximize();
  39.     }
  40.    
  41.     @AfterClass
  42.     public void stopSelenium() {
  43.         selenium.stop();
  44.     }
  45.     @AfterSuite
  46.     public void stopSeleniumServer() throws Exception {
  47.         seleniumServer.stop();
  48.     }
  49.     /**
  50.      * Check whether the selenium server in the given port is running or not
  51.      * @return
  52.      */
  53.     public static boolean isSeleniumServerRunning() {
  54.         try {
  55.             String baseUrl = "http://" + host + ":" + port;
  56.             System.out.println("Checking selenium server status [" + baseUrl + "]");
  57.             URL url = new URL(baseUrl + "/selenium-server/driver/?cmd=testComplete");
  58.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  59.             if (connection.getResponseCode() == HttpURLConnection.HTTP_OK)
  60.                 return true;
  61.         } catch (Exception e) {
  62.             System.err.println("Could not check selenium server status: " + e.getMessage());
  63.         }
  64.         return false;
  65.     }
  66. }

In the above the method isSeleniumServerRunning() returns a boolean value true if the server is running in the given port and host and returns false otherwise.

Saturday, July 14, 2012

Reading from properties file using Ant

Here is an example ant target which explains how to read from a properties file in ant.
//Build.xml
  1. <project name="Sample" default="delete-dir" basedir=".">
  2.     <!-- Read the properties file -->
  3.     <property file="config/config.properties" prefix="config"/>
  4.        
  5.     <!-- Using the property value(read from file) in ant's preperty attribute-->
  6.     <property name="build.loc" value="${config.build_dir_loc}" />
  7.    
  8.     <!-- Delete directories that are not needed -->
  9.     <target name="delete-dir" >
  10.         <delete dir="${build.loc}"/>
  11.         <echo> /* Directory deleted successfully ! */ </echo>
  12.     </target>
  13. </project>
In the above ant script the file from which we read data is 'config.properties' (present inside 'config' folder)which is shown below:
//config.properties
  1. username = aksahu
  2. password = aksahu123
  3. url = http://quickprotech.blogspot.in/
  4. port = 4444
  5. build_dir_loc = E:/temp/build