Wikipedia

Search results

Sunday, 3 August 2014

Create selenium webdriver project in eclipse with maven

I often see beginners ask that I have downloaded the selenium jar file, now tell me how to start :) Well Selenium webdriver for testing and building a test framework is not just only to have API or jar file, there is much more needed. You will need to integrate Selenium WebDriver API with different libraries, tools, and plugins and so on, for test script creation. You will need an Integrated Development Environment (IDE) to build your test project and other dependencies to build the framework.

Eclipse is a widely used IDE in the Java world. Along with Eclipse, Apache Maven provides support for managing the entire lifecycle of a test project. Maven is used to define project structure, dependencies, build, and test management etc.
We will use Eclipse and Maven for building the Selenium WebDriver test framework. There are some other benefits of using Maven is that we can get all the Selenium libraries files and their dependencies by configuring the pom.xml file.

Maven is a project management tool, provides concept of a project object model (POM) file to manage project’s build, dependency and documentation. The most powerful feature is able to download the project dependency libraries automatically. Click here how to configure mavenKnow more about Maven.
Let see how to configure eclipse with maven for selenium: (assuming eclipse is already there or you can go to http://www.eclipse.org/downloads/packages/release/juno/sr2)
  • Launch the Eclipse IDE.
  • Create a new project by selecting File |New | Other from Eclipse Main Menu.
  • On the New dialog, select Maven |Maven Project as shown in the following screenshot:
  • maven-project-selection
  • 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:
  • new-maven-project
  • 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. Set everything to default and click on the Finish button as shown in the following screenshot:
  • new-maven-project-selenium
  • Eclipse will create the myproject project with a structure (in Package Explorer) similar to the one shown in the following screenshot:
  • project-explorer
  • Right-click on JRE System Library [J2SE-1.5] and select the Properties option from the menu.
  • On the Properties for JRE System Library [J2SE-1.5] dialog box, make sure Workspace default JRE (jre7) is selected. If this option is not selected by default, select this option.
Note: The JRE version might change based on the Java version installed on your machine.
  • Click on the OK button to save the change as shown in the following screenshot:
  • project-explorer-jre
  • 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.
  • Add the WebDriver and JUnit 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-java</artifactId>
      <version>2.40.0</version>
   </dependency>
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
      <scope>test</scope>
   </dependency>
</dependencies>
</project>
You can get the latest dependency information for Selenium and JUnit
from http://seleniumhq.org/download/maven.html and http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html
respectively.
Note: TestNG is another widely used unit-testing framework in Java World. If you want to add TestNG support to the project instead of JUnit, you can get its Maven entry at http://testng.org/doc/maven.html
Hope, this article is helpful in setting up selenium java project using maven. Keep visiting!

No comments:

Post a Comment