Book Image

Hands-On Automation Testing with Java for Beginners

Book Image

Hands-On Automation Testing with Java for Beginners

Overview of this book

Java is one of the most commonly-used software languages by programmers and developers. Are you from a non-technical background and looking to master Java for your automation needs? Then Hands-On Automation Testing with Java for Beginners is for you. This book provides you with efficient techniques to effectively handle Java-related automation projects. You will learn how to handle strings and their functions in Java. As you make your way through the book, you will get to grips with classes and objects, along with their uses. In the concluding chapters, you will learn about the importance of inheritance and exceptions with practical examples. By the end of this book, you will have gained comprehensive knowledge of Java.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Working with the Java editor tools


Here, we are going to look at the editor tool that we will be using to write our Java code. There are many tools available in the market that can act as a new Java editor, but I personally prefer using Eclipse. It comes with lot of in-built features and syntax additions. We will see other advantages of Eclipse as we progress. Some of the advantages cannot be discussed theoretically, so once we progress and start coding practically, we will understand how it prompts us to write a correct syntax. So, throughout the course of book, we will write all our Java code in the Eclipse IDE editor.

First, we download the Eclipse IDE editor and take a look at the interface that it has to offer. The following link will take us to the official website of Eclipse: https://www.eclipse.org/downloads/. The website will look like the one shown in the following screenshot:

When we click on Download Packages below the Download 64 bit button, it takes us to the following page:

We will be using Eclipse IDE for Java EE Developers. We can select between 32-bit or 64-bit, depending on the system we are working on. We already know how to check if our system is 32-bit or 64-bit, by accessing Control Panel and following the instruction given during the installation stage.

An important thing we need to ensure is that our Java version is compatible with the IDE we are downloading. If our system is 32-bit and we download 64-bit Java, then Eclipse will not open. So ensure that our system, Java, and Eclipse versions are all on the same line.

The file will be downloaded in a ZIP folder form and we can extract it. The following screenshot shows folders that will be present in the eclipse folder:

 

If we double-click on the eclipse.exe file, the Eclipse UI will open.

If we want to write our Java code, we need to create a Java project. Right-click on the white pane window, which is on the left side, and click on New | Project. This is shown in the following screenshot:

We get a prompt to tell to Eclipse what kind of project we are working on, as shown in the following screenshot:

As we can see, a lot of different frameworks are available, such as Java Project, C/C++, and Android, but we are interested only in the Java project, so we select Java Project, and click on Next. We will get a New Java Project window where we will fill in all the information for our new project, as shown in the following screenshot:

We select a project name for our Java project that we will be creating. We'll name our first project coreJavaTraining. Click on Next and then Finish. We will get a prompt asking us if we want to Open Associated Perspective?; select No:

This will successfully create coreJavaTraining. Within the project, there is a source folder that is automatically created. This means we need to write our classes inside this source folder. What exactly are the classes? Basically, all Java code is written inside a class. When we write Java in Notepad, we open Notepad, write the Java code, and save that particular Notepad file with the .java extension. But in Eclipse, all that work is done by this tool itself. So all we need to do is create a class and that will give us a proper template. We right-click on the source (src) file and click on New | Class. We will get a Java Class prompt where we will be entering the class name. We will name this class Firstclass and ensure that we select the public static void main (String[] args) check box; we will discuss the importance of this later. And, finally, we click on Finish. This is shown in the following screenshot:

We see that the in-built hierarchy is already created for us, as Eclipse creates an outer template. We can see in the editor that a class and public static void main is present. All this is created by the Eclipse tool. If we were writing normally on Notepad without using any tool, we would need to create the template. But in Eclipse, all we need to do is give the class name. The code we will be typing will be encapsulated in the class; that is, inside the brackets of the class. Whatever name we use while creating the file will be the class name.

All the execution of the code will be placed in public static void main because whenever we run this file, Java control will go directly to this block. It will not touch any of the code written outside public static void main. In short, we write the code outside the public static void main block, but ultimately we need to call that code inside the block. This is because only the main block is responsible for the execution of our Java code. That is why we write public static void main. We will learn about the public and void keywords as we move further in this book because it is too early to get into the details of these now. We can see the template in the following screenshot:

The class created by the Eclipse tool