What is Selenium and what it is used for?
Selenium is an open-source framework that is used to automate the testing process over web applications. The interface allows writing test scripts in various programming languages on web applications spread across several platforms and browsers.
How to use Selenium with Java?
Before we start, we will need :
Java (Installed)
Eclipse IDE
WebDriver (Downloaded)
Setting up the project
First, we need to set up the project so we can use Selenium.
Create a Maven project
In this tutorial, we’re using Maven to import external dependencies. We start by creating a new Maven project, which will automatically create some of the directories and files we will need.
To create a new Maven project in Eclipse:
Click the menu option File > New > Project
In the New project dialog box, select Maven on the left (if it isn’t already selected)
Make sure that the Project SDK is selected (for instance, Java 1.8) and click Next
Specify a GroupId and ArtifactId for your project and click Next
Specify a Project name and Project location for your project (if needed) and click Finish
You will see pom.xml file at the end of the project structure. You can add all the Maven Dependencies we need for Selenium in that file.
To add the dependencies you can simply got to Maven Repositories website and search for dependency.
Selenium Dependency :
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.12.1</version> </dependency>
After adding dependencies, you can just right click on project and Update Maven which will add all files required to the project.
What is Web Driver and how to use it?
Selenium WebDriver tool is used to automate web application testing to verify that it works as expected. It supports many browsers such as Firefox, Chrome, IE, and Safari.
In our case we are using Google Chrome browser so we need a Chrome Driver which you download below :
Select the correct version of driver which matches with your Google Chrome browser version.
After downloading the driver, put it under a folder because we will need to use the path of where the driver is located.
Now follow the below steps :
Create a new package under src/main/java and give it a name Selenium.
Create a java class under Selenium package and name it Demo.java.
In the above file, we are first setting the property to use the webdriver which we downloaded before. So simply copy the path of your webdriver location and paste in System.setProperty.
driver.manage will be used to all the operations to control the behaviour of the browser window.
driver.get will open the website on which you want to do testing.
driver.close will close the current browser window after completing the test.
Comments