hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Guide to build Selenium Project

How to build a Selenium Project ?


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 article aims to guide readers how to build and execute Selenium projects.



Important note:- Software to Install - Any IDE(Eclipse), Java, driver executable(chrome,gecko) , Selenium jar file, TestNG jar file & Maven on your machine.


Lets get started & follow the steps,

Step #1: Required Downloads to build a Selenium Project

Step #2: Configuring Selenium using Java Project

Step #3: Sample Selenium Project using Java Project

Step #4: Configuring Selenium using Maven Project

Step #5: Maven pom.xml file

Step #6: Sample Selenium project using Maven Project

 

Step #1: Required Downloads to build 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.


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



  • 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.




Step #2: 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.


  • Java project “Building_MyFirst_Selenium_Project” 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


Click on the latest version available & download jar file


  • 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

  • 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.


Step #3: Sample Selenium Project using Java Project

Lets write a simple code using TestNG Annotation(@Test) to open the Google page & verify the title.

As we have not includes a main method, once you have written the code-->Right click --> Run As --> Test NG Test


Code Snippet

Explanation of Code

Code Line-13 to 22: 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.

Step #4: Configuring Selenium using a Maven Project

Apache Maven is a software project management and comprehension tool. It is built on the concept of the Project 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

  • Verify that the “Maven Project” is created successfully with the name “Building_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.

Step #5: 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 search TestNG & copy the dependency into pom.xml file under the dependency node.


  • Now expand the "Building_Selenium_Project" and double click on pom.xml file to add maven & TestNG dependencies



Code Snippet: POM.XML

  • After couple of minutes verify that all the required Selenium dependencies are added to our project.


Now finally the last step

Step #6: Sample Selenium Project using Maven Project

Once a Maven project is created, under the package create a java “class” & write the same code as used earlier to open the browser & verify the tile

Code Snippet




Explanation of Code

Code Line-17to 25: 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.

Conclusion:

Thus, building and executing a Selenium project can be a bit overwhelming at start. Hope you found this article helpful and soon you will be building your own projects flawlessly in no time.

Until next time, happy automating!!!









27 views0 comments

Recent Posts

See All