App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide How to Build and Execute Selenium Projects

How to Build and Execute Selenium Projects

By Chaitanya Pujari, Community Contributor -

Building and executing Selenium projects is a very important part of test automation. It helps to execute tests, analyze reports and report bugs faster. Needless to say, this helps immensely in delivering quality products to customers quicker, thus keeping them happier. 

This tutorial will teach readers how to build and execute Selenium projects. 

Building a Selenium project

A Selenium project can be built in multiple ways. This article will demonstrate how to build a Selenium project using a “Java Project” as well as a “Maven Project”. To build a Selenium project the following software must be installed:

  • Java: Java or JDK needs to be installed as we are using Java to build a Selenium project. 

             Click here to download & Install Java

  • Maven: Maven needs to be installed as we are using a Maven project to build a Selenium project. 

             Click here to Download & Install Maven

  • Eclipse: Eclipse is an IDE (Integrated Development Environment) that needs to be installed. One can even download eclipse packages and use them. Using an IDE helps create, execute and maintain tests or projects efficiently. If you are not comfortable with Eclipse you can use some other IDE as well. 

            Click here to Download Eclipse

Configuring Selenium using a Java Project

One of the simplest ways to build a Selenium project is by creating a Java project. To do so, first, all the necessary Selenium jars must be downloaded manually.

Note: Steps in this tutorial are written using Eclipse.

  • Click on “File” in the Eclipse navigation menu. Select/Hover on “New” and click on “Project”How to build and execute selenium projects
  • Select “Java Project” and click on Next.

How to build and execute selenium projects

  • Enter the “Project Name”. In this example, the name is “Building_a_Selenium_Project-BrowserStack”. Click on Finish.How to build and execute selenium projects
  • Java project “Building_a_Selenium_Project-BrowserStack” is successfully created in the package explorer of Eclipse.

How to build and execute selenium projects

  • Once the Java project is created, we need to download the necessary Selenium jars as well as a unit testing framework such as “TestNG” jars manually. Go to Selenium downloads page and click on “Latest Stable Version”.How to build and execute selenium projects

How to build and execute selenium projects

  • Once the Selenium server standalone jar is downloaded, add it to the Selenium project. Right-click on the project created. Hover the mouse on “Build Path” and click on “Configure Build Path…”

How to build and execute selenium projects

  • Click on “Add External JARs…” This will add the downloaded jar to the Selenium project

How to build and execute selenium projects

  • Select the download jar “selenium-server-standalone” and “TestNG” and click on Open.

How to build and execute selenium projects

 

  • Verify that the “selenium-server-standalone” and “TestNG” jars are added to the java build path. Then click on Apply and Close.

How to build and execute selenium projects

  •  Verify that the downloaded jar is added under “Referenced Libraries”

How to build and execute selenium projects

  • Right-click on “src” and create a “package”. Once a package is created, create a java “class” under that package.

How to Build and Execute Selenium Projects

Now let’s try to create a simple program to validate that our project is successfully configured with Selenium.

Sample Selenium Project using Java Project

How to Build and Execute Selenium Projects

Code Snippet

package browserStackTutorials;

import static org.testng.Assert.assertEquals;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

public class BuildingSeleniumProject {

@Test

public void buildingTestMethod() {

System.setProperty("webdriver.chrome.driver", " C:\BrowserStack\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.navigate().to("https://www.browserstack.com/");

String verifyBrowserStackTitle = driver.getTitle();

assertEquals("Most Reliable App & Cross Browser Testing Platform | BrowserStack",verifyBrowserStackTitle);

}

}

Explanation of Code

Code Line-13 to 15: It verifies the title of the website by navigating to the website and getting the actual website title. Then it compares the actual title with the expected title.

Configuring Selenium using a Maven Project

Apache Maven is a software project management and comprehension tool. It is built on the concept of the Page Object Model (POM). Maven can manage a project’s build and reporting from a central piece of information. 

Now let’s see how to build a Selenium project using Maven.

Note: Steps in this tutorial are written using Eclipse.

  • Click on “File” in the Eclipse navigation menu. Click on “Others” and click on “Project”

How to Build and Execute Selenium Projects

  • Expand the “Maven” module. Select “Maven Project” and click on “Next”
  • Click on “Next”
  • Select “maven-archetype-quickstart” and click on “Next”. Maven archetypes are project templates which can be generated for users by Maven

How to Build and Execute Selenium Projects

  • Enter “Group Id” and “Artifact Id” and click on “Finish”.

Group Id: Will identify your project uniquely among all projects. It must follow the package name rules. This means that it must contain, at least, the domain name.

Artifact Id: It is the name of the jar without a version. If you create the jar then you can choose whatever name you want with lowercase letters.

How to Build and Execute Selenium Projects

  • Verify that the “Maven Project” is created successfully with the name “Building_a_selenium_project”

Earlier we have configured Selenium using a Java project by downloading all the necessary Selenium jars. Then we have added the downloaded jars and placed them into the Java project through the configure build path. 

This is not required in a Maven project. In this case, we must define the dependencies required to configure this Maven project with Selenium.

Maven pom.xml file

POM is stated or defined as a Project Object Model. The pom.xml file contains information related to the project such as configuration information for Maven to build the project. This includes dependencies, build directory, source directory, etc. Maven reads the pom.xml file, then executes the tests.

pom.xml file with elements

ElementDescription
packagingDescribes packaging type such as jar
nameDescribes name of the maven project
URLDescribes URL of the project
dependenciesDescribes dependencies for this project
dependencyDescribes a dependency. It is used inside dependencies
ScopeDescribes scope for this maven project

Now let’s see how to configure the Maven project with Selenium.

  • Navigate to “www.mvnrepository.com” and search for “selenium server”. Click on Search.

How to Build and Execute Selenium Projects

  • Click on “Selenium Server” and select the latest stable version.

How to Build and Execute Selenium Projects

  • Copy the dependency into pom.xml file under the dependency node.

How to Build and Execute Selenium Projects

Now let’s look at the structure of POM.xml.

How to Build and Execute Selenium Projects

Code Snippet: POM.XML

<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>BrowserStack</groupId>

<artifactId>Building_a_selenium_project</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>Building_a_selenium_project</name>

<url>http://maven.apache.org</url>


<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>


<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-server</artifactId>

<version>3.141.59</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>6.14.3</version>

<scope>test</scope>

</dependency>

</dependencies>

</project>
  • Now let’s verify that all the required Selenium dependencies are added to our project.

How to Build and Execute Selenium Projects

Now let’s try to create a simple program to validate that our project is successfully configured with Selenium.

Sample Selenium Project using Maven Project

How to Build and Execute Selenium Projects

Code Snippet

package BrowserStack.Building_a_selenium_project;

import static org.testng.Assert.assertEquals;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

public class BrowserStack_MavenProject {

@Test

public void buildingTestMethod() {

System.setProperty("webdriver.chrome.driver", "C:\\BrowserStack\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.navigate().to("https://www.browserstack.com/");

String verifyBrowserStackTitle = driver.getTitle();

assertEquals("Most Reliable App & Cross Browser Testing Platform | BrowserStack",verifyBrowserStackTitle);

}

}

Explanation of Code

Code Line-11 to 13: It verifies the title of the website by navigating to the website and getting the actual website title. Then, it compares the actual title with the expected title.

Thus, building and executing a Selenium project can be a bit complicated. But follow the instructions in these articles and you will be building your own projects flawlessly in no time. 

Run Selenium Tests on Real Devices for Free

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

How to Automate TestNG in Selenium

Learn Selenium with Java to run Automated Tests

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.