Thursday, August 13, 2015

Build Optimization - Getting list of big images/files shipped during release

Large images in a webpage is the one of the biggest contributor to performance problems in most web pages. Most of such issues are catched during performance testing when you see large images taking long time to load and also increasing the overall size of page.



Small changes can have a big impact

As a build person I always felt the best time to catch such issues is during build time. A designer might make a mistake of committing large images in Source Control but if we have a mechanism to catch it in next CI build will be great. To make it possible I needed a script which I can hookup with my build or may be jenkins job to take care of this task for me. I used the Linux "find" command with ANT .....wallahhh !!! 
I have list of all JPG/PNG/GIF images which have size greater then 100 kb.

 <target name="get-length">
  <echo file="mail.txt" append="false">List of JPG images :-${line.separator}</echo>
  <exec executable="sh" append="true" output="mail.txt">
     <arg value="-c" />
     <arg value="find /deploy/HEAD -path '*/dist/*' -prune -o -iname '*.jpg' -size +100k -exec ls -lh {} \; | awk '{print $5,$9}'" />
  </exec>
  <echo file="mail.txt" append="true">List of PNG images :-${line.separator}</echo>
  <exec executable="sh" append="true" output="mail.txt">
     <arg value="-c" />
     <arg value="find /deploy/HEAD -path '*/dist/*' -prune -o -iname '*.png' -size +100k -exec ls -lh {} \; | awk '{print $5,$9}'" />
  </exec>
  <echo file="mail.txt" append="true">List of GIF images :-${line.separator}</echo>
  <exec executable="sh" append="true" output="mail.txt">
     <arg value="-c" />
     <arg value="find /deploy/HEAD -path '*/dist/*' -prune -o -iname '*.gif' -size +100k -exec ls -lh {} \; | awk '{print $5,$9}'" />
  </exec>
 </target> 

Hope this helps.

Wednesday, April 13, 2011

Useful Selenium functions Part-1

Calculate similar links count:-
One of the major problems faced during automation is identifying objects on a page which have same attributes e.g multiple check-boxes with the same name and same id.

Number i = browser.getXpathCount("//a[starts-with(@filter,'B:')]");
System.out.println(i); // i will print count of filters starting with B: // this is used for comparing count

browser.click("xpath=(//input[@type='check-box'])[first()]"); // Select first checkbox
browser.click("xpath=(//input[@type='check-box'])[last()]"); // Select last checkbox

browser.click("xpath=(//input[@type='check-box'])[position()=2]"); // Select second checkbox
browser.click("xpath=(//input[@type='check-box'])[position()=4]"); // Select fourth checkbox

Similarly many button which is created using dynamic names can be accessed using the @start-with() function.
browser.click("xpath=button[starts-with(@id, 'li-')]"); // Click button name starting with li-**

Waitforpageload Failure:-

Sometimes waitForPageToLoad() fails then to handle this condition we just need to wait for an element we are waiting to load on the page. Once its loaded our execution should proceed. One of the most useful methods when working with selenium.
    int second = 0;
    // Text 'Copyright' to load on the present page.
    while(!selenium.isElementPresent("//div[@id='searchPageWrapper']/div[@id='footer']/div[contains(text(),'Copyright')]"))
       {
          if(second >= 5) // If page not loaded in 5 second break
                break;
          Thread.sleep(1000);
          second++;
        }

There are many more such methods that will be updated in the later parts.

Wednesday, March 16, 2011

Power of ANT

I am in love with ANT & I seriously mean it. Whenever I think of automation, there is no way I can ignore it. There is so much you can do to automate your daily tasks. Deployment of builds across multiple platforms, restart services, link alive checks, reports generation and even running mysql queries. I consider myself very lucky as whenever I came across any issues I always found the solution using various ANT commands. I will be sharing few important utilities I created using ANT in future blogs but this is something I implemented today.

The task in hand was that while deploying builds manually on stage bed or production sometimes the configuration files were overwritten by the person deploying it. Although the occurrence of it was very low but the impact was huge which can result in downtime of the application. The request was to rename all occurrence of some configuration xml/properties files to be changed to FileName.xml.template/*FileName.properties.template so that config files doesn't get overwritten. I thought it was worth sharing the ant code.

 <move todir="${basedir}/SourceDirectory" includeemptydirs="false">  
       <fileset dir="${basedir}/SourceDirectory">  
         <include name="**/dbconfig.xml"/>  
         <include name="**/webconfig*.xml"/>   
         <include name="**/validip.properties"/>  
         <include name="**/web.xml"/>  
         <include name="**/ServiceSetting.xml"/>  
         <include name="**/logsettigs.properties"/>  
       </fileset>  
       <mapper type="glob" from="*" to="*.template"/>  
  </move>  

Simple commands which processesed around 53,000 files in CVS checkout directory and changed total of approx 530 files in just 1 Sec on a CentOS machine.

One more interesting thing which I implemented was using the macrodef. This is a part of advance ant technique which is supported after ant 1.6 and later. I was working on creating template based build.xml for few projects which used common code base to create multiple deployments. Consider a lib folder needs to be deployed over 10-20 different locations. A simplified macrodef use can really help over come this and reduce reduntant code use.

A simple example :-
 <macrodef name="copylibs">  
  <attribute name="todir" />  
    <sequential>  
      <copy todir="@{todir}" flatten="true" overwrite="true" includeEmptyDirs="no" failonerror="false">  
       <fileset dir="${sourcedirectorytocopyfilesfrom}">  
         <include name="lib/*.jar"/>  
       </fileset>  
      </copy>  
    </sequential>  
 </macrodef>  
 <copylibs todir="${destinationdirectory}/project1/WEB-INF/lib" />  
 <copylibs todir="${destinationdirectory}/project2/WEB-INF/lib" />  
 <copylibs todir="${destinationdirectory}/project3/WEB-INF/lib" />  
 <copylibs todir="${destinationdirectory}/project4/WEB-INF/lib" />  
 <copylibs todir="${destinationdirectory}/project5/WEB-INF/lib" />  

In this way you can overcome some of daily problems faced. In case you are facing some issues similar to what I described or if there something you want to implement and need a solution or guidance for the same, do let me know. I will try to give you the best possible solution.

Tuesday, March 15, 2011

Selenium Simplified

I was in the NCRT-4th meet where I met a lot of people who were struggling to integrate junit with selenium IDE. The whole idea of how to record with Selenium IDE -->migrate the testcase to junit -->Integrating it with eclipse was a real pain for some of the attendees. I think what’s important is to make the first simple test-case run and then move ahead step by step. I thought of simplifying it a bit to help people to kick start with selenium integration.

Let’s list down the some prerequisite first. It’s very important to have these things installed/downloaded in the machine you are trying to setup:-


2.       IDE Installations :-

PS: - These links should be opened only on Firefox as the selenium IDE is available for Firefox only.

3.       Library to be added in Eclipse project class-path

(Do not add this in Eclipse project class path)

(Needed by eclipse)

Xpather: - Feature rich XPath generator, editor, inspector and simple extraction tool.
Firebug: - Firebug integrates with Firefox to put a wealth of development tools at your fingertips while you browse. You can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page.

Scenario to Test:-
 Open a url http://ajay-sharma.blogspot.com/ and check for “Ajay Sharma” link.


Selenium IDE

Steps:-
1.       Open link http://ajay-sharma.blogspot.com/ on your Firefox browser.
2.       On the Firefox toolbar select Tools-->Selenium IDE.
3.       On the Selenium IDE click on the red button to start recording the above scenario.
4.       On the link bar enter the URL http://ajay-sharma.blogspot.com/ .
5.       When the page opens right click on the link “Ajay Sharma” and select “show all available commands” --> assertTextPresent Ajay Sharma.
6.       Navigate back to Selenium IDE and stop the recording.
7.       Now playback the testcase to see there is no error.
If no errors wallah….you are through with you first simplest selenium testcase.

It’s time to take it to the next level and start working on some real stuff.

Export to Junit4:-
1.       On the Selenium IDE from top menu select the “Options -->Format -->Junit 4 (Remote Control).
2.       You will see the code like
package com.example.tests;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;

blah blah…….

You are through to the second step of the ladder. It’s time to setup Eclipse and start automating those monotonous regression cases.

Setting Up JAVA_HOME:-
You need to setup Java_Home to the installed JDK location before starting eclipse. To know more how to setup environmental variables visit the link:-

Setting Up your first Selenium Project:-
1.       Start eclipse and setup a workspace on desired location (In case eclipse is installed for the first time).
2.       Select File -->New -->Java Project.
3.       Type the Project Name as “FirstSeleniumFramework”.
4.       Select the installed JDK version in the section JRE.
5.       Click on the finish button.
6.       Right click on the project “FirstSeleniumFramework” and select new -->option folder.
7.       Name the folder as lib.
8.       Copy the selenium-java-client-driver-1.0.2.jar and junit-4.7.jar to lib folder.
It’s time to add the lib folder to project classpath.
9.       Right click on the project and click on properties.
10.   Select the LHS option “Java Build Path”.
11.   Select libraries from RHS top navigation bar.
12.   Click on the link add external jars and navigate to “${workspace}\FirstSeleniumFramework\lib” folder to add the above two jars or this might be any other folder where you have kept selenium-java-client-driver-1.0.2.jar & junit4.7.jar

All set to create your first Selenium Junit Testcase.

1.       Right click on src folder and click on create package option.
2.       Set Name as “com.example” and click on finish button.
3.       Again right click on newly created package “com.example” and select the option class.
4.       Set the name as “HomePage” and click on the finish button.
5.       Copy pastes the code from selenium IDE to the newly created class.
You will have few errors which will be corrected by changing the following:-

public class Untitled -- > public class HomePage 
package com.example.tests --> package com.example
public void testUntitled() --> public void testNamelink

You are done to execute your first “testNamelink” testcase.

Few things to understand here:-
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://ajay-sharma.blogspot.com/");
localhost= The server where Selenium remote control is installed. You can set this up in some other machine too.
Port= Default port on RC server runs.
*chrome= Browser on which the test-case will be executed. (Other values can be *iexplore,*firefox,*safari,*opera).
Note: - The browser will be opened on the machine where the RC server is running. So for testing on various different browsers you need to install different browsers on the machine acting as RC server.

Start Selenium Remote Control:-
1.       Navigate to the location where you want to setup selenium server.
2.       Copy selenium-server-standalone-2.0b2.jar to the location and start the command prompt.
3.       Navigate to the location and type :-

java –jar selenium-server-standalone-2.0b2.jar –singlewindow

4.       Press enter to start the selenium server.
Finally after all the hard work we are done with project setup. Let’s start testing now. 

Right click on the file Homepage.java and select the option Run As -->Junit Test.

“I know everything will work fine.”

Well this is the simplest of way you can add a selenium test-case. You can use Xpather and firebug to add more complex cases. I will be talking more about this in my next blog.
I will be soon writing about integrating simplium and selenium-grid which will surely help in making selenium worth using for web application testing.

Don't forget to give your feedback if this article is somewhat proved helpful to you. In case of any errors/issues while setup just let me know.

Saturday, March 12, 2011

NCR Testers Meet

Finally after thinking many times how to go about it, I have finally sat down to write my first blog and It will be an injustice if I don’t write it about the people who inspired me to do so. The idea of writing blogs came into my head when I attended my first NCRTMM-3 meet back in Feb held at Sopra Group where I met Mr Ajoy Singha, truly an inspirational person who encourage people to write/read blogs and share their knowledge with the testing community. The entire meet was impressive and it was great to see so many testers discussing about new methodologies, testing methods, key notes and daily problems related to work in my personal favorite segment of the meet problem on the table. Since that day I was occupied with prior commitments toward my work. Till the time I realized that I haven’t written anything, I found myself sitting in the NCRTMM-4 meet scheduled for March 12, 2011 in Innito Technologies in Cyber City, Gurgaon.

As always everything was perfect about the NCR monthly meet. The ambience of Innito was amazing and people were punctual to reach well in time. I was excited for the session conducted by Mr. Manoj Papneja from FIS on the topic “Continuous Integration - A boost to Project Automation” which I have been working on implementing in my company.  People who are interested in knowing more about Continuous Integrating Testing can read the article by Mr. Martin Fowler till the time I write something :).

The key-notes section was also interesting as always. Discussion on topics like “Domain Champions Vs Reshuffling -> Merits-Demerits for a Tester” was a great learning for a tester to improve their skill set and the managers to motivate team reshuffling and help the team to bring out the creativity which is not limited to a certain domain.

Problem on the table had the discussion on the topic "Updating the regression testcases". The question was asked by an IBM employee who has been stuck with the monotonous job of executing 1000 manual testcases for every release that to in a very demanding agile environment where releases are mostly weekly or sometimes monthly. This is something which was annoying for everyone who has felt this pain. People with wide range of background/levels gave different opinions and solutions to overcome this issue.

I had the chance to speak about this issue as I have been facing the same challenge and have found a great solution in term of selenium to overcome this issue. This is the first time I was speaking in NCRTMM so I was excited and nervous. I talked about a real time scenario where I had the same issue of executing 500 regression test-cases for 10 components to be tested on 5 different browsers for a single release which roughly make 500X10X5=25000 manual testcases. The total man hours to manually test will take more than 2 week. So I guided them how I used selenium to integrate those testcases and used selenium grid (simultaneously on different machine) and simplium (serially) to make those testcases work on different browsers. I also talked about how these testcases can be integrated into nightly build/continuous builds which can be run nightly or important testcases on each continuous build and the reports will be delivered to the team on number of testcases passed/failed.

It was nice to receive appreciation from people on the topic I talked about and they encouraged me to give a full presentation on things I did to make it work for me. A lot of them came over and talked to me about the common problems which they are facing while trying to implement selenium.

Finally it was again Ajoy who asked me again that do I write blogs and with a dead voice I replied “I will soon”. So Ajoy your inspiration has definitely made me write this blog. You will soon see my blog on implementing selenium with simplium and definitely an article on Mobile Testing for Testing Circus magazine.

In the end I would urge testers to come forward and attend such NRC tester’s monthly meet. It was a great learning experience for all those who attended it. I would like to congratulate Vipul/Ajoy/Innito/NCRTMM team for successfully organizing the 4th NCR testers meeting. I am looking forward to hear feedbacks, questions from you and surely focusing on contributing more toward the community.
              “Let’s work together to make NCR capital of Software Testing”


Wednesday, February 16, 2011

Welcome to my Zen !

This is Ajay working as Sr. Software Engineer with a leading MNC.

I have over 7 years of experience in the Industry worked on Development, Testing, Maintenance and Technical Support. All these years of experience made me good at understanding the applications/system and how to validate the same.

I have always been fascinated with trying new things. Started my initial career as Network Engineer working on cisco routers/switches, wireless networks and various different operating systems but didn't find it much challenging and creative. Choosing Testing as career brought the best out of me. I am excited to test the applications and discover it’s behavior in many scenarios. I always try to work well with in the system and consistently produce creative ways to ease the work done in a challenging/demanding environment. I believe Testing is not at all a dumb skill and a tester need to have more skills than the one who developed the same.

These skills will eventually spread over your work Domain, Technology and how the systems behave in general.

So here I am to share some of my experience with you all.

PS:- Special thanks to NCR Testers Monthly Meet which inspired me to start spreading the knowledge.