Understanding the POM

A POM file is necessary for any automated testing with Maven.  It can seem daunting at first, especially if you don’t usually work with XML files, but it’s actually very simple.  

POM stands for “Project Object Model”.  It contains instructions about where to look for the source code for your tests, and what versions of JUnit, Maven, and Webdriver to use.  
Here is an example of a POM file, which should look very similar to the file you created for your First Java Project:
<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>MyFirstJavaProject</groupId>
  <artifactId>MyFirstJavaProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
    </dependency>
    <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.32.0</version>
</dependency>
  </dependencies>
</project>
Let’s look at the file one section at a time:  
<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”>
</project>

This is information that describes the schema for the Maven project.  This will be automatically generated for you.
 <modelVersion>4.0.0</modelVersion>
 <groupId>MyFirstJavaProject</groupId>
 <artifactId>MyFirstJavaProject</artifactId>
 <version>0.0.1-SNAPSHOT</version>

This is required information for every POM file-
The version number of Maven
The groupId (can be any name you like)
The artifactId (should match the name of your project)
The version number of your project (can be any number you like, and it’s easy to just leave it at the default of 0.0.1-SNAPSHOT)
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
    </plugins>
</build>

The build section defines what will be necessary to build the project-
The sourceDirectory defines where Maven should look for your source files.  If you ever have trouble with a build, check to make sure this value is correct.  In this case, the source files will be found in the src folder.
The plugins section defines what plugins are necessary in order for your tests to run.

<plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
</plugin>

The plugin that is needed in this case is the Maven compiler plugin.  The compiler version is specified, and the source and target Java versions are specified.  
<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
    </dependency>
    <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.32.0</version>
     </dependency>
  </dependencies>

Finally we need the dependencies; any extra software that the code needs in order to run properly.  In the case of this example, we need JUnit and Selenium to run our tests.  
As you become adept at test automation, you will probably run into situations where a test that ran without difficulty suddenly won’t run at all.  This is often because of browser/Selenium incompatibility; for instance, a new version of Firefox may have been released, and you will need the latest version of Selenium in order for your tests to run in the new browser.  Rather than downloading and replacing files yourself, all you need to do is alter the version number of Selenium in your POM file, and the next time you do a Maven build, the project will fetch and download the new files for you!  
This is far from an exhaustive list of all the information that a POM file can contain; it is merely a brief description to help demystify what the file does and how it can be used.  For more information, see these helpful links:
  

Leave a Reply

Your email address will not be published. Required fields are marked *