What & Why Maven?
- Maven is an automation and build/project management tool.
- It is written in Java Language and used to build and manage projects written in C#, Ruby, Scala, and other languages.
- Maven helps the developer to create a java-based project more easily.
- Central repository to get all dependencies.
- Maintain the common project structure across the organization
- Maven has flexibility to integrate with CI tools like Jenkins
- Plugins for Test framework execution (TestNG, ExtendReportNG)
- To configure the Maven, we need to use Project Object Model, which is stored in a pom.xml-file.
Standard project structure/Template in Maven (mvnrepository.com).Google’ maven in 5 minutes’ :
How to use Maven:
- To configure the Maven, you need to use Project Object Model, which is stored in a pom.xml-file.
- POM includes all the configuration setting related to Maven. Plug-in can be configured and edit in the <plugins> tag of a pom.xml file. And developer can use any plug-in without much detail of each plugin.
- When user start working on Maven, it provides default setting of configuration, so the user does not need to add every configuration in pom.xml
Maven Setup:
-Search ‘Maven Download’ or Go to https://maven.apache.org/download.cgi
-Click on bin.zip link for Windows OR bin.tar.gz for Mac
-Extract the file
-Go to Control panel> System & Security>System> Advanced system settings> Environment Variables>Click on New >type name : MVN_HOME>Past the path of Maven folder> Double click on ‘Path’> move the cursor to the end of the Variable value field.Type a semicolon (;), and enter the path to the Maven bin directory (for example, C:\apache-maven-3.0.4\bin). Click OK.
-To verify whether Maven configured or not: Go to run>type cmd>type mvn –version and press enter. If the Path environment variable upgrade was successful, then you should be able to see versioning information for Maven.
Maven Terminology:
Artifact: An artifact is a file, usually a JAR that gets deployed to a Maven repository.
GroupId: it will identity your project uniquely across all the projects.
Archetype:generate: Generates a new project (empty template)from an archetype
Create Maven Project through command:
mvn archetype:generate -DgroupId=com.codenbox -DartifactId=E2EProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Note: DgroupId is your Package Name & DartifactId is your Project Name
Project connect with Eclipse through command:
1. Go to the project folder and type in command line: cd ProjectName (Ex: cd E2EProject)
2. then type: mvn eclipse:eclipse
3. Build success Means E2EProject connected to Eclipse.
4.Confirmed in project folder to have .classpath, .project files.
5. Open Eclipse>File>import>existing maven project>next>browse and select the project folder (ex:E2EProject)>finish
Maven Dependencies adding:
-Open POM.xml file. -Type in Google ” selenium maven dependency” or go to https://mvnrepository.com/
-Search all the required dependency one by one as you need for your project and add to your POM file. Example: selenium-java, TestNG, Apache commons io, Log4j Core, ExtentReports, etc.
– As Example Copy the latest dependency for Se selenium-java as below:
<dependency>
<groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version>
</dependency>
-Past the dependency in POM file under parent dependencies
TestNG integration to Maven:
-First convert your project TestNG due to create TestNG Xml file. Right click on your project>TestNG>Convert to TestNG
-Search “testng maven integration” or go to https://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html
-Copy the following plugins code for TestNG suite XML :
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
-Go back to POM xml file.
-Type build tag as <build></build> before <dependencies> tag
– Past TestNG suite XML plugins code inside the build tag.
-Then if you run the POM file it will trigger TestNG xml file and will run all the test classes/test case in TestNG file.
Run Maven/POM file/Test suite through command prompt:
-Right click on your Project>Properties>Get the location of the project
-Open command prompt >type cd project location path (ex: C:\Users\Sarif\E2EProject)>hit enter>mvn clean(clean your previous project to reduce failure )> mvn compile (confirm no syntax error by the build success)>mvn test (confirm the build success & no failure).
Note: Make sure to add ‘Test’ word at the end of every test class name as Maven use an algorithm to read and execute the test class where all the test class name should have ‘Test’ word at the end.
Running a Single Test: During development, you may run a single test class repeatedly. To run this through Maven, set the test property to a specific test case. Go to cmd & type> mvn -Dtest=TestCircle test > (note:testClassName+’test’ word)
Build multiple Profile & run through pom.xml:
Lot of time we have requirement to run our selenium automation test cases on different browser ,environment or simply as Smoke, Functional, Regression test.
To understand this lets take an simple example where we have segregated our automation test script based on Smoke and Sanity Test and based on client requirement. I wanted to execute either smoke or sanity test . Below is my folder structure:
Now let’s configure maven profile in our POM.xml like below:
<profiles>
<profile>
<id>SanityTest</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>Sanity.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>Smoke</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>Smoke.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Here I have created two separate maven profile one for executing Smoke automation test and the other one to execute Sanity automation test.Now to execute the required profile we need to go to maven command prompt and use:
mvn test -P profile name
i.e mvn test -P Smoke
hello, your post is amazing.Following your site.
Hey There. I found your blog using msn. That is a very neatly written article. I?ll be sure to bookmark it and come back to learn more of your helpful information. Thanks for the post. I?ll certainly return.