In this Selenium WebDriver and Java based tutorial, we will understand- What exactly is Selenium? Is Selenium a Framework or Tool? and How to start with Selenium Framework using Java and Chrome binary. We will also understand, How to use Selenium as a Software Tester or Developer, to Automate things on the fly. So, Let’s get started –
In one line, Selenium is fundamentally a portable framework, which is majorly used to test the functionality of Web applications. To start with Selenium Framework, you don’t really need to learn any new Test scripting language (such as with Selenium IDE), you can test your Web Application in any of these programming languages –
- C#,
- Java,
- Python,
- Ruby,
- Groovy,
- Perl,
- PHP, and
- Scala.
Now, we know What is Selenium Framework and with which programming languages can it be used. Now, we will understand, How to start with Selenium Framerok Java –
Selenium Framework Java
We can optimize the performance and verify the quality of our Java web applications, whether is developed with Servlets-JSP, Spring MVC or Spring Boot. We can write a number of Test Cases using Selenium framework for Java based web applications. We can also execute on any platform including Windows / macOS / Linux.
Pre-requisites for Selenium Framework with Java:
In order to write and develop Automation Testing Scripts with Java, you should have these things installed in your Laptop/Computer system –
- Java JDK,
- An IDE (such as Eclipse / IntelliJ / similar),
- Maven,
- Web browser (such as Chrome / Firefox / similar).
1) Java »
To develop and run your first automation script, you’ll need all the above 4 things. Now, to check whether your system already has Java or not, you need to open your terminal by pressing Windows key+R and Type cmd and hit Enter in windows.
Or, if you’re working with macOS, click the Small magnifying glass icon in your Menu bar (or press Command+Space). When the Spotlight Search bar pops up on your screen, type terminal.app and hit Return or Enter key.
Once the terminal gets opened, type javac –version, and hit the Enter key. If you are getting the response containing message “java version “1.8.0_121“ or like “Java 16“, then your system already has Java. If you’re not getting such a response, then go to the official Oracle webpage, download and install the Java 8 or any latest Java JDK in your system.
2) Java » IDE »
In order to work with the Selenium framework and Java, you’ll need an Environment or an IDE (Integrated Development Environment) such as Eclipse. These are the tools using which we will develop and write test scripts in Selenium with Java programming.
You can check the latest release of Eclipse from here, or also choose any other IDE such as- Netbeans, IntelliJ, Visual Studio, or anyone else to get started with Selenium.
3) Java » IDE » Maven »
If you’re using Eclipse as an IDE, and it’s version is above 4.5 (Eclipse Mars), you already have maven installed in your Eclipse. In case, if you are working with an older version of Eclipse, just search for- How to install maven on Eclipse, and you’ll get the steps of it.
In case, if you’re not using Eclipse, no problem at all. Just go to your New Project menu tab and search for Java Web Project or Maven project, and follow the below “Getting Started…” guide for further configurations.
4) Java » IDE » Maven » Web browser »
Chances are, your system might already have at least any of these Web browsers- Internet Explorer, Chrome, Firefox, Safari. If not, search for “Download YOUR_BROWSER_NAME” and install it as per your Operating System.
Our Configurations for further Selenium guides –
In this Selenium Framework tutorial series, we are working with these Environments, Platforms and Applications –
- Java: JDK 16
- IDE: Oxygen.2 Release (4.7.2)
- Maven: (In-build with IDE)
- Browsers: Analysing with 3 Web browsers –
1) Chrome v89.0.4389.128 (Official Build),
2) Firefox v87.0,
3) Internet Explorer v11.0.18362.1256 - OS: Both- macOS Sierra 10.12.6 And Windows 10.
Creating Project with Selenium Framework – Java
To build your first project with Selenium and Java, you must have the above 4 things installed and configured in your system. If everything is done, Follow the below instructions step by step. In this tutorial series, we are going to work with :
Step 1) Open IDE and Create a Maven project :
Let’s start with the very first step, i.e. Building a workspace and a Maven project in our Eclipse IDE. We are going with a Maven based Java Web Project, as Maven helps to include and maintain the dependencies (or say libraries) very easily –
So far, we have chosen the Project type as a Maven Project, by going through File > New > Others > Select Wizard. Now, when you’ll select Maven Project and click on Next, you will get the below SS –
Here’s our SeleniumFrameworkJavaProject‘s source code file, libraries and package structure. There are a number of files and directories in it. But first we will take a look at an important file in this set, i.e. the pom.xml. It contains all the information about the Dependencies (or in simple words- the External Libraries) in an organized and manageable manner.
To open that xml document file, just double click on it, and you’ll get this-
Now click on the highlighted tab pom.xml, and an XML document will be in front of you. What pom.xml majorly contains is, meta information of all the dependencies (or external libraries) of your project in the form of XML tags, something like this –
<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>SeleniumFrameworkJavaProject</groupId> <artifactId>SeleniumFrameworkJavaProject</artifactId> <version>0.0.1-SNAPSHOT</version> </project>
Apart from dependencies sometimes this XML document file also contains some internal configuration-related code.
The above code is generated by the IDE, by processing your inputs, such as Project’s Group Id, Artifact Id etc. If you’re not aware of these terms, then first have a look at these.
Side note on GroupId, ArtifactId, and the Version
groupID : The value contained by this property, helps to uniquely identify your project across all of your projects. Whenever creating a group ID, you should follow Java’s package name rules. Some better examples are with domain names that you own – com.shubhamklogic.seleniumproject or SeleniumFrameworkJavaProject will also work fine.
artifactId : It’s the name of the jar but without mentioning the version. If you created it, then you can choose whatever name you want with lowercase letters and no strange symbols. Eg- automate.fb or SeleniumFrameworkJavaProject would be ok.
eg. maven, commons-math
version : It will help you in situations when you’ll distribute the Final product. At that time, you can choose any typical version with numbers and dots (such as 3.0, 1.8, 1.0, 1 , etc). But remember, Never use dates as versions (because, they are usually associated with something called ‘SNAPSHOT builds’).
Adding Libraries of Selenium Framework in Java Project
So far, we have fullfilled all the pre-requisites for getting started, and created a basic Selenium Framework Java-based project. Now we will include the dependencies from some official online locations.
To include the Selenium framework dependencies (or libraries), Visit https://mvnrepository.com and search for “Selenium Framework Java“, and click on the very first search result.
You’ll get a table of Selenium Framework Java Library’s Versions with the released Dates. Then click on any NON-ALPHA and NON-BETA stable release to copy the Dependency related meta information and then paste into our pom.xml file like this-
<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>SeleniumFrameworkJavaProject</groupId> <artifactId>SeleniumFrameworkJavaProject</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- The Selenium Framework Dependency / Library --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.14.0</version> </dependency> <!-- The junit framework dependency --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> </project>
As you can see, in above XML code of pom.xml file, both the libraries of Selenium Framework and JUnit are been included inside the dependencies tag. As you’ll save the file, the libraries would be automatically downloaded and included into your project’s maven library’s directory, something like this –
Wrapping Up
It’s one of the many advantages of using Maven, as it automatically downloaded and added the dependencies at desired locations of our project. If you won’t go with such a build tool, then you might need to explicitly download all of these jar files, include them manually, and configure the path as well.
In the next Selenium Guide, we will start writing the Automation Test Scripts. We will understand, how to target web elements and how to manipulate them on the fly.
So, don’t forget to check the next Selenium Framework Java Guides in the continuation of this post. Also, use the below icons and share the guide with other Geeks, if you found this Selenium Introductory Guide helpful!