In our previous Selenium tutorial, we acquired knowledge about a build tool called “Apache Ant”. We also extensively discussed its applicability and importance along with the practical approach.
In this Selenium Testing tutorial, we will explore Maven – a build automation tool which is distributed under the Apache Software Foundation. It is primarily utilized for Java projects. It ensures consistency in building projects.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
Maven is also used to manage dependencies. For instance, if you are using version 2.35 of Selenium and need to switch to another version later on, Maven can handle this easily. We will discuss more examples of this later in this chapter. Maven works effectively when dealing with a large number of Jar files with different versions.
Key Learning Points:
Definition of a build tool
A build tool is used to set up everything required to independently run your Java code. This can be applied to your entire Java project. It generates source code, compiles code, and packages code into a JAR file, among other tasks. Maven provides a common platform to perform these activities, making the programmer’s life easier when handling large projects.
Maven provides the pom.xml file, which is the core of any project. It acts as the configuration file where all the necessary information is stored. Many Integrated Development Environments (IDEs) are available to facilitate its usage, such as Eclipse, NetBeans, and IntelliJ.
Maven stores all project JAR files in a repository, which can be a central, local, or remote repository. Maven downloads the necessary dependency JARs from a central repository. Most commonly used libraries are available at http://repo1.maven.org/maven2/.
Downloaded libraries are stored in the local repository called “m2”. Maven uses the libraries available in the “m2” folder, and if any new dependency is added, Maven downloads it from the central repository to the local repository. If libraries are not available in the central repository, Maven looks for them in the remote repository. The user has to configure the remote repository in the pom.xml file to download from it.
Here is an example of configuring a remote repository in the pom.xml file. Provide an ID and URL for the repository where the libraries are stored:
<repositories> <repository> <id>libraryId</id> <url>http://comanyrepositryId</url> </repository> </repositories>
General Phrases used in Maven:
- groupId: Typically, the groupId refers to a domain ID. Best practice is to use the company name as the groupId, as it helps uniquely identify the project.
- artifactId: It is basically the name of the JAR file without the version.
- version: This tag is used to specify the version of the project.
- Local repository: Maven downloads all the required dependencies and stores them in the local repository called “m2”. More details about this will be shared in the next topic.
Build Life Cycle
The basic Maven phases are used as follows:
- clean: Deletes all artifacts and targets that have been previously created.
- compile: Used to compile the source code of the project.
- test: Tests the compiled code. These tests do not require packaging or deployment.
- package: Packages the project, converting it into a JAR or WAR file, for example.
- install: Installs the package into the local repository for use by other projects.
Maven Setup
Step 1: To set up Maven, download the latest version of Maven from the Apache website, depending on your operating system.
Step 2: Unzip the downloaded folder and save it on your local disk.
Step 3: Create an environment variable for MAVEN_HOME. Follow the steps below:
Navigate to System Properties -> Advanced System Settings -> Environment Variable -> System Variable -> New -> Add the path of the Maven folder.
Step 4: Edit the PATH variable and provide the path to the bin folder.
Step 5: Verify the Maven installation by using the command prompt and don’t forget to set up JAVA_HOME.
Use “mvn –version” to verify the Maven version. The output should be similar to the following:
Installing Maven IDE in Eclipse
Maven provides an IDE integration for Eclipse. Here, we will use Eclipse Juno for the demonstration.
Navigate to Help -> Eclipse Marketplace -> Search for “maven” -> Install “Maven Integration for Eclipse”
After installation, restart Eclipse.
Then, right-click on the pom.xml file and verify that all the required options are available, as shown below.
Creating a Maven project:
Step 1: Navigate to File -> New -> Other -> Maven -> Maven Project -> Click Next
Step 2: Check the “Create a simple project” option and click Next
Step 3: Provide the Group Id and Artifact Id. You can change the version of the JAR file as per your requirements. Here, I am using the default name. Click Finish.
Step 4: After finishing the setup, you will find that the project structure has been created, as shown below. The pom.xml file is created and is used to download all the necessary dependencies.
The pom.xml file looks like the following:
<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.softwaretestinghelp.test</groupId> <artifactId>com.softwaretestinghelp.selenium</artifactId> <version>0.0.1-SNAPSHOT</version> </project>
Step 5: Add dependencies for Selenium.
All Selenium Maven artifacts are available in the central repository at:
http://repo1.maven.org/maven2/org/seleniumhq/selenium/
Add the following dependencies in the pom.xml file:
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.41.0</version> </dependency>
Similarly, the following is the dependency for JUnit:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.4</version> </dependency>
If you want to add other third-party JAR files, you can include those dependencies in the pom.xml file as well.
Step 6: The final pom.xml file will look like the following:
<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.softwaretestinghelp.test</groupId> <artifactId>com.softwaretestinghelp.selenium</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.41.0</version> </dependency> </dependencies> </project>
Step 7: Maven will download all the necessary dependency JARs into the local repository called “.m2”.
The “.m2” folder is usually located at Users -> username -> .m2.
All the JAR files will be placed in a repository folder within the “.m2” folder. Maven creates separate folders for different versions and different group IDs.
Step 8: If the “.m2” folder does not appear in Maven dependencies, you can manually add the JAR files:
- Eclipse: Windows -> Preference
- Navigate to Java -> Build Path -> Classpath Variables
- Click the New button -> Define M2_REPO and provide the path to the “.m2” folder.
Step 9: After a successful setup, you will find a Maven Dependencies folder containing the required dependency JARs for the project.
Building the Project
The project can be built using either the IDE or the command prompt.
To build the project using the IDE, right-click on the POM file and select “Run As” -> “Maven Build”.
Enter goals such as “clean install” and click “Run”.
The same can be done using the command prompt by navigating to the project folder where the pom.xml file is located, and using the following commands to clean, compile, and install:
For clean: mvn clean
For compile: mvn compile
For Install: mvn install
Upon successful execution, you will see the following information displayed when cleaning a project, along with the message “BUILD SUCCESS”:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building com.softwaretestinghelp.0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ com.softwaretestinghelp ---[INFO] Deleting C:UsersrshwusWORKSPACEcom.softwaretestinghelptarget [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.702s [INFO] Finished at: Sat May 24 18:58:22 IST 2014 [INFO] Final Memory: 2M/15M [INFO] ------------------------------------------------------------------------
Conclusion
Maven simplifies the handling of code and the process of building projects. Most projects follow the Maven structure.
Download all the necessary dependencies from the Maven Central Repository. If a dependency is not available in the Maven Central Repository, you need to add the repository path explicitly in the pom.xml file.
While other build tools like Ant are available, it is recommended to use Maven for managing different versions and dependencies. Maven can also manage the dependencies of dependencies, which other tools may not provide. If you have any queries related to Maven, feel free to ask.
Next Tutorial #25: In the upcoming tutorial, we will discuss Hudson, a continuous integration tool. We will explore its importance, role, and benefits in Test Automation Framework. We will cover the installation and working of Hudson from the beginning.