Saturday, July 20, 2013

SetUp Selenium WebDriver with Eclipse using Maven and TestNG


Guide to Setup Selenium WebDriver with Eclipse

Description:-

Easiest and very useful step by step guide to Setup WebDriver with Eclipse(i.e. Java IDE) using Maven(i.e. Build Tool) and TestNG(i.e. Java Testing Framework).
It can be further integrated  with any Continuous Interrogation tool(e.g. Hudson, Jenkins, Teamcity etc.) as it uses build tool (i.e. Maven) and we can do Maven build integration with any of these CI tools.

Note:- Don’t need to download selenium jars then putting those in to a folder then referencing then to your project maually which is an overhead and chances of mistakes are higher.

Pre-Requisite:-

JDK 1.6 should be installed & Path variable should contain “bin” folder location of installed JDK.

Eclipse Indigo installation:-

·         Now Unpack downloaded Eclipse package at desired directory.

Maven Plug In for Eclipse:-
·         Run eclipse.exe to start Eclipse > Click Help > Install New Software… In the dialog that appears, Enter the following URL in the field work with:
http://download.eclipse.org/technology/m2e/milestones/1.0
Note:- Screenshots attached shows different old URL for maven plugin. So, Please ignore that.



·         Click Add…& Give a name for the repository (such as m2eclipse).





·         Click OK.



·         Select the checkbox “Maven Integration for Eclipse”



·         Click “Next”.


  • Click “Finish”.






  • Click “Restart Now”.


Create a Maven Project:-

  • In Eclipse: File > New > Other




  • Expand Maven > Select Maven Project.




  • Click “Next”.




  • Click “Next”.




  • Click “Next” & Enter “com.<company_name>.selenium” in Group Id and “<project_name> (i.e. “spider” here)  in Artifact Id.



  • Click “Finish” & See Spider project with following directory structure has been created.



  • Now delete App.java & AppTest.java using right click context menu “Delete” option.
[Note: - Make sure that JRE System Library is set to “JavaSE-1.7” as above]
Edit pom.xml:-

  • Open pom.xml in edit mode > Replace <dependencies> </dependencies> section with following & Save it:-

<dependencies>
    <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.9</version>
                        <scope>test</scope>
    </dependency>
            <dependency>
                        <groupId>org.seleniumhq.selenium</groupId>
                        <artifactId>selenium-java</artifactId>
                        <version>2.33.0</version>
    </dependency>
            <dependency>
                        <groupId>org.testng</groupId>
                        <artifactId>testng</artifactId>
                        <version>6.5.1</version>
                        <scope>test</scope>
    </dependency>
            <dependency>
                        <groupId>org.apache.poi</groupId>
                        <artifactId>poi</artifactId>
                        <version>3.8</version>
            </dependency>
  </dependencies>

  • Refresh the “spider” project so that Eclipse detects the pom.xml changes, build the project and download required dependencies.
  • After build is finished, All dependencies jar should be available under Maven Dependencies under the Project Explore pane as following:-



TestNG Plug In for eclipse:-

·         Run eclipse.exe to start Eclipse > Click Help > Install New Software… In the dialog that appears, Enter the following URL in the field work with:



·         Click Add…& Give a name for the repository (such as TestNG).




·         Click “OK”.




·         Select the checkbox “TestNG”




  • Click “Next”.




  • Click “Next”.



  • Accept license agreement & Click “Finish”.



  • Click “OK”.



  • Click “Restart Now”.
Now Start writing your test script & Enjoy !!!