How to download Selenium ?

Download Selenium WebDriver Jars and configuring in eclipse.
In order to start with Automation testing using selenium WebDriver, we need below things –

1. Installed JDK or JRE on your machine.

           2. Installed Eclipse / NetBeans / or any other IDE
As we know, java is not a tool but it is a collection of jars which we need to use in eclipse or any other IDE. Here we will see how can this be achieved.

Step 1: Download Selenium WebDriver jar files.

Use below URL to download Selenium WebDriver jar files.
To Download Latest version, use this URL –   http://www.seleniumhq.org/download/
Once Selenium WebDriver jar has been downloaded, extract them to some folder. Now we will see how to configure these jar files in to our project.

Step 2: Create Java Project

Create Java project in eclipse so that we can configure selenium jars in to java project, which can be used for creating test cases.
Below steps explain how to create java project.
Click on File -> New -> Other
In select wizard, type Java, then select “Java project” from the list.
Give the desired project name & click on finish.
Now, Once the project is created, we need to create Package.
Right click on project that you have created.  Then go to New -> Package
Enter proper package name -> then click on Finish.
Now, once package is created, we need to create a class  where we can write our automation test cases.
Right click on Package, -> New -> class
Give some class name, then click on finish.
That’s it, now we have created a project along with package and class within it.

Step 3: Configure Selenium jar files in java project

Since now, we have created java project, package & class within it, its time to configure selenium jars within project. Below steps explains how this can be done.
Right click on the project -> Build Path -> Configure Build path
Go to Libraries, Click on Add External Jars -> Select the selenium jars (which you downloaded. Mentioned in step 1)
Click on Apply and OK
This is all done. To verify if your jars are added in to project, Check Referenced library in your project. It should contain all the jar files which you have added.
All set now, you are good to begin with your first test case using selenium
Hope this helps. Drop your comments.

How to use multiple browsers (firefox, Chrome, IE) from properties file ?

In my previous post, we talked about how can we use various browsers for testing using selenium WebDriver.

Refer below post:
Now. If you want to use multiple browsers and you want them to choose its value dynamically, let us suppose from properties file or from some xml file, we can do that.
So, you can set the browser value, suppose in properties file (at run time), then that particular browser should be invoked for testing.
First of all, you should know how to read properties file.
Please refer below post for learning How to read propertiesfile in selenium (java)?
Once you are able to read properties file, below lines of code will help you to select browser from properties file – 

package com.aayushCorp.utilities;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.ie.InternetExplorerDriver;



public class BrowserSelection {

public WebDriver selectBrowser() throws Exception {

FileInputStream fis = null;

File configfile = new File( "C:UsersPrakashworkspaceAayush-SeleniumsrccomaayushCorpexternalResourcesConfiguration.Properties");

fis = new FileInputStream(configfile);

Properties prop = new Properties();

prop.load(fis);

WebDriver driver = null;

String browser = prop.getProperty("browser");



while (browser == null) {

System.out.println("Browser is not specified in Configuration file. Terminating process !!!");

System.exit(0);

}

if (browser.equalsIgnoreCase("firefox")) {

driver = new FirefoxDriver();

System.out.println("Browser selected for testing is : Mozilla Firefox");

} else if (browser.equalsIgnoreCase("chrome")) {

System.setProperty("webdriver.chrome.driver", "C:chromedriver.exe");

driver = new ChromeDriver();

System.out.println("Browser selected for testing is : Google Chrome");



} else if (browser.equalsIgnoreCase("ie")) {

System.setProperty("webdriver.ie.driver", "C:IEDriverServer.exe");

driver = new InternetExplorerDriver();

System.out.println("Browser selected for testing is : Internet Explorer");



} else {

System.out.println("Selected browser value should be either firefox or chrome or ie --> Update in Configuration file is required");

System.exit(0);

}

return driver;

}

}

 In above peice of code, Selenium will launch either firefox or chrome or ie based on what value you specifies in properties file.

Hope this helps. Post your comments / suggestions.

Working with Mozilla Firefox, google chrome and Internet Explorer in Selenium WebDriver

We saw earlier that; Selenium WebDriver supports various programming languages and various web browsers for testing. Selenium WebDriver supports various browsers like IE, Firefox, Google Chrome, Opera. 
It can also be used on Android & iOS. Selenium WebDriver can support all operating systems where these browsers can run.
Suggested post to read: Introduction to Selenium 
Majorly people use Mozilla Firefox. In order to use Mozilla Firefox, we do not need to do any special configurations, but for using other browsers like Chrome and IE, we need to set system properties.
Let us talk one by one.
How to use Mozilla Firefox browser using selenium?
For using Mozilla Firefox in Selenium Webdriver, we do not need any configurations setup.
Just create a reference variable of the WebDriver instance and assign it to the instance of FiirefoxDriver class.
So, below line of code will launch Firefox web browser.

WebDriver driver = new FirefoxDriver();
Now, you can use “driver” instance for further process like navigating to URL and proceed with your testing.
To know more about Interface and to understand  Why do we write WebDriver driver = new FirefoxDriver() ?, refer below post
How to use Google Chrome browser using selenium?
In order to use Google Chrome for executing test cases, we need to set system property for “Webdriver.chrome.driver” with executable file of chrome driver (chromedriver.exe).
You can download chromedriver.exe file from here.
 
Once the chromedriver.exe file is downloaded, set system properties as –

      System.setProperty("webdriver.chrome.driver", "C:chromedriver.exe");
(assuming that it is stored at C drive, else provide your file location).
Now, once system property is set, we can launch chrome browser using below line of code – 
WebDriver driver = new ChromeDriver();
Now, you can use “driver” instance for further process like navigating to URL and proceed with your testing.
How to use Internet Explorer browser using selenium?
In order to use Internet Explorer for executing test cases, again, as like we did for google chrome, we need to set system property for “Webdriver.ie.driver” with executable file of IEDriverServer. (IEDriverServer.exe).
You can download IEDriverServer.exe file from here.
Once the IEDriverServer.exe file is downloaded, set system properties as –

   System.setProperty("webdriver.ie.driver", "C:IEDriverServer.exe");
 
(assuming that it is stored at C drive, else provide your file location).
Now, once system property is set, we can launch chrome browser using below line of code –
WebDriver driver = new InternetExplorerDriver();
Now, you can use “driver” instance for further process like navigating to URL and proceed with your testing.
Hope this helps. Drop your comments.

How to read properties file in selenium (java)?

Working with properties file in java / Selenium – 
Hard coding is not a good practice anywhere. It’s hard to go back and change something you’ve hard-coded previously in your code. Suppose you are testing some application. Consider that, its URL keep changing environment to environment, like in UT, UIT, ST, SIT, UAT etc.
Now, your application will be same but URL only will be different.
In this case if you are hard coding URL in your coding, then you need to update the URL whenever it changes.
 Same case would be with Username and password.
So, to overcome situations like this, we usually give these changing parameters in properties file.
You can create the properties file anywhere in the project, just make sure that you give correct path while loading the file.
I
n properties file each entry is stored as a pair of Strings, let us say, Name and value.
Now, let us see, how can we create properties file first of all.
Click on File -> New -> File
 Give some meaningful file name. save the file with “.properties” extension
Once the file is created, Open the file, and have some data, can refer below snap.
Now, the file is created. Let us see how to read this file in our program.
There are 3 simple steps to read file
1. Define FileInputStream & Give your file path of properties file. 

Ex –

File configfile = new File("C:UsersPrakashworkspaceAayush-SeleniumsrccomaayushCorpexternalResourcesConfiguration.Properties");

FileInputStream fis = new FileInputStream(configfile);

2. Load properties file 

For this, create object of properties file & then load.

Properties prop = new Properties();
prop.load(fis);

       3. Retrieve data from properties file.
Use below line to retrieve data.

     PropertyFileValue = prop.getProperty(key);
Below is the complete code, which return the value from properties file based on the key you supply.

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Properties;

public class PropertyOperations {

public String getPropertyValueByKey(String key) {

String PropertyFileValue = null;

FileInputStream fis = null;

try {

File configfile = new File(
fis = new FileInputStream(configfile);

} catch (Exception e) {
System.out.println("Configuration file is not found. terminating Process !!!");

System.exit(0);

}

Properties prop = new Properties();

try {

prop.load(fis);

} catch (IOException e) {

e.printStackTrace();

}


Boolean isFileEmpty = prop.isEmpty();


if (isFileEmpty == false) {

PropertyFileValue = prop.getProperty(key);

} else {

System.out.println("Configuration file is empty. Processing is terminated");

System.exit(0);


}

return PropertyFileValue;


}

}

You can create object of this class in any class where you wanted to retrieve the data from properties file. And then retrieve the data by giving key.

Ex – 

PropertyOperations prop = new PropertyOperations();

String username = prop.getPropertyValueByKey("username")

Above piece of code will return username from properties file in to String username.

Post you questions in comment if you have any

 

First Program in Selenium WebDriver to test www.google.com

Once we have configured Selenium jar files in to Eclipse (refer post: Downloading Selenium WebDriver Jars and configuring in eclipse), We are now good to start with Basic programs using Selenium.
We write the java program for selenium test in a java class. So, let us create one java class.
Right click on Package -> New -> Class (If you don’t find Class over there, then go to Other, and search for Java class there)
Give some meaningful name to the class and click on Finish.
The first basic thing is, we need to create driver of the web browser which will drive the flow.
Here I am using Mozilla Firefox driver.
Below is the sample code for my first selenium test where I am navigating to www.google.com and finding out the Title of the page.
Once the code is written in a class file, right click on it, select Run AS – > Java Application
Sample code – 

package basicsOfSelenium;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class FirstSeleniumTest {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

driver.navigate().to("https://www.google.co.in");

String TitleOfPage = driver.getTitle();

System.out.println("Title of Current page is : "+TitleOfPage);

driver.close();
}

}

Sample Output:

Title of Current page is : Google
Good luck for your beginning

Do post your comments for any questions / suggestions.

Introduction to Selenium – What is Selenium?

           Selenium is an automation tool like other tools in market, like QTP, TestComplete etc.

It is Open Source tool used to automate web based application.
Selenium is suite, consist of different tools as below –
1.      Selenium IDE
2.      Selenium RC
3.      Selenium WebDriver
4.      Selenium Grid
Later, Selenium RC & Webdriver is combined & it becomes Selenium 2.
 
Most commonly used tool is selenium 2. Selenium IDE is not used much. Let us talk about both here.
What is Selenium IDE?
Selenium IDE provides a record & playback feature for authoring tests without learning any test scripting language. It is not used mostly since it has limitations. Only recorded scripts can be played. Selenium IDE is a fully-featured Integrated Development Environment (IDE) that installs as a plugin in Mozilla Firefox and enables developers to test their web applications through Selenium. With the Selenium IDE.
Pros & Cons of Selenium IDE:
Pros – Easy to install & use, No Programming knowledge required, has built in help & reporting module.
Cons – Available only on Mozilla Firefox, can only create prototype of tests, No support to conditional operations.
What is Selenium WebDriver? 
            Selenium WebDriver is better than both, Selenium IDE & Selenium RC in many aspects.  Selenium WebDriver uses programming language to communicate to browser directly.  It supports various programming languages like Java, C#, PHP, Python, Perl & Ruby.
 Selenium WebDriver is not a tool like something. Selenium WebDriver has few Jar files which needs to be added in Eclipse IDE. We Will talk in detail later about this. 
Selenium WebDriver supports various browsers like IE, Firefox, Google Chrome, Opera. It can also be used on Android & iOS. Selenium WebDriver can support all operating systems where these browsers can run. 
Pros & Cons of Selenium WebDriver:
Pros – Communicate directly to browser, Faster execution, Supports various browsers like IE, Firefox, Chrome, Safari etc.

Cons – Programming knowledge required.
More documentation on Selenium can be found on seleniumhq site- 
http://www.seleniumhq.org/