Maven | Installation and project creation in Selenium. (Part-1)

Maven is basically a build automation tool and used to manage project lifecycle and project dependencies.

Maven Key Features

  1. First of all, it increases the reusability.
  2. Handle dependencies to manage library files.
  3. It manages project compilation, distribution, and build
  4. It has a central repository to manage library and jar files.
  5. We can easily create jar/war packages for deployment or distribution.

Maven Download

Follow below steps to install the Maven.

  1. Open Eclipse Application
  2. Navigate to Help
  3. Click on ‘Install New Software
  4. In the work with text field type URL “https://www.eclipse.org/downloads/” and press Enter key.
  5. Click on the Next button and wait to complete the installation.
  6. Restart the Eclipse (if prompt).

Maven Project creation

  • File > New > Project
  • Search for Maven Project and click Next
  • Select one of the required Archetype
Select Archetype for Maven Project.
  • Enter Group Id, should be unique based on the organization or individual name. And it would be basically the reverse of the website like com.allinoneblogs or com.project.
  • Enter Artifact Id, your project name for example MavenDemo1
  • Group ID and Artifact ID will create a package in the project under src/main/java and src/test/java folders.
    • src/main/java: It contains all the classes or methods for the different modules.
    • src/test/java: It contains the test methods marked @Test annotation which would be considered as Test cases.
  • After Finish it a new project will be created and the structure will be like as:
Maven default Project Structure.
  • Target folder: All the compiled versions of the project will be saved in the target folder. Will learn it later stage.
  • Pom.xml: This file contains project information as well as all the dependent library files, required for the project.

Pom.xml File

  • A pom.xml file is added to the project, this file contains all the necessary project information, library files location, and configuration settings.
    • Group Id, Artifact Id
    • Version
    • Packaging format like .jar after building the code
    • Dependency configuration to import library files. By default, JUnit is added to the file. We can change it based on the requirement.
pom.xml file default structure.

Start Coding

Now, we can start our coding. Suppose we have to use the TestNG framework and write a program to launch a web URL like below:

  1. Open Chrome Browser
  2. Launch URL “https://google.com”
  3. Search any keyword like “Computer”

To start the code we need two libraries Selenium Java for Web Driver handling and TestNG. So, the first, step to find the latest version and add dependencies for these library files.

Steps to locate and add the dependency

  1. Navigate to “https://mvnrepository.com/”, where we can find all the library files.
  2. Now, in the search field type name of required library files like “Selenium Java” and press enter.
  3. Always keep in mind that we have to use the link which has more usage numbers.

4. Click on the “Selenium Java” link and on the next page again look for the latest version which has the highest usage count. It shows the stability of that version. Now click on version “3.141.59”

5. Now, we redirected to the page from where we have to copy the code.

6. Now, open the pom.xml file and add the entire code under the “<dependencies> </dependencies>” tag. And save the file.

7. If you navigate to the Configure Build Path dialog box you will observe that the dependency is added there also under “Maven Dependencies

Creating Test Cases

Now we can create test cases as we normally used to do under “src/test/java/” folder based on the requirement as below:

  1. Open Chrome Browser
  2. Launch URL “https://google.com”
  3. Search any keyword like “Computer”

TestA.java

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 */
public class TestA 
{
	static WebDriver driver;
	
	@Test
	public void launchBrowser()
	{
		System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
		driver=new ChromeDriver();
	}
	
	@Test (dependsOnMethods = "launchBrowser")
	public void launchURL()
	{
		driver.get("https://google.com");
		
		System.out.println("URL lanuched successfully.");
	}
	
	@Test(dependsOnMethods = "launchURL")
	public void searchKeyword()
	{
		driver.findElement(By.name("q")).sendKeys("Computer"+Keys.ENTER);
	}	
}

Maven:


Leave a Comment

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

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.