How to build a Selenium Maven project
Maven is a central repository for Java-based projects. In this blog, I will show how to build a simple Selenium test project using Maven as the build automation tool. One of the most important advantages of using Maven is that it is easy to configure the dependencies required for building, testing and running code using the pom.xml file
Let us go through the steps
- You can use any IDE of your choice. For this blog, I will be using Eclipse. The same configuration can be done in Intellij
- Create a New project. The below wizard would appear if you are working with Eclipse
3. Select Maven project. Click on Next. Select maven-archetype-quickstart in the Artifact Id as shown in the image
4. Click on Next. Enter the GroupID and ArtifactId-Name of your project. Click on Finish
5. A new project with the name would be created and displayed in the Package Explorer as shown in the image
6. Double click on the pom.xml file. In this file, we can add all our dependencies. For adding the dependencies, we shall go to the maven repository by following this url:-https://mvnrepository.com/
7. Search for Selenium
8. Click on Selenium Java. Select the latest Selenium Java version
9. Copy the Maven dependency code
10. Paste this in the dependencies section of the pom.xml file in Eclipse
11. We will now create a new class file. Right click on the src/test/java package. Click on New. Click on Class. Name your class file
12. Write the following lines of code
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.testng.Assert;
public class check {
public static void main(String[] args) {
//Creating webdriver instance
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
//Open web application
driver.get("https://elenastepuro.github.io/test_env/index.html");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
//Enter data into textboxes
driver.findElement(By.id("change_id")).sendKeys("test_id");
driver.findElement(By.id("change_className")).sendKeys("test_class");
driver.findElement(By.id("Submit")).click();
//driver.findElement(By.id("change_id")).sendKeys("updated_test_id");
//driver.findElement(By.id("change_className")).sendKeys("updated_test_class");
}
}
13. Execute the code
And it’s done. During execution, you will be able to see the Chrome browser opening the web application, entering the required data. This is a basic instance of how to create a simple Selenium Maven project. This could be further enhanced by using the TestNG/Junit framework