Sunday, September 1, 2013

WebDriver with Maven

Maven is a build Automation Tool somewhat like ANT. Having use ANT for the previous project wanted to try Maven this time. I thought it would be easy as I know ANT, however it took a while for me to figure out as i didn't find any proper tutorial on net. And this is the reason I am writing new Blogpost.

I have just started, So if someone knows the better way to do few things mention here please let me know in comment.

Now let's try setting up new Maven Project for Webdriver using Eclipse. However before you do, you need to download and setup Maven on you local machine.

Setup up Maven
  • download the appropriate maven from the following site.
    http://maven.apache.org/download.cgi Extract it to some location and setup the PATH variable. 
  • If you have done everything correctly, Following command on CMD will let you know the Maven Version.
    mvn --version 


Install Maven Plug-in in Eclipse 
Install the m2Eclipse plugin in you eclipse

Setup Maven Project for WebDriver.

  • In Eclipse choose New Maven Project and check the "Create Simple Project" 
  • Fill the Group id and Artifact id 
  • Create New Class named "GoogleTest" under "src/test/java" Copy Following code in it.
  • GoogleTest.java 

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.Test;
    
    /**
     * @author Gaurang_Shah
     */
    public class GoogleTest {
     private WebDriver driver;
    
     @Test
     public void verifySearch() {
      driver = new FirefoxDriver();
      driver.get("http://www.google.com/");
      driver.quit();
     }
    }


  • It will show you some compile time error as we haven't added the dependencies yet.
  • Maven mention all the dependencies in pom.xml file. Please copy and replace the following pom.xml with your project pom.xml file.
    <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>MavenWebDriverDemo</groupId>
     <artifactId>MavenWebDriverDemo</artifactId>
     <version>0.0.1-SNAPSHOT</version>
    
     <dependencies>
      <dependency>
       <groupId>org.seleniumhq.selenium</groupId>
       <artifactId>selenium-java</artifactId>
       <version>2.33.0</version>
      </dependency>
      <dependency>
       <groupId>org.seleniumhq.selenium</groupId>
       <artifactId>selenium-server</artifactId>
       <version>2.33.0</version>
      </dependency>
      <dependency>
       <groupId>org.testng</groupId>
       <artifactId>testng</artifactId>
       <version>6.8.5</version>
      </dependency>
     </dependencies>
    </project>
  • Running Project
    • To run from Eclipse, Right click on pom.xml and choose Run as --> Maven Test 
    • To run from command line, Go to project directory and enter following command
       mvn test

    7 comments:

    Alan Treviño said...

    This is ok but no tests run.

    -------------------------------------------------------
    T E S T S
    -------------------------------------------------------

    Results :

    Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2.143s
    [INFO] Finished at: Thu Dec 05 11:36:48 PST 2013
    [INFO] Final Memory: 8M/111M
    [INFO] ------------------------------------------------------------------------

    karanz said...

    Hi Gaurang,

    This WebDriver+Maven setps are working fine. Could you please let me know what configuration requires to run the same from Jenkins.

    I have installed jenkins and placed the source in SVN but dont have much idea about how to configure a Build for it.

    Please explain.

    deepak gupta said...

    All are good,But last steps are not working properly

    László Kruták said...

    All are good with 2.44.0 Selenium version

    László Kruták said...

    All are good with 2.44.0 Selenium version

    Ajit Jadhav said...

    Its not Working....
    as there is Error Underline at line

    class="IL_AD" id="IL_AD">dependency

    in pom.xml.

    please Help.
    Thanks,
    Ajit Jadhav.

    Gaurang shah said...

    Hi Ajit, I have updated the code, please check.

    Post a Comment