Tuesday 4 March 2014

Selenium Webdriver and TestNG setup in Eclipse:


This tutorial assumes that you are familiar with Java and you have some basic knowledge in coding in Java. Assumes that you are familiar with installations(Running setup files). In order to start setup we should get below listed tools which helps you to write and customize your automation tests.

Pre-requisites:


  1. Eclipse - This is Java IDE where you will be writing your automation scripts. Download Eclipse IDE for Java EE Developers based on system configuration(32-bit or 64-bit).
  2. Selenium Webdriver Java Client Libraries - These libraries are used to customize your automation tests. At the time of writing this post the Webdriver version is 2.39. You can update these jars as per the current versions available. I am also including most useful Jars along with Webdriver Jars. Download them from here.
  3. TestNG - This is a test framework similar to JUnit. We will not use JUnit anywhere in our tutorials. I will explain how to integrate TestNG with eclipse later. TestNg plugin url for eclipse is,  
             For Eclipse 3.4 and above, enter http://beust.com/eclipse.
        For Eclipse 3.3 and below, enter http://beust.com/eclipse1
                      


Step by step guide to install TestNG in Eclipse:
  1. Download Eclipse from the link provided and install
  2. Open Eclipse from your installed location
  3. Open the Install New Software from Help menu on Eclipse. It opens this dialog,
  4. When above dialog appears click on Add button.
  5. Write 'TestNg' in name field or you can name it to what ever you want and get TestNG appropriate plugin url provided above and insert in to Location field by clearing the default text on it. If your url is correct you will see TestNg installation files on the install new software dialog.
  6. Select all check boxes and proceed with installation.
  7. Choose to trust the software site and Restart Eclipse after completion.
Now you have TestNg installed on your Eclipse. Let's see how to setup Webdriver project in Eclipse.

Step by step guide to setup Webdriver project in Eclipse:

  1. In Eclipse go to File -> New -> and select 'Java Project' if visible if not select 'Project'. You will see this dialog, 
  2. Enter 'java' without quotes in Wizards input box and select 'Java Project' click Next.
  3. Give any name to the project that we are going to create. Let's say selenium.demo and click finish. You will see selenium.demo project in the Package Explorer on Eclipse. Expand it.
  4. Right click on src and select New -> Package.
  5. Give it any name. Let's say com.selenium.webdriver. Click here to know best practice in naming the package.
  6. Now right click on your package created in above step and select New -> Class.
  7. Give it any name. Let's say WebdriverDemo. Usually the best practice is to use Upper Camel Case like WebdriverDemoTry to use nouns because a class is normally representing something in the real world.
  8. Leave everything default click Finish. See here, 
  9. Now it's time to download the Selenium Webdriver libraries(Jars). Download it from above given link. Create directory called 'selenium' in C:\ drive. Move downloaded Jars to that directory. So path to the Jars is now 'C:\selenium'
  10. Now right click on the project title and go to Build Path -> Configure Build Path. You will see this dialog, 
  11. Select Libraries tab and click Add External JARs. This will open file explorer.
  12. Go to the directory where Jars are located and select all of them and click open. Yours Jars are associated with your project now.
  13. Double click on the class file created in step 7 in order to open it.
  14. Copy the below code and paste it in your class.
  15. Right click on the editor and select Run As -> TestNG Test.
  16. If everything is fine, above code will launch the firefox browser, loads Google search page and prints 'Web page title: Google' in the console.
That's it, you are done with your setup. Follow my forthcoming posts to learn advanced usage of Webdriver.

If you really want to learn Selenium it's better to install an open source web application on your machine because they are stable. Here is a good application to automate - Sugar CRM.

See my post to know how to install Sugar CRM application and launch it - here.

No comments:

Post a Comment