Configure Selenium Continuous Integration with Maven project is not a big task. You just need to follow a few sets of steps and you are done with that. But making that project work with Maven is a little tricky and that requires few configurations around your project. Once you are done with that only then your test will be recognized by the Maven build system, else Maven will always ignore your test and perform the build. The complete list of steps are as followed:
- Convert Selenium project into Maven Project
- Remove Errors from the Project
- Remove Associated Libraries from the Project Build Path
- Add Dependencies to the Maven Repository
- Add JDK to the Project Build Path
- Add Maven Dependencies to the Project Build Path
- Resolve Apache POI errors
- Create a Maven Test folder structure
- Create a new TestNg Test
- Run the Test with Maven Build
To practice this step by step I have uploaded the dummy project here, so that we all can be on the same page while converting a complete Selenium project into Maven. There can be many issues you might face with setting up Maven project.
Prerequisite for Converting a Selenium Project into Maven Project
Step 1: Convert a Selenium Project into Maven Project
Before moving on to this step, make sure that you have imported the dummy Selenium project I have shared above. Importing a project in Eclipse is simple, you just need to go on File menu > Import… Then on the new window select General > Existing Projects into Workspace and press Next. Browse the downloaded Selenium project and that’s it. You are all set to go with this tutorial.
1) Once you are done with the import of the dummy project, your project explorer window will look like this.
2) Right-click on the Online Store folder and select Configure > Convert to Maven Project.
3) It then asks you to fill the Group ID and Artifact ID. Fill the appropriate name and click on the Finish button. Do not change the rest of the default settings and try to keep the same name used in this tutorial, as it will help to reduce the ambiguity later.
4) The eclipse will take a few seconds to convert the project and once it is completed with the process, the project explorer window will look like this. There will be few errors in the few packages of the project and a new pom.xml file is created at the bottom.
Step 2: Remove Errors from the Project
You will notice that the project now has quite a good number of errors, These errors are on throws statement because the try-catch block is used in the methods but Exception is not declared in the method declaration. I am not very technical to answer this why the errors were not there when the project was not a Maven project and why it appeared after converting the project. But I do not mind to resolve these errors at first place.
1) Double click on any of the errors under the problem tab on the eclipse ide.
2) Notice that all errors are displayed for throw(e) statement. Just bring the cursor over the throw(e) and it will display the suggested solutions. Select ‘Add throws declaration‘. Just like this, resolve every error on the project by adding throws declaration in all the methods where ever the try-catch statement is used.
Step 3: Remove Associated Libraries from the Project Build Path
During the build, Maven only considers its own libs/repository and it will not consider the libraries available in the project build path. So the next step is to add all those libraries and dependencies which is required in the project. To figure out what all is required, just remove all the libraries from the build path and the project will end up with many more errors. Then you can browse to every single error and one by one add missing jars files to maven repository.
1) Right Click on the project explorer and select Build Path > Configure Build Path.
2) Select the Libraries tab.
3) Select all the available libraries and Remove.
4) The project will now have a number of errors.
Step 4: Add Dependencies to the Maven Repository
Let’s just open the Utils class under the utility package. Notice that the first error is displayed on the WebDriver. This is because we just removed the Selenium Libraries from the build path. So it is the time to add Selenium Libraries under the Maven Dependencies.
Dependency Keyword
Dependencies are the libraries, which are required by the project. For example Log4j jars, Apache Poi jars, Selenium Jars, etc.
Maven Local Repository
This is the place where Maven stores all the project jars files or libraries or dependencies. By default the folder name is ‘.m2‘ and by default the location in windows 7 is ‘Libraries\Documents\.m2‘ or ‘C:\Users\yourusername\.m2‘.
Maven Central Repository
Maven central repository is the default location ‘http://mvnrepository.com/‘ for Maven to download all the project dependency libraries. For any library required in the project, Maven first looks into the .m2 folder of Local Repository, if it does not find the required library then it looks in Central Repository and downloads the library into the local repository.
Maven POM
POM is Project Object Model XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Some of the configurations that can be specified in the POM is the project dependencies, the plugins or goals that can be executed, the build profiles, and so on.
1) Go to http://www.mvnrepository.com/ and search for Selenium Java.
2) Click on the Selenium Java from the search results.
3) Click on the latest version of the Selenium. You are allowed to choose any version here if in case it is specific to your project.
4) Take a look at the highlighted area on the below image. This highlighted details needs to be entered in the Maven pom.xml. There are two different ways of adding dependencies to pom file. First, we will go through the easy way.
5) Double click on the pom.xml file which is at the bottom of the project explorer window.
6) Select the Dependencies tab and then click on Add button.
7) A pop window will display. Enter the highlighted detail here which we copied from maven repository website, as per the below screenshot. Make sure that Group ID, Artifact ID & Version are exactly the same. It is better to copy-paste it, as I am not sure if it is case sensitive.
8) Now the Dependencies tab will look like this with Selenium Java added on it. It is the time to save the changes, that can be done by pressing Ctrl + S.
9) Now once the changes have been saved, right-click at the empty space of the project explorer and select Refresh.
10) If you choose ‘Run in Background‘, it will download the Selenium Java jar files into the Maven dependencies and the downloading process will take place in the background. If you choose ‘Details>>‘, it will display the downloading status bar and the process.
Note: Maven try to connect with Maven repository website and download the added dependency to the maven dependency folder. If in case you are in the office or behind the firewall, you might need to perform some proxy changes to download it.
Note: By default the folder name is ‘.m2‘ and by default the location in windows 7 is ‘Libraries\Documents\.m2‘ or ‘C:\Users\yourusername\.m2‘.
11) In the same way, it is required to add all the dependencies which are required in the project. Now add the Log4j dependency in the POM file.
12) Notice the other errors in the project, you will notice that the TestNG is also required for the dummy project which we have used. Add TestNG dependency to the POM file.
13) Go to ExcelUtils class under the utility package. You will notice so may error on the Excel keywords. It is also required to add Apache POI jars to maven dependencies.
13) One more dependency is left which is called Surefire. The Surefire Plugin is used during the test phase of the build life cycle to execute the unit tests of an application. It generates reports in 2 different file formats like plain text file, XML files, and HTML files as well. Even if you are using TestNG or Junits framework for reporting, this plugin is a must to use, as it helps Maven to identify tests.
This time lets look at the second way of adding dependencies to the project. Click on the POM at the bottom bar of the Eclipse view and it will open the XML structure of the pom file.
14) Select the text from the Maven Repository website.
15) Paste the selected text in the pom file under the ‘<dependencies>‘ tag.
Note: I called this method difficult above, just because if you paste the dependency at the wrong place in the XML, it will create multiple errors, so by following the first method there is no chance of mistake.
Now we are done with adding all the dependencies to the project.
Step 5: Add JDK to the Project Build Path
The prerequisite of this step is the JDK should be installed on the computer. Please follow the Set Up Java link to follow the step by step installation of JDK.
1) Right Click on the project explorer and select Build Path > Configure Build Path.
2) Select the Libraries tab.
3) Click on Add Library...
4) Select JRE System Library and click on the Next button.
5) Select ‘Workspace default JRE(jdk XXXXX)‘ radio button and click on the Finish button.
Note: JDK can be found also by clicking on the Installed JREs button.
Step 6: Add Maven Dependencies to the Project Build Path
When we create a new Maven project, dependencies are added by default, so Maven Dependencies can be seen under the project explorer window. But when the Selenium project is converted to the Maven Project, maven dependencies need to be added on to the project build path. So far we have just added the dependencies in the maven repository, now we need to link that repository to the project.
1) Right Click on the project explorer and select Build Path > Configure Build Path.
2) Select the Libraries tab.
3) Click on Add Library...
4) Select Maven Managed Dependencies and click on Next.
6) Take a look over the project explorer window, Maven Dependencies can be seen.
Step 7: Resolve Apache POI Errors
There is a little change in the current version of Apache POI which is that they have changed the XSSF into HSSF. IF you look into the ExcelUtils class of utility package, you will find many errors around it.
1) Just change all the XSSF entries into HSSF for the sheet, workbook, cell, and row.
2) Now by bringing the cursor over each of the keyword, import the relevant packages.
3) Project explorer window will now look like this, as now the Selenium project is completely error-free.
Step 8: Create the Maven Test folder structure
Maven has its own project folder structure and if you do not want any hassle, it is advisable to follow its default folder structure. For example, all the tests of the project should be in src > Test > Java folder. Let’s just create the same for this project now.
1) Right-click on the src folder and select New > Folder.
2) Give the folder name as ‘test/java‘ and click on the Finish button.
Step 9: Create a TestNG test for the Selenium Maven Project
Either a new test can be created or you are most welcome to cut paste the existing tests to this src/test/java folder. The prerequisite for this step is that the TestNG should be installed in the Eclipse. Please follow the step by step Installation guide to Install TestNG in Eclipse.
1) Right-click on the test/java folder and Select New > Other. A new window will display and then select TestNG Class from it.
2) Enter Class name as any of your choices, but for now, take it as NewTest. You are welcome to select any of the Annotations as well. Click on the Finish button.
3) Project explorer and the Eclipse window will look like this now.
Note: Test cases reside under the src > test > java > PackageName will only be considered as a test by Maven, rest will be ignored if you put your test cases in some other folder.
Step 10: Run the Selenium Test with Maven Build
It is a long journey to reach this point. Everything is set up and the Selenium project is ready to build.
1) Right-click on the pom.xml and Select Run As > Maven Test. It will take a few seconds to start the process.
2) Once the run is complete, the result will be displayed in the console window of the Eclipse IDE.
Note: Only in case of successful test pass, Maven will do the build. In case of any failure, the Maven build will also fail.