Showing posts with label pom.xml. Show all posts
Showing posts with label pom.xml. Show all posts

Saturday 3 October 2015

Create selenium WebDriver project in eclipse with Apache maven

The Problem
I often see Selenium practitioners ask that I have downloaded necessary resources to work on Selenium WebDriver. But, What should be project structure, how to manage dependencies, How to do build  and test management etc.

A Solution
By leveraging Apache Maven in our test framework, we can manage entire life-cycle of a test project. It provides support to define project structure, download the project dependencies libraries automatically, build and test management etc. Maven allows us to get all the Selenium libraries files and their dependencies by configuring the pom.xml (POM – Project Object Model) file.

Let’s dig in with an example

An Example
We will use Eclipse IDE and Maven for building the Selenium WebDriver test framework.
  1. Launch the Eclipse IDE.
  2. Create a new project by selecting [File -> New -> Other] from Eclipse Main Menu.
  3. On the New dialog, select [Maven -> Maven Project] as shown in the following screenshot:

  1. Click on [Next], the New Maven Project dialog will be displayed. Select the [Create a simple project (skip archetype selection)] check-box and set everything to default and click on the Next button as shown in the following screenshot: 

  1. On the New Maven Project dialog box, enter base package name (like com.myproject.app) in Group Id and project name (like myproject) in Artifact Id textboxes. You can also add a name and description but it is optional. Set everything to default and click on the [Finish] button as shown in the following screenshot:


  1. Eclipse will create the myproject project with a structure (in Package Explorer) similar to the one shown in the following screenshot:

7.       
           7. The JRE version might change based on the Java version installed on your machine. 
              Right-click on JRE System Library [J2SE-1.5] and select the Properties option from the 
              menu.       
  1. Select pom.xml from Package Explorer. This will open the pom.xml file in the editor area with the Overview tab open. Select the pom.xml tab instead.
  2. Add the WebDriver and TestNG dependencies highlighted in the following code snippet, to pom.xml in the <project> node:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
         <modelVersion>4.0.0</modelVersion>
         <groupId>com.myproject.app</groupId>
         <artifactId>myproject</artifactId>
         <version>0.0.1-SNAPSHOT</version>
<dependencies>
     <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>2.42.2</version>
    </dependency>
   <dependency>
                  <groupId>org.testng</groupId>
                  <artifactId>testng</artifactId>
                  <version>6.8.8</version>
   <scope>test</scope>
   </dependency>
</dependencies>
  <build>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </testResource>
      <testResource>
        <directory>src/test/java</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </testResource>
    </testResources>
   
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
    </build>
/project>

  1. Right click on pom.xml from Package Explorer & Select [Run As -> Maven Install]

Conclusion
If you'd like to see the code and example used in the above project, you can find them here.


Happy Testing!