Pages

Sorting of objects based on their fields | Java

We experience the problem of sorting certain arrays or values quiet often in the world of programming. Well, When it is the inbuilt data types like int or char or even string, there are inbuilt methods that will solve the problem.
int[] c={1,5,3,7,3,9,100,50,40,30,20,10,11,9,12};
        
        //sorting of normal array
        Arrays.sort(c);
        System.out.print(Arrays.toString(c));

Now the problem is that, how do we solve a problem of sorting a list of custom objects like this one.

public class CustomSortingTest{
 
    int value=0;

    public CustomSortingTest() {
     }
    public CustomSortingTest(int a){
    value=a;
    }
}

The solution is pretty simple actually. I remember my effort some years back trying to create a separate utility method and passing the list of this object and writing whole logic there to sort the list and return another one.

Java when sorting a collection, looks for the method "compareTo(T..)" which is the function for an inbuilt interface which most of the inbuilt classes happens to be implementing. When we write a custom class, we normally do not implement this interface, and so while trying to sort the list, java never finds the interface method. Now you could simply implement this interface and override the method and write your custom logic of sorting there. Below is an example of this one.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 
 * @author Rajan Prasad Upadhyay
 *
 */
public class CustomSortingTest implements Comparable<CustomSortingTest> {
 
 int value=0;

    public CustomSortingTest() {
    }
    public CustomSortingTest(int a){
        value=a;
    }
    public static void main(String[] args) {
        List <CustomSortingTest> a=new ArrayList();
        int[] b={1,5,3,7,3,9,100,50,40,30,20,10,11,9,12};
        int[] c={1,5,3,7,3,9,100,50,40,30,20,10,11,9,12};
        
        //sorting of normal array
        Arrays.sort(c);
        System.out.print(Arrays.toString(c));
        
        //sorting a list of objects based on their certain fields
        for(int i=0;i<b.length;i++){
            a.add(new CustomSortingTest(b[i]));
        }
        Collections.sort(a);
        for(int i=0;i<a.size();i++){
            System.out.print(" "+a.get(i).value);
        }
    }

    @Override
    public int compareTo(CustomSortingTest o) {
        if(value<o.value){
            return -1;
        }else if(value==o.value){
            return 0;
        }else{
            return 1;
        }
    }
}
The logic is preety simple. Just determine when to return -1 and when to return 1 or 0.

Best of luck

External table cheat sheet


 /*
  *  The directory should have full 777 permission
  *  The directory in which db is trying to create log file, should have full permission
  *  The reject limit unlimited should be given so that it can reject wrong rows
  *
  */
--Creation of directory

CREATE OR REPLACE DIRECTORY raj_directory1 AS '/oracle_data';

--Creation of the external table

  DROP TABLE raj_actors;

    CREATE TABLE raj_actors (
      first_name        VARCHAR2(10),
      last_name         VARCHAR2(10),
      gender            VARCHAR2(10),
      dob               VARCHAR2(12),
      unq_num           VARCHAR2(10)
    )
    ORGANIZATION EXTERNAL (
      TYPE ORACLE_LOADER
      DEFAULT DIRECTORY raj_directory1
      ACCESS PARAMETERS (
        RECORDS DELIMITED BY NEWLINE
        FIELDS TERMINATED BY '|'
        lrtrim
        MISSING FIELD VALUES ARE NULL
        REJECT ROWS WITH ALL NULL fields
        (
          first_name      CHAR(10),
          last_name       CHAR(10),
          gender          CHAR(10) ,
          dob             CHAR(12),
          unq_num         CHAR(10)
        )
      )
      LOCATION ('datafile.txt')
    )
   reject limit UNLIMITED;
----------------------------------------------------------------Table created---


SELECT * FROM raj_actors;

CREATE TABLE raj_actors_int AS SELECT * FROM raj_actors;

SELECT * FROM raj_actors_int;
TRUNCATE TABLE raj_actors_int;

describe table raj_actors_int;




--ZZZ_
--ZTBL_
--HI_




Cheat sheet PLSQL Package

#what is a pl/sql package?
-schema object that groups logically related pl/sql TYPES, ITEMS, AND SUBPROGRAMS.
-packages have two parts
-- specification
-- body
--sometimes body is unnecessary
--specification (spec) is the interface to your application
--it declares the types, variables, constants, exceptions, cursors, and subprograms available for use.
-- body defines cursors and subprograms, implements the spec

CREATE [OR REPLACE] PACKAGE package_name
  [AUTHID {CURRENT_USER | DEFINER}]
  {IS | AS}
  [PRAGMA SERIALLY_REUSABLE;]
  [collection_type_definition ...]
  [record_type_definition ...]
  [subtype_definition ...]
  [collection_declaration ...]
  [constant_declaration ...]
  [exception_declaration ...]
  [object_declaration ...]
  [record_declaration ...]
  [variable_declaration ...]
  [cursor_spec ...]
  [function_spec ...]
  [procedure_spec ...]
  [call_spec ...]
  [PRAGMA RESTRICT_REFERENCES(assertions) ...]
END [package_name];

[CREATE [OR REPLACE] PACKAGE BODY package_name {IS | AS}
  [PRAGMA SERIALLY_REUSABLE;]
  [collection_type_definition ...]
  [record_type_definition ...]
  [subtype_definition ...]
  [collection_declaration ...]
  [constant_declaration ...]
  [exception_declaration ...]
  [object_declaration ...]
  [record_declaration ...]
  [variable_declaration ...]
  [cursor_body ...]
  [function_spec ...]
  [procedure_spec ...]
  [call_spec ...]
[BEGIN
  sequence_of_statements]
END [package_name];]

##Advantages of PL/SQL Packages
-modularity
-easier application design
-information hiding
-added Functionality


Selenium Web Driver Downloading video

G’Day people. Well as the heading of this article suggests, I am going to share with you some of my adventures I have had in the recent months. The adventure is in the virtual world of computers and internet rather than the real world.

The Real problem

Every story begins with a problem. The problem in my case is that Internet is very expensive. And so, as magic comes with a price, so does the internet in modern age. I have to download some video tutorial series from youtube and I prefer not to do that from my home. I know a place where I could but I could not spend too much time sitting there and going to each and every video url and then downloading the video. Now, the quest starts. How to download the videos without spending much time there? I have two choices.

· Get someone to download them for me.

· Discover of an automation, that could do that for me.

It’s not that I could not go with solution number 1, but what if I have to download another set of videos again after some time. Every time finding some to do the job for me was not an option I would prefer. I am a type of person who would rather do some learning, some discovery, some hits and trials and go to solution #2.

First steps

Now I happen to know of a tool, called a webDriver. A web driver is a tool which could communicate with a browser and tell it to perform what actions you desire through programming. I have done clicks, navigations', hovers, execution of javascript etc through it, and I truly believed that I could go one step further and do file downloads as well. I started digging.

Second thought

OK, that was the beginning of the story. The overall steps and happenings of the story are not always funny or adventurous as you might have been thinking. Keeping this in mind, lets directly go to the interesting parts of findings and problem solving and skip the boring part of blood sucking labors and hopeless efforts.

The technical problem

To find some videos in youtube is not really a big problem. It is very easy to write a selenium script that will open a browser, navigate to the youtube site and search for a term in the search box. Now when the search box is available. The real problem begins. Lets say we reached the video page where the video is being played.

clip_image002

The problem are as pointed below.

1. Youtube does not have a direct link to download the video.

Luckily there are plugins which you could just install to the Firefox and it will add a download link to your youtube video page. You just have to click it and then save it. Now another problem which I later found was that, Firefox had these things called the profiles. When you install Firefox for the first time(I am using windows7), a default profile is made. When you install extensions on Firefox from default profile, it is available only for the instance of Firefox opened from default profile. Now selenium opens Firefox with a new profile by default. So the add ons are not available. I had three options.

· Configure Firefox to open with the default profile.

· Create a different profile and configure Firefox to use this one each time.

· Pass Firefox with the add-on files each time to add to itself.

Solution number was not a good idea, mixing normal browsing and development profiles as same. So I went for solution #2.

Steps.

Ø Create a new profile called selenium.

o Hit ctrl + r on windows

o Type “Firefox –profilemanager”

o Create a new profile named selenium, directory to somewhere other than the default place like E:FirefoxSeleniumProfiles

o Now while creating instance of selenium, configure it to use this profile.


FirefoxProfile profile = new FirefoxProfile(new File("E:ProgrammingFirefox-selenium-profile"));

WebDriver driver = new FirefoxDriver(profile);
Ok, the Firefox started with this WebDriver will use that particular profile. You could install the youtube downloader add-ons on that profile by opening its instance and installing it there.

The problem to the point where there is no download button available on the video page of selenium is solved. Now when you manually click the download button and then the mp4 option, a window appears asking you about what to do with the file. Two options are whether to open it with some registered program, or to save it to some location. Now this window is part of native windows and selenium is unable to handle the window.

So now, the problem is “How to force Firefox to save the file in disk quietly instead of asking for option”. And this happen to be easy. Just while instantiating Firefox, add some more options for that profile.
FirefoxProfile profile = new FirefoxProfile(new File("E:ProgrammingFirefox-selenium-profile"));

profile.setPreference("browser.download.folderList",2);

profile.setPreference("browser.download.manager.showWhenStarting",false);

profile.setPreference("browser.download.dir","E:ProgrammingDownloads");

profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,video/mp4");

WebDriver driver = new FirefoxDriver(profile);

 
These options override the default behavior of Firefox download-manager, specify a default download directory and also the mimetypes of files for which to quietly save the files.

The problem is almost solved. The only logic remaining to write is about which keywords to search, how to find the video urls, and how to iterate through those urls. These are yours choice. I have added a sample code to iterate to the urls of the searched videos.
import java.io.File;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Firefox.FirefoxDriver;
import org.openqa.selenium.Firefox.FirefoxProfile;
public class WithExtension {
 static WebDriver getExtendedFirefox(){
 FirefoxProfile profile = new FirefoxProfile(new File("E:ProgrammingFirefox-selenium-profile"));  profile.setPreference("browser.download.folderList",2);  profile.setPreference("browser.download.manager.showWhenStarting",false);  profile.setPreference("browser.download.dir","E:ProgrammingDownloads");  profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,video/mp4");
 WebDriver driver = new FirefoxDriver(profile);
 return driver;
}
public static void main(String[] args) {
 WebDriver driver = getExtendedFirefox();
 driver.get("http://www.youtube.com/watch?v=2V9h1aa-Cro");  driver.navigate().to("https://www.youtube.com");
 driver.findElement(By.id("masthead-search-term")).sendKeys("java tutorial advanced leveln");
 longWait(5000);
 WebElement videoSection = driver.findElement(By.id("gh-activityfeed"));
 List videos = videoSection.findElements(By.className("yt-lockup"));
 System.out.println("The number of videos is " + videos.size());
 for(WebElement vcontent : videos){
  WebElement vpanel = vcontent.findElement(By.className("yt-lockup-content"));
  WebElement vtitle = vpanel.findElement(By.className("yt-lockup-title"));
  System.out.println(vtitle.getText());
  System.out.println(vtitle.findElement(By.tagName("a")).getAttribute("href")+"n");
}
}
public static void longWait(long a){
 try { Thread.sleep(a); }
 catch (InterruptedException e) {}
}
}

Software Testing Approach

1       Abstract

Testing is one of the common tasks almost all the application and software developers out there are adopting. One who is not using automated testing is really a dumb ass and too slow to be in a fast lane. Applications have lots of errors in them when they are developed. The test after development approach cannot simply point out all of them. The purpose of testing is not to completely point out all the defects, but to keep checking the most common types of bugs that could be introduced in the application while trying to change the other features.

2       Introduction

What most of the companies out there do? They have one or two products as their main product. Most of them being analytics application, forums, portals etc . What these applications offer is some set of features and functions. There are so many modules and each click of a button in each page is expected to be linked with some form of feature. Each month or so, they have to upgrade their applications with some requests of the clients. Clients pay lots of money for the applications, and their demands are also too much. They want refinements, logic change, design change, additional features integrated in the application etc. During the process of adding these additional features, there are often change in the previous modules or they are affected in some way.

3       Types of Testing

3.1            Smoke Test

Smoke testing or sanity testing is one very basic form of testing where a software application is tested with very basic features. The approach is
Go to some particular pages
Set some values in some inputs
Click some buttons/links
Verify some features occur or not.
There is a test-plan where these things are written.
sn
Location(Page)
Expected features
Test/Result
Remarks
1
There should be a test box. Starting to type there should result in suggestions dropping down.
Pass with Comments
The expected conditions were met but these things were noticed
2
drive.google.com

Now the person doing the manual testing has to go to the location. Do what is to be done specified in the test plan. There may be a separate column for that as well. There is a test result column which has a conditional formatting. Most commonly, Green for Pass, Red for Fail and Yellow for pass-with-comments. Sometimes the user has to have a good understanding of the product (domain knowledge) to do what is instructed to be done. If there is anything worth mentioning that is noticed during the test, there is a Remarks column.

3.2            Regression Test

Regression testing is a testing where you navigate to each pages there is on the application, and check certain values or features that’s needs to be verified. It’s the QA’s responsibility to prepare a checklist for the pages to be tested and the features for each pages that are to be tested or verified one round before taking the product to production environment at verify them at least once after the release for quick bugs discovery and hot fixes.
sn
page
Feature
Result
Remark
1
Page 1
feature 1
Pass
2
Page 1
feature 2
Fail
3
Page 1
feature 3
4
Page 1
5
Page 2
6
Page2

The template seems same as the Smoke test but the main difference between Regression test and Smoke test is that it is much detailed and covers every page to be tested and every features that is to be tested. This is a very long checklist of items to be tested.

3.3            Data Validation Test

There may or may not be a data validation test based on the scenario and the scope of change and also the type of change. Generally, the general data validation test is covered by the smoke and regression test itself. While testing the general features you happen to be checking the data also that are populated in the tables or paragraphs, forms, drop-downs etc. The data in the backend may be directly affecting the items and options you see from your  account in the application. A separate data validation test is needed in some of the major changes like server-migration, transferring data from one place to another, pointing to different instance of the backend etc. Separate SQL scripts may also be made to compare the tables of the backend.

3.4            Before After Test

This is the test which is most often done each time adding new features in the existing application, or migrating data/application from one place to another etc. One module being developed, and the changes might be affecting the others as well. In the development environment, the changes and the new modules are integrated and two instances of application are run. One of the past release which is assumed to be correct(mostly) and other which is the latest application. The application should point to identical dataset which may or may not be the same instance and if there is a security framework or dashboard feature control system implemented in the application then the two accounts used for testing should have the same settings. Now unlike the regression and smoke test where main focus in on the features like you clicking some buttons and links and expecting something to happen, here you focus on the data. You could simply use the text comparison tools where you feed with string of different text and compare if they are exactly the same. Mostly the differences are found in tables, forms etc. Some custom developed automated testing applications could find which table which column in which page is not matching in the two applications.
Application
Application a on xyz1 module x, Application b on xyz2 module b
Tested by
Rajan Prasad Upadhyay


Date
20 july to 30 july 2013


sn
Column1
Feature
Result
Remark
1
Page 1 of a and page 1 of b
feature 1
Pass
2
feature 2
Fail
3
feature 3
4
5
6
I am just reusing the template for simplicity.

3.5            Load Testing

This is not a very common testing. Load testing is generally done for those applications where performance is an issue. For example a web application takes too long to load, or some feature takes whole lot of time, One module takes too long to process etc. Load testing in one sentence is the test of response time vs number of users. When the number of users simultaneously using an application increases, the throughput decreases and response time increases. On the basis of how many users might be using the system simultaneously, proper care should be taken to keep the response time to an acceptable limit.

There are some other kinds of testing also but these are the most common types of testing most of the applications are doing for their product. They are investing even millions yearly for them getting automated.

4       Tools


There are lots of tools being used for software applications testing. I don’t personally know whole lot of tools but am familiar with a few of them. The most commonly used tool to automate the web-application is Selenium-WebDriver. It is an api and is available in most common languages. What it does is it acts as a user and could open a url, make clicks, find elements and grab their texts, execute javascripts etc using a browser just as a human would do. JMeter is a free tool in java for performance testing. You create a test plan add different ready-made components, configure them and run them. You could visualize your data in different ways.

Summary

So this was my understanding of the general way it is in this world. The article's main purpose is to give a general concept of the processes followed while testing, and the way testing is done, tools used for testing, etc.

Excel Ribbon Introduction 1

Introduction

I have been reading and practicing different skills about Microsoft tools like Office and Excel. As it happens to me that I very easily forget things when I leave them(except some), So what am I going to do is document it in my blog as Word and Excel Tutorial Series so that I could refer to it later. In the mean time, other viewers could also benefit from it. So lets get going.Smile

Excel Ribbon

Microsoft Excel 2007 has eight standard ribbon tabs. There are other additional tabs as well which could be added to the ribbon. There is also a contextual tab which appears when a particular element is selected. For example when a picture is selected in excel, a new tab appear under which are different commands that could be performed to a picture.
image
The ribbon has mainly three parts.
  1. Tabs
  2. Groups
  3. Commands
The tabs are the highest element of the hierarchy. Each tab contains different groups and under each group contains similar kind of commands that could be performed to an element.

Home

The home tab the most general tab. Inside it contains different options like the font size, color, copy-paste utilities, text align commands, text wrap and cells merge-format commands. There are also conditional formatting utilities from where you could add rules like what will be the background of a cell depending on their values etc.

Insert

I guess this is the second most used tab. It contains options like creating a table, pivoting the table elements to analyze data, insert pictures, draw pie charts and bar diagrams, Symbols, etc.
image

Page Layout

This should be one of the most used tab. I contains commands for setting the design of the page. For example, its layout, margins, width-height, backgrounds etc.
image

Formulas

As you know that excel is mostly used to perform certain mathematical formulas and analysis on a table of data, the formulas tab is relevant for excel. Most of the groups in this tab contains commands related to some type of formulas to be performed in the data.
image

Database Test with JMeter


Overview

In my previous post, I have described the basic idea of getting started with JMeter. I am going to give continuation to the same Tool with a Database test design. For better understanding of this post, You might have to take a look at that one.

1. click on the bat file

Go to the bin folder of JMeter and start the Jmeter window.
image

2. Rename the Test Plan to Database Test

When the JMeter window starts, you are ready to develop the test. Start this process by Renaming the Test-plan to Database Test. This name is relevant to the test we are actually developing.
image

3. Add the Thread groups

As always, add the Thread-Group under the Database Test. Configure the timings and the number of users as described in my previous post.
image

4. Add the JDBC Connection Configuration

Not the actual database work begins. Add the JDBC Configuration element inside the Thread-Group. This is the element where you configure your database-server’s location and provide an access variable to this server. You could have several database configurations defined in single test plan, and choose between which one to use from the variable name you defined in those configurations.
image

5. Enter the configuration information

Enter the database configuration information.You can see the Database-URL, and the JDBC driver class etc. The database connector jar for that database should be in the class path of JMeter before running this test. You could do this by putting the jar in  the lib/ or lib/ext/ folder of the JMeter and then restart the JMeter. The top most variable name field is where you provide a reference name for this configuration. Later you will use this same name to access this database.
image

6. Add Database Request Sampler

Database Request sampler is where you actually write the query to be fired and the database configuration to be used.
image

7. Add the queries and connection information

image

8. Add the appropriate listeners

This part is exactly same as my previous post.

9. Run

image

Summary

This example is not much, but it provide the fundamental ideas to use JMeter for database testing. You could start with this simple example and then start exploring and digging deep into the fun of JMeter. You could compare two database, simulate multiple users vs response time rise, create a graph etc.

User creation Oracle

Background

I was involved in transfer from one machine to another in oracle. Initially I have never done this kind of data transfer kind of things. I searched for this one on the google, and It started showing lots of different example and stuffs where we have to unlock the SCOTT user which is also a default user in oracle as system and sys, grant privileges to it etc. In my case, those things may already have been taken care of.

There are mainly two paths you could follow to replicate the same schema to another.
  • Transfer through file dump
  • Transfer through SQL/PL-SQL using dblinks
Now there are some advantages and disadvantages of those two methods.
  • The PL-SQL methods is easier for the implementation as it is quiet straight forward, but not everything is transferred by this method. You may transfer the tables, stored procedures, but there are many objects in the schema and it is hard to transfer all of them. For example: It is most unlikely that you will replicate the users with same usernames and passwords, and you may skip the privileges that they were granted in the source schema.
  • The file dump method is slight tedious. You have to first create a dump file for the schema. The size of the dump may be much larger than the current size of the schema itself because the history data are also stored in the schema unless you forced it to re-size it. You may use the compress all option to create the compressed version of the dump. You then transfer it to the target machine through FTPor SCP or others. Then you have to import the file to the database. Its main advantage is that everything in the schema is transferred. The users, privileges, and everything else.

Create a dump of the schema that you want to transfer(server1)

expdp <schemauser>/<password> schemas=<schemaname> dumpfile=<dump>.dmp DIRECTORY=<DUMP_DIR>
Note:
  • expdp is a os level command for oracle that gets installed on the user or group where oracle is installed.
  • The DIRECTORY object should already have been created in oracle pointing to a location, and the dump file will be created in the location.(Create or replace directory <Directory_name> as '/orcl/data/dumps')

Transfer the dump to server2(server1)

Now we need to transfer the dump created above to server2 where we could actually import it.
This could be done by scp or ftp commands. I am going to use the scp command.
scp <src> <dst>
scp abc.txt user@<host>:/location/of/remote/directory
scp user@<host>:/location/of/remote/file  /path/to/local/directory

Import the dump file to a schema(server2)

To import the dump file just transferred to server2, we could use the following command.
impdp <username>/<password> schema=<schema_name> dumpfile=<filename>.dmp DIRECTORY=<DUMP_DIRECTORY>  
NOTE:

  • The schema <schema_name> on which we are trying to import the dump, should already be created.
  • if the schema names etc were different then the source schema names, remap commands should be used.
  • The schema/user should have appropriate privileges.

Create an schema passing the location where to store the .dbf file(server2)

The dbf file is where the actual informations and metadata regarding the schema are stored. First login to the server using putty(SSH) and hit the following command on the terminal.

CREATE bigfile tablespace tablespacename
 datafile '/orcl/data1/oradata/orcl/tablespacename.dbf'
 size 100M
 autoextend ON NEXT 100M maxsize unlimited
 SEGMENT SPACE MANAGEMENT AUTO
 NOLOGGING;

Create a user, with that tablespace name as the default tablespace(server2)

CREATE USER username IDENTIFIED BY oracle DEFAULT TABLESPACE tablespacename

Grant create session to the user(server2)

GRANT create session TO username;

Now you may also need knowledge about the deletion of users in case your first try did not go so well as mine. Well here's how you do it.

How to drop a user including everything of the user(server2)

 DROP USER XXXXXXXX cascade;

The cascade word will delete everything related to the user including its privileges etc.

Drop the tablespace all everything of it(server2)

DROP TABLESPACE XXXXXXXXXXX INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS

Getting started with Maven

Overview

I still remember my initial college projects where we use to develop various components using various sets of API’s. We used to face compile problem transferring the project from one members machine to another’s machine mostly because the libraries were stored in a location in the main developer’s machine. And when it was transferred to another’s machine, the APIs were not in the same location. The jars were directly included in the build path or sometimes jars were included in a library and the libraries were included in the build path of the project. We also found a general method where, we made a Lib folder inside the project itself and put all our jars into that folder. Now all we have to do while transferring our project from one machine to another machine was to transfer the root folder completely, and include the /Lib folder in the build path of the project. There were other problems however. We have heard about version control systems, and we were using it to store our project safe in the remote location. We have to upload all the library files (jars) to the Git. Certainly not a good idea. The source files were few MB where as the jars could be hundreds of MBs. There is however an easy solution to the problem of which we were unaware  then. The name of the solution is called maven. I have tried it only for Java so I don’t know whether it is or some similar tools is available for other language.  

Introduction

Maven is a command line tool mainly. You go to a location here. Extract it in some location in your computer. Add the location of the bin file in the path of the operating system. Now open a command-prompt and type mvn and click Enter. If it displays some information about maven, congratulations, Maven is installed.  

Creating Maven Project with eclipse

As per the concept of learning by example, let's create a maven project from eclipse and see our first experience with maven. Lets create a java application that requires “Selenium” API. A little thing about this API is that it is a web Driver which can simulate users action of browsing the web browser(hovering, clicking, scrolling, downloading etc.). Lets create a normal Java project with eclipse. clip_image002
 Now , right click on the project folder in the left Project-Explorer panel and then click configure->”Convert to maven Project”. clip_image004
 A configuration window will appear where you have to provide a couple of information. Some of them are group Id, Artifact Id, Version, Packaging, Name, Description etc. These are the minimum things you have to do as shown in the picture. clip_image006
 The group Id, artifactId are rather kind of a signature for the project. You can guess what others are for. Clicking on the finish button will create a pom.xml( Project Object Model) file. clip_image008 This is a general pom for the whole project and not related to just selenium only. Selenium will be just a dependency for the project. To add the dependency , Right click on the pom file and then click Maven->”Add Dependency”. clip_image010 A window will appear. Type “selenium” on the search box. clip_image012
 Select the seleniumhq as selected and click ok. It will add a dependencies tag in the pom. Now create a class as the example below.
Note: For the eclipse to find the search term(eg. selenium here), the maven repository index should have been created. If its not created, Goto Window->ShowView->Maven Repositories and click it. When it appears, expand the central Repository, right click and then finally Create Index. You should have active internet connection, and it could take some time.

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 

import org.openqa.selenium.WebDriver; 

import org.openqa.selenium.WebElement; 

import org.openqa.selenium.chrome.ChromeDriver; 

import org.openqa.selenium.firefox.FirefoxDriver; 

import org.openqa.selenium.ie.InternetExplorerDriver; 

import org.openqa.selenium.remote.DesiredCapabilities; 

public class GoogleTest { 

public WebDriver driver; 

public String testUrl = "http://www.google.com"; 

public GoogleTest() { 

initiateFirefoxDriver(); 

//driver = initiateIEDriver(); 

//driver = new ChromeDriver(); 

} 

public void initiateFirefoxDriver() { 

driver = new FirefoxDriver(); 

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

} 

public WebDriver initiateIEDriver() {// 

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer(); 

capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); 

WebDriver driver = new InternetExplorerDriver(capabilities); 

return (InternetExplorerDriver)driver; 

} 

public void test1() { 

driver.get(testUrl); 

WebElement searchBox = driver.findElement(By.name("q")); 

searchBox.sendKeys("selenium"); 

driver.findElement(By.id("gbqfba")).click(); 

} 

public static void main(String[] args) { 

GoogleTest gt = new GoogleTest(); 

gt.test1(); 

} 

}
Now you are ready to run the class file. Run it and you will see the result.  

Summary

Notice that you have not downloaded any of your library files (jars) and added them in the build path of the project. Rather you added the dependencies in the pom, and the maven, which is the most popular build tool for java applications, will automatically add the dependent libraries into your projects. This method is very useful in the industrial environment as it is totally tiresome to build the dependency manually, and then compile it etc. I will talk about the deployment automation in my next document. Till then happy coding and happy mavenization.

Getting Started with JMeter

The Apache JMeter™ desktop application is open source software, a 100% pure Java application designed to load test functional behavior and measure performance. It was originally designed for testing Web Applications but has since expanded to other test functions.
Apache JMeter may be used to test performance both on static and dynamic resources (files, Servlets, Perl scripts, Java Objects, Data Bases and Queries, FTP Servers and more). It can be used to simulate a heavy load on a server, network or object to test its strength or to analyze overall performance under different load types. You can use it to make a graphical analysis of performance or to test your server/script/object behavior under heavy concurrent load.

Getting started

Download the JMeter binary from here (tgz, zip). Extract the file. You do not need to install jmeter on your computer. Go to the bin folder and run the jmeter.bat if you are in windows or jmeter.sh if you are in linux. After sometime you should see a window like this.

This is the dashboard of JMeter. You do not need to write any codes in jmeter. You just have to add the necessary readymade components in the UI, and then configure them to the particular thing you are trying to test. However, JMeter will generate a *.jmx file which contains necessary informations to start the same set test-plan the next time and you do not have to go through all the steps of arranging the component and configuring them everytime. You can just drag and drop the .jmx file to jmeter window to load the test. You are ready to create your first test plan using JMeter.

Preparing the TestPlan

Lets simulate a condition such that ten users are using google continuously each user clicking some request20 times and then see the performance of www.google.com  .
1.     First of all right click on the test plan and add a ThreadUsers->ThreadGroup.


This will add a component called ThreadGroup as a child component of the TestPlan. When you click on the component, its corresponding panel will appear on the right where you can control various things including its name that appears on the left component tree as well as on the final result.


Enter the values like this. This shows that there are 10 users and they will request 20 times each. So the total number of page requests will be 200. The ramp-up period is the gap of time in which the total request will be made.
2.     The second step is to add sampler(http-Request). The sampler is a component which makes a request to the server of component which you are trying to test. There are various kind of samplers (Ftp, Java, soap etc).
Right click the ThreadGroup and add Samplers->HttpRequestSamplers.



Enter “google.com” in the ServerName or IP text Box.


3.     This is the time to add some listeners.  A listener is a component which records the request and response data and lets the user see the actual data. Different listeners listens to the same data and provides different views of the result. Some focus on time taken, latency, while some shows graph. The “View results Tree” listener which we are using here will visualize the results sequentially with option to see the request data made and response data received. The “Summary Report” listener will show the metrices of the data and the times taken.
Right click the Thread-Group and add Listeners->ViewResultsTree, and Listeners->SummaryReport.



Execution

This is the time you run the test. Save the file with suitable name say “GoogleTest”. Then click the green triangle at the ribbon. 



The test will execute and you will be able to view the request and results in the “View results tree” sampler.


Summary

Thus you have successfully prepared a JMeter script to browse the web for the google with multiple users simultaneously make multiple requests. These kind of tests are very necessary for the production environment to verify that the server could handle expected number of requests per seconds.
This article covers very less features of the JMeter. But as the getting started concepts are much important, this is a good start. You could now yourself explore other features of JMeter. If I get any concern or query or feedback, I will post some other good practices and common issues in testing and how to solve it. :) Happy testing.