Create New Maven Project In Eclipse For Selenium WebDriver + TestNG

Before creating maven project In eclipse IDE for selenium webdriver, You must be aware about maven and It should be Installed properly In your eclipse IDE. Earlier we learnt about what Is maven In THIS POST and how to download and Install maven In THIS POST. So If maven Is Installed In eclipse then you are ready to create new maven project as described In bellow given steps.


Steps to create new maven project

Step 1 : First of all, Open eclipse IDE and go for creating new project from New -> Other as shown In bellow Image.


It will open new project creation window.

Step 2 : Expand Maven folder In new project creation wizard window and select Maven Project and then click on Next button as shown In bellow Image.


It will take you to project name and location selection screen.

Step 3 : On  project name and location selection screen, You can choose work space location for maven project by clicking on browse button. If wants to use default work space location then select that check box and click on Next button as bellow.


It will take you to Archetype selection screen.

Steps 4 : On Archetype selection screen, Select Maven-archetype-quickstart option as bellow and click on Next button.


Step 5 : On next screen, Enter Group Id = STTA, Artifact Id = MavenProject and Package = STTA.MavenProject as shown In bellow Image. Then click on Finish button.


It will create new maven project In eclipse as bellow.


Now your maven project Is created. You can see there Is pom.cml file under your project as shown above.

Step 6 : Now you need to add selenium webdriver and TestNG dependencies with latest version In pom.xml file. Open pom.xml file and copy-paste bellow given code In It and then save It.

<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>STTA</groupId>
  <artifactId>MavenProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenProject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
     <groupId>org.seleniumhq.selenium</groupId>
     <artifactId>selenium-java</artifactId>
     <version>2.44.0</version>
   </dependency>  
    <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.8.8</version>
    <scope>test</scope>
 </dependency>
  </dependencies>
</project>

Current latest version of selenium webdriver Is 2.44.0. TestNG latest version Is 6.8.8. You can change It If version updated version release In future.

Step 7 : Delete existing AppTest.java file from src/testjava folder package and create new class file under same package with name = WebDriverTest.java as bellow.


Now copy-paste bellow given code In that WebDriverTest.java file and save It.
package STTA.MavenProject;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class WebDriverTest {
 WebDriver driver;

  @Test
  public void verifySearch() {
   driver = new FirefoxDriver();
   driver.get("http://only-testing-blog.blogspot.com/");
   driver.quit();
  }
}

Above file will show you errors as we do not have Included selenium and testng jar files In project's build path. Here maven will do It automatically for us.

When you save It, It will start building work space by downloading required jar files from maven central repository and store In local repository based on your selenium webdriver and TestNG versions as bellow. It can take 5 to 20 minutes based on your Internet speed.


Running webdriver project from pom.xml file
When above process get completed, You can run your project from pom.xml file. To run It, Right click on pom.xml file and select Run As -> Maven test as shown In bellow Image.


It will start executing webdriver test WebDriverTest.java file. This Is the way to create and run webdriver test In eclipse using maven and testng.

12 comments:

  1. Aravid,

    MAny many thanks for tutorials

    Please send me ur email address

    need to ask some suggesstions

    ReplyDelete
    Replies
    1. you can contact me on aravindgabani@gmail.com

      Delete
  2. Thanks for sharing this information.

    ReplyDelete
  3. [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Project 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Project ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory C:\Users\jaggnath\workspace\Project\src\main\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Project ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to C:\Users\jaggnath\workspace\Project\target\classes
    [INFO] -------------------------------------------------------------
    [ERROR] COMPILATION ERROR :
    [INFO] -------------------------------------------------------------
    [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    [INFO] 1 error
    [INFO] -------------------------------------------------------------
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 6.490 s
    [INFO] Finished at: 2016-02-23T22:10:07+05:30
    [INFO] Final Memory: 11M/114M
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Project: Compilation failure
    [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    [ERROR] -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

    When I am trying to run this code through maven then I am getting this error

    ReplyDelete
    Replies
    1. Google it man , u will find on stackoverflow

      http://stackoverflow.com/questions/19655184/no-compiler-is-provided-in-this-environment-perhaps-you-are-running-on-a-jre-ra

      Delete
    2. Google it man , Stackoverflow u will find everything

      http://stackoverflow.com/questions/19655184/no-compiler-is-provided-in-this-environment-perhaps-you-are-running-on-a-jre-ra

      Delete
  4. hi
    your posts are very informative..thank you so much for posting...i am new to maven plugin,struggling with one error. unable to create Maven project in eclipse luno getting below error



    Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories.
    Creating maven-archetype-webapp' has encountered a problem. Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webap p:RELEASE from any of the configured repositories.


    Can you please help me to get out from this

    ReplyDelete
  5. aravind
    tnx a lot
    but i have few doubts to ask could u provide ur mail id??

    ReplyDelete
  6. Thanks a lot Arvind for tutorial,

    Here, I am struggling with error displaying in code, its not disappear. it seem that not getting dependency we added. i added dependency of Selenium,TestNG, Junit as you describes. here is my dependency file. please help


    4.0.0

    SSTA
    MavenProject
    0.0.1-SNAPSHOT
    jar

    MavenProject
    http://maven.apache.org


    UTF-8




    org.seleniumhq.selenium
    selenium-java
    3.4.0


    org.testng
    testng
    6.8
    test


    junit
    junit
    4.12



    ReplyDelete
  7. Line below is picked up from the article:-

    "You can see there Is pom.cml file under your project as shown above"
    Rather than pom.cml it should be pom.xml.

    ReplyDelete