Tuesday, October 18, 2016

Create your First Selenium Project

I was reading out many articles\blogs on selenium, I was wondering if I can also share my knowledge whatever I have achieved over my career. So why not start writing something about the one of the most popular open source Test Automation Tool called Selenium. There are lot of references available on Selenium over internet but still I will try to write down all the steps which I had followed when I started using Selenium.

Rather than a need, I started to learn Selenium because of my interest in coding and Test Automation. Since there was no other open source available and I started with Selenium. Though I have used Test Complete before but still learning Selenium was like teaching A, B C to a nursery kid.

I saw people some people using Selenium and I went to them and asked how to automate test case in Selenium. That it is first time I came to know about the IDE called Eclipse. I took the required files from my friend and started exploring it. So in my Tutorial 1, we will cover following points

1. How to Start Eclipse and create Java Project.
2. How to Add External Jars required for Project.
3. How to Open and Close Browser.
*I will try to add ScreenShot where ever required.

So lets start it with our  Step1 

Creating your Java Project
a. Copy all the required eclipse IDE files into a folder.
b. Run executable eclipse.exe. It will open workspace launcher and ask you to select a workspace  location. Select the a location and click OK. It will open the IDE. Workspace is the folder where your project are stored.  
c. Click on Menu File->New->Other. It will open the selection wizard. Select Java Project from there and click Next. Chose the Project Name(say Demo) and default location. Runtime JRE will be by default selected to current installed JRE version on the machine. Alternatively, you can check the Java version by running "java -version" command on command prompt. Click Finish.
d. After successful project creation, you will notice a Package Explorer window will be opened 

Add External Jar required for Selenium
a.  Selenium provide inbuilt libraries and functions to automate most of web pages. Download the latest Selenium server standalone jar from internet. I am here using "selenium-server-standalone-2.53.0.jar".
b. Open Demo project properties page by right click on the folder Demo and click properties option. 
c. Click on Java Build Path option on the left of the window and then click Libraries Menu.
d. Click on Add External jar option and add the above jar to project. 
e. A new option "Reference Libraries" will be created in which we can find all the external jars.

Now we move to our last step of this Tutorial, that is the most exciting part for all the beginners :)

Code to Open Browser
a. First of all create a new class StartTest.java which will be starting point for this point i.e. contain the main function. To add a class, right click on src folder -> New -> Class.
b. Give a package name (say project.test.suites) and class name (say StartTest) and select public static void main(String[] args) option and click Finish.
c. Similarly add another class (say Launcher) without main function.
d. Add the below code snippet

package project.test.suites;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Launcher
{
           public static WebDriver driver=null;
           public void OpenBrowser()
          {
                       try
                       {
                                  driver = new FirefoxDriver();
                                  driver.get("http://www.google.com");
                                 driver.quit();
                        }
                       catch(Exception e)
                     {
                                  e.printStackTrace();
                     }
           }
}

e. Add below code snippet in StartTest.java

package project.test.suites;
public class StartTest
 {
             public static void main(String[] args)
             {
                      Launcher object=new Launcher();
                     object.OpenBrowser();
              }
}

f. Now you ready to launch your firefox browser. Right click on StartTest.java and choose run as -> Java application.

g. It will launch firefox and google .

I hope it will work for you. Thanks for bearing me. Hope it will help you.

No comments:

Post a Comment