Setup Selenium Test Automation Project Using Java

In this article, we will look at how to setup selenium test automation project using java.

Pre-requisite

  • Java JDK needs to be installed
  • Eclipse IDE needs to be installed
  • Selenium jars in case you are creating a normal java project inside the Eclipse IDE. Most companies and Automation testers prefer to use maven project.
  • Respective Browser drivers downloaded or if you want to use Webdriver Manager Dependency then don't need to install the browser drivers.

Steps

Open the Eclipse IDE.

Click File new-->Project---->Other--->Expand the maven folder--->select Maven Project

selenium Test Automation project using Java

Choose the desired location of the project or you can set the default location of the project.

Check the check box create a simple project(skip archetype selection)

selenium Test Automation project using Java

If you do not check this check box the Maven will ask you to select the archetype solution which you want to build.

selenium Test Automation project using Java

selenium Test Automation project using Java

Archetype is nothing but a default template of a predefined project solution. For example, if you want a create a spring/JDBC project then maven will have a predefined archetype template created for this project solution. If we select this archetype all the config and folder settings will be default built by maven itself. As a programmer we just need to go do the coding required for that project.

Click next button.

Provide the group Id and Artifact Id and click finish.

selenium Test Automation project using Java

Naming conventions for GroupId is a companyName and Artifact Id is specific project name inside the company.

Maven project will be created. It will take a few minutes.

A pom.xml file will be generated which contains all the metadata regarding the project.

We can add the dependencies and plugins needed for your maven project inside the pom.xml file.

A maven project provides a default folder structure that is easy to maintain. These folders will get automatically generated while we create the maven project.

  • src/main/java----> Contains all the java source code for the project
  • src/test/java------>Contains all the tests(unit/Automation tests).
  • src/main/resources-->configuration files and others files used.
  • src/test/resources----->configuration files and others used by tests.

selenium Test Automation project using Java

Now open the Pom.xml file.

Go to the maven central repository site(https://mvnrepository.com/)

Search for the selenium dependency. Select the version of selenium. Copy the dependency code clip.

selenium Test Automation project using Java

Inside the pom.xml file create a dependencies tag.

Paste the code clip inside the dependencies tag.

Search for the TestNG dependency. elect the version of testNG. Copy the dependency code clip.

selenium Test Automation project using Java

Note: If the scope is compile then Change the scope to test (ie <scope>test<scope>) and paste the TestNG dependency code.

This defines the dependency scope, the Default value is compile. So we change it to test meaning that the dependency should be available for test compilation and run time.

Paste the code clip inside the dependencies tag.

Now save the project. The project will start building. Now maven will download the selenium and TestNG jar files from a central repository and place it inside your local repository. You will find Maven dependencies folder with all the jar files downloaded. The local repository will be available in your system %USER_HOME%/. m2 folder.

selenium Test Automation project using Java

Right-click your project and select Maven Update Project and update your project.

Note: You need an Internet connection while you are working on Maven project because maven uses the internet when it downloads jars from a central repository to your local repository.

Dependencies tag Example:

<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.6.0</version>
</dependency>
 <!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.6.1</version>
    <scope>test</scope>
</dependency>
</dependencies>

Now you will be able to write selenium tests using TestNG framework inside your project.

But if you want to run the TestNG tests inside your Eclipse IDE you will need to install the TestNG plugin inside your Eclipse IDE.

For that follow the below steps

  • Go to help menu ---->Select the eclipse marketplace option
  • Choose the "Install New Software" option
  • In the Eclipse Install dialog box
  • Enter http:/beust.com/eclipse
  • It will open a dialog box. In that dialog box enter "TestNG" in the Name Text Field. Click on Add button.
  • Select the TestNG check box and click next and then again next. And then agree on the End user agreement.
  • Click Finish
  • It will ask for a warning click install anyway and it will prompt for restart of eclipse. After restart then the TestNG will get added to the Eclipse IDE.
  • For confirming that testNG has been installed right click on the empty area inside the script editor and go to run as --> you will find run as TestNG test option. This will confirm that TestNG plugin has been successfully installed in your Eclipse IDE.

Hope this article will help you in setting up Test Automation Project using Java

Thanks

Happy learning...............


Similar Articles