August 18, 2015

Create a Sample Maven Project

In this post i would like to discuss about Maven.

Here are few basic questions:

What is Maven?
What are the advantages of Maven.
How to use Maven in Selenium projects.
How to Create a maven project?


What is Maven:
Maven -- is a build tool and it is mainly used for Java projects. 
Maven is used to manage dependencies.
For example if you are using selenium latest version 2.47.1 and later point of time if you have to upgrade to newer version of selenium, then it can be manged very easily by using Maven dependencies.

What are the advantages of Maven.

It is a build tool, so it is used to setup everything which is required to run your java code independently.

Maven provides pom.xml which is core to any project.

Maven is used to manage dependencies.

Maven is a plugin for many of IDEs. (Eclipse, Netbeans, IntelliJ etc..)


Frequently used terms in Maven:

GroupID:
Group id refers to domain ID. For example you can use company name as groupID.

Artifact ID:
Name of the project. what ever the name you provide in artifact ID it is nothing but your project ID.

Local repository:

Maven downloads all required jar files and stores in local repository with folder name .m2 (C:\Users\username\.m2)


Maven Set Up:

Pre-requisites:
Make sure Java is installed
Make sure Java_Home and path are set under environmental properties.
Latest version of eclipse (Luna or Mars)

If you are using Luna or Mars you need not to install Maven plug in, bcs maven plug in is already installed in these versions of Eclipse EclipseIDE 


Verify Maven is already installed or not.
1. Open Eclipse, Click on Help ----> select About Eclipse..a window will open.

if you see m2 symbol there ..that means maven is installed.




Create Maven Project:

1. File ---New --Other ---Expand Maven and select Maven Project



2. Click Next in "New Maven Project Screen"

3. select "maven-archetype-quickstart" from list of artifacts. (be default quickstart will be selected)
4. Enter Group ID, Artifact ID Click Finish

5. Once you created a project in eclipse you can see the project




Add Dependencies:

Open pom.xml
Click on pom.xml tab
Add a testNG dependency.

  1. Navigate to MavenRepository and search for TestNG
  2. Select the desired version.
  3. Copy testNG dependency and paste it in pom.xml



4. Add Selenium related dependencies.

http://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java/2.47.1


org.seleniumhq.selenium
selenium-java
2.47.1



org.seleniumhq.selenium
selenium-server
2.47.1

Allow some time to download those Jar files and these files will be automatically added to your project. Files will be stored under .m2 folder (C:\Users\username\.m2)

Now you can write your normal Selenium-TestNG program and run the script.





If you want to run test case from maven you need below dependencies:
Pre requisite: Create a testNG test suite. 


Add below plugins to POM.xml

  1. maven-compiler-plugin
  2. maven-surefire-plugin
  3. testng.xml
The maven-surefire-plugin is used to configure and execute tests. Here plugin is used to configure the testing.xml for TestNG test and generate test reports.

The maven-compiler-plugin is used to help in compiling the code and using the particular JDK version for compilation. Add all dependencies in the following code snippet, to pom.xml in the node

Maven-Compiler-plugin:

http://maven.apache.org/plugins/maven-compiler-plugin/usage.html

Maven-sure-fire plugin:
http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html


To run your test cases:

Right click on POM.xml--Run As ---mvn test

scripts which are in testNG.xml will be executed and results will be available under:
Project---target---surefire--report






POM.xml looks like this:










8 comments:

  1. Nice post buddy....

    ReplyDelete
  2. Very nice Naga :)

    ReplyDelete
  3. I have been reading your blog posts. You blog posts are awesome. They provide good and extremely information which is more valuable. Best selenium training in chennai is predominant famous for Selenium Automation Training and how to get the necessary knowledge about the technology and implement it in a effective way.

    ReplyDelete
  4. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Java Online Training from India . or learn thru Online Training mode Hibernate Online Training . Nowadays Hibernate ORM has tons of job opportunities on various vertical industry. or learn thru JavaScript Online Training from India. Nowadays even JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  5. Great blog.you put Good stuff.All the topics were explained briefly.so quickly understand for me.I am waiting for your next fantastic blog.Thanks for sharing.Any coures related details learn...

    software testing course in chennai

    ReplyDelete
  6. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog... Selenium Training in Chennai | Selenium Training institutes in Chennai

    ReplyDelete