How to Build and Execute Selenium Projects
By Chaitanya Pujari, Community Contributor - February 12, 2023
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
Read More: How to configure Selenium in 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”
- Select “Java Project” and click on Next.
- Enter the “Project Name”. In this example, the name is “Building_a_Selenium_Project-BrowserStack”. Click on Finish.
- Java project “Building_a_Selenium_Project-BrowserStack” is successfully created in the package explorer of Eclipse.
- 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”.
- Now, let’s download “TestNG” Jars Click Here to download TestNG Jar
- 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…”
- Click on “Add External JARs…” This will add the downloaded jar to the Selenium project
- Select the download jar “selenium-server-standalone” and “TestNG” and click on Open.
- Verify that the “selenium-server-standalone” and “TestNG” jars are added to the java build path. Then click on Apply and Close.
- Verify that the downloaded jar is added under “Referenced Libraries”
- Right-click on “src” and create a “package”. Once a package is created, create a java “class” under that package.
Now let’s try to create a simple program to validate that our project is successfully configured with Selenium.
Also Read: Selenium WebElement Commands
Sample Selenium Project using Java Project
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”
- 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
- Enter “Group Id” and “Artifact Id” and click on “Finish”.
Also Read: Maven Dependency Management with Selenium
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.
- 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
Element | Description |
packaging | Describes packaging type such as jar |
name | Describes name of the maven project |
URL | Describes URL of the project |
dependencies | Describes dependencies for this project |
dependency | Describes a dependency. It is used inside dependencies |
Scope | Describes 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.
- Click on “Selenium Server” and select the latest stable version.
- Copy the dependency into pom.xml file under the dependency node.
Now let’s look at the structure of POM.xml.
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.
Now let’s try to create a simple program to validate that our project is successfully configured with Selenium.
Read More: Best Practices for Selenium Test Automation
Sample Selenium Project using Maven Project
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.