Thursday, October 17, 2013

Writing your first script in Webdriver

As we have already created the set up for writing scripts in selenium through the post: How to set up Selenium Webdriver.
Now, we will write our first script in WebDriver that will do the following operations:
1. Opens the Firefox browser window
2. Open the Google page in it.
3. Verify the page title.
4. Close the browser window.

Below is the WebDriver code to achieve above scenario:

package seleniumpkg;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestClass {

 // declaration of objects
 static WebDriver driver;

        //Initialize the String with the url you want to open
 static String baseUrl= "https://www.google.com";  

 public static void main(String[] args) {

  // instantiation of object
  driver= new FirefoxDriver();

  //Open Firefox and direct it to the Base URL
  driver.navigate().to(baseUrl);
  
  //Maximize the browser window
  driver.manage().window().maximize();

  //Get the actual page title
  String title = driver.getTitle(); 
  
  //Verify the actual title with the expected title
  if(title.equals("Google")){
   System.out.println("Verify Title is Passed!");
  }
  else{
   System.out.println("Verify Title is Failed!");
  }

  //Close the browser window
  driver.close();
 }
}

Running the script
In the Eclipse IDE, Click on Run-> Run As-> Java Application


Eclipse Output is displayed in Console and should be "Verify Title is Passed!"


Tuesday, October 8, 2013

How to install and use Firebug and FirePath for Selenium

Firebug and FirePath are add-ons of firefox

Firebug is usually used to identify objects within the DOM (Document Object Model) structure. You can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page.
FirePath is a Firebug extension that adds a development tool to edit, inspect and generate XPath expressions and CSS3 Selectors. It is a objectid locator formed to uniquely identify object within DOM. It helps in also passing them as identifiers within your scripts.

Steps to install Firebug

1. Open Firefox browser and go to www.google.com
2. Write Install Firebug in the search box and Click enter.
3. Click Download Firebug link in the Suggestions.

4. Below window will open. You can click the Download link as per your Firefox version.

5. Click the latest link in the below window and downloading will be started.

6. A window will appear once the download gets completed. Click Install Now on the window and the Firebug will be installed successfully.


Steps to install FirePath

1. Open Firefox browser
2. Click Firefox menu and Select Addons.


3. Add-ons Manager window will be opened.Click Get Add-ons tab.
4. Write Firepath in the search box and click Enter.


5. FirePath icon will be displayed in the available Add-ons. Click Install button of FirePath and the FirePath is successfully installed into your browser.



After installation, Firebug and FirePath will be available in the Extensions Tab of the Add-ons Manager as shown:

How to use Firebug and Firepath?

1. Right click on the element to be inspected on web page.
2. Select "Inspect element using Firebug" option.

3. A window will open at the bottom of the page showing the element's html content. The element can have attributes like id, name, class etc which can be used to find the element in your scripts.



4. If you want to get the Xpath or CSS value of the element then click the FirePath panel as marked in red and select Xpath or CSS from dropdown as per your choice.


 5. Now click the icon marked in red in the below image, move your cursor to the element and click it.


6. The Xpath/Css value will be appeared in the text box. The identified Xpath or CSS value can also be used to find the element in your selenium scripts.




Friday, October 4, 2013

How to install and set up Selenium tool using Java and Eclipse

In this post, we will learn to install and configure Selenium 2 in our system step by step. We will use Java to write our test scripts for selenium, so we configure our Selenium set up using JAVA and Eclipse. We need following components to configure Selenium:
1. Java Development Kit: It is a tool kit that contains jar files responsible for writing and running of Java program.
2. Eclipse IDE: It is a Java-based open source platform that allows a software developer to create a customized development environment (IDE) from plugin components.
3. Selenium Java Client Driver: Selenium webdriver supports many languages and each language has its own client driver. Here we are configuring Selenium 2 with java so we need 'Selenium Java client driver'.
4. Selenium Server: It is basically a selenium RC server and is required when you write your test scripts using Selenium RC API or when you want to connect to a remote machine. If your browser and tests will all run on the same machine, and your tests only use the WebDriver API, then you do not need to run the Selenium-Server.

We will now learn how to download and install Java Development Kit (JDK) and Eclipse in your system and how to download and configure Java client driver and Selenium server inside Eclipse.

Step 1: Download and Install JDK

1. Click here to download Java Development Kit. Click the radio button as shown in below image and click on the JDK link as per your Operating System. For now, I have clicked JDK link for Windows 7.


As you click the JDK link, a dialog box will appear asking to save the file. Save the file on your system and double click it to install JDK.
This JDK version comes bundled with Java Runtime Environment (JRE) so you do not need to download and install the JRE separately.


2. Configure JDK path in the System as follow
a). Go to My Computer-> Properties. Select Advanced System Settings. A dialog box will be opened. Click Environment Variables. A new dialog will appear.

b). Click New button in User Variables. Enter Variable Name: Java_Home and Variable value: "C:\Program Files\Java\jdk1.7.0_11" in the dialog box displayed. (You need to enter the path where your jdk is stored). Click OK button.

c). Select Variable: Path from the list in System Variables and Click Edit button. Add "C:\Program Files\Java\jdk1.7.0_11\bin;" to the Variable value in the dialog box displayed. (You need to enter the path of your jdk bin). Click OK button.


d). Click OK button on the System Properties window. JDK is successfully configured in the system.

Step 2: Download Eclipse IDE

Download Eclipse IDE for Java EE Developers. Make sure you select correctly between Windows 32 bit and Windows 64 bit versions. Choose the Eclipse version you want to work on. Eclipse Indigo is recommended to be used as the latest version "Juno" might have some unresolved issues.You should be able to download a ZIP file named “eclipse-jee-indigo-SR2-win32-x86_64.zip”.


Extract the zip file and save it anywhere on your system. Inside the ZIP file, there is an “eclipse” folder which contains all the application files and the "eclipse.exe" file. You can run 'eclipse.exe' directly so no installation is required to use Eclipse. Create a shortcut on Desktop for "eclipse.exe" for your convenience.


Step 3: Download Selenium Server

Click here to go on the download page for Selenium Server. Click on the link as mentioned in below image to download Selenium Server. (Selenium version is changing time to time so it may be a different version when you will visit download page)


Downloaded Selenium Server will be a jar file named "selenium-server-standalone-2.35.0.jar". 
Save it own your system as per your choice.



Step 4: Download Selenium Java Client Driver

Click here to go on the page from where we download Selenium Java Client Driver. You will find client drivers for other languages there, but only choose the one for Java as shown in image. Click the link marked in red color to download Selenium 2 Java Client driver.

(Selenium version is changing time to time so it may be a different version when you will visit download page)
Downloaded file will be in zip format named as "selenium-java-2.35.0.zip". Extract the zip file and save it own your system as per your choice. This directory contains all the jar files which will be used to configure inside Eclipse.

Step 5: Create a New Project in Eclipse

1. Launch Eclipse IDE by double clicking the "eclipse.exe-Shortcut" icon that we made on desktop in Step 2

2. At the first time on starting Eclipse, it will ask to select your workspace where your work will be stored. Just accept the default location and click OK button. (You can also give the workspace location as per your choice).

3. Eclipse will get opened. Click Workbench icon on Welcome screen.

4. Create a new Project from File->New->Project. A dialog box will be opened. 

5. Select Java Project and click Next Button.

6. Give any Project Name as per your choice, eg: SeleniumProject and click 2nd radio button as shown in image and then click Finish button.

7. A dialog box will appear asking to open a Perspective view. This is not needed right now so select "No" button.

8. Your Project will be created and appear like as shown in the image.

9. Create a Package: Right click on the newly created project and select New->Package and name the package as per your choice. eg: seleniumpkg. Click Finish button.


10. Create a Class: Right click on the newly created package and select New->Class and name your class as per your choice. eg: TestClass. Click Finish button.

11. Now your Eclipse window will look like below.

Step 6: Add Selenium 2 external jar files to the new project:

a). Right click the "SeleniumProject" and select Build Path-> Configure Build Path. A dialog box will be opened.

b). Select Libraries->Click Add External Jars-> Select both .jar files in selenium-2.35.0 folder and Selenium Server jar file named "selenium-server-standalone-2.35.0.jar" -> Click Open.

c). The dialog box will appear as shown below. Click OK button.

Finally, we have successfully configured Selenium libraries into our project. Now you are ready to write your test scripts in eclipse and run it in WebDriver or through Selenium RC server.

Wednesday, October 2, 2013

Choosing among Selenium IDE, RC and Webdriver

Selenium has a lot of different components due to which people often get confused which one to use.
In today's post we will see the features and limitations of different components of Selenium and the purpose of using them.

Why to use Selenium IDE

Selenium IDE was created by Shinya Kasatani to increase the speed of creating the test cases. It has record & playback options which help you to create simple tests quickly (with in seconds).It has a very good user interface and also supports multiple extensions.
Limitation: The main limitation of Selenium IDE is that, it supported in only Firefox browser and basically used as a rapid prototyping tool. For serious, robust test automation and to run your scripts in different browsers, you should use Selenium 1 or Selenium 2.

Why to use Selenium 1 or Selenium RC

Selenium 1 is also known as Selenium RC, was created by Paul Hammant to create a server which allows you to run HTML test suites in a range of different browsers like IE, Mozilla, Chrome, Opera, Safari etc.It also supports multiple languages like Java, Ruby, C#, Perl, Python etc. Similar to language independent it is also platform independent, same code will work on Windows OS, Linux, Mac & Solaris. Selenium RC server only supports the Selenium 1 API's.
Limitation: The main limitation of Selenium RC is that we have to start & stop the server to execute the test scripts.

Why to use Webdriver

To overcome this issue & increase the scope of Selenium RC, Simon Stewart introduced the new version of selenium called as Selenium Webdriver. It was the first cross-platform testing framework that could control the browser from the OS level. It is the actual core API which allows you to write standalone tests. It has all the same features as of RC. Main feature of webdriver over the Selenium RC is that we don’t need to start the server to run the test scripts.

Limitation: Webdriver cannot readily support new browsers because different browsers communicate with OS in different ways and the webdriver operates on the OS level.

Selenium 2 is the latest version of the Selenium project, and includes the IDE, Server and both the Selenium RC and WebDriver APIs, with WebDriver being the core. Selenium 2 still runs Selenium 1’s RC interface for backward compatibility. It supports the WebDriver API and underlying technology, along with the Selenium 1 technology for maximum flexibility in porting your tests.Selenium 2 includes more cohesive and object oriented API making it a much more robust automation tool. Most of the Selenium Project’s efforts are now focused on Selenium 2.

Recommended Selenium Tool
Those who are completely new to Selenium should go with Selenium 2 since this is the portion of Selenium that will continue to be supported in the future. Using webdriver API's of Selenium 2 is recommended.


Tuesday, October 1, 2013

Selenium Components and their Comparison/Differences

 Selenium Components

Selenium tool is a suite of softwares having four components as explained in the diagram.



   Comparison of Selenium IDE, RC and Webdriver