Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Hands-On Automation Testing with Java for Beginners
Hands-On Automation Testing with Java for Beginners

Hands-On Automation Testing with Java for Beginners: Build automation testing frameworks from scratch with Java

€19.99 €13.98
Book Sep 2018 156 pages 1st Edition
eBook
€19.99 €13.98
Print
€24.99
Subscription
€14.99 Monthly
eBook
€19.99 €13.98
Print
€24.99
Subscription
€14.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Sep 29, 2018
Length 156 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781789534603
Category :
Table of content icon View table of contents Preview book icon Preview Book

Hands-On Automation Testing with Java for Beginners

Chapter 1. First Programming Steps in Java

Welcome to Hands-On Automation Testing with Java for Beginners. This is the only book you will find on the internet that teaches every topic needed to emerge as a strong Java automation tester. It contains easy teaching and simple techniques to effectively handle Java-related automation/projects. Considering that we will cover each and every topic of core Java, with detailed explanations, this will really help us develop and grade our own Java automation projects.

All the core Java concepts are explained from scratch. We do not assume that readers have any prerequisite knowledge, so we consider all readers to be from a non-coding background, and we teach each and also support them with examples that we will use in real time. Hence, we will not stick to only theories.

When you check for courses in the market, you should try to learn one new concept. You just see three lines of definition followed by examples; that's all. But here, we will understand why, when, and where we are using the object-oriented programming systems (OOPS) concept in Java. There will also be proper programming examples, showing a particular OOPS concept in real-time usage. In this way, our book will drive through real-time projects; it's all about practical learning. This will come into play when we start with Java collections, such as core Java, which is one of the main concepts in our book since it is definitely required for you to start your basics and to develop an automation framework in your workplace. Also, since Java collections is one of the core parts, throughout the entire book we will take utmost care in providing all the necessary practical scenarios for each Java collection that we will discuss.

We will be working on tricky Java programs, looking at printouts, prime numbers, Fibonacci series, and pyramids. We will sort out print in descending order, looking at array matrices, and printing the maximum column numbers. This book will provide you with detailed strategies and tips that you will need to use when approaching and designing a logic for these programs. This will help you to think beyond the boundaries and get the logic that you will need to write difficult Java programs.

Programs discussed in this book derive from commonly asked questions during interviews in many companies. You will receive help with those questions, with detailed solutions, and the methodology to approach that logic. So, this book is mainly focused on core Java. We are not dealing with swings and buttons, which are out of scope for Java learning in this book.

In this book, we will be learning core Java, collections, and other concepts such as loops, classes, and arrays . These are more than enough for you to start and develop a Java project. Whichever field you are put in, the knowledge gained from this book will help you start testing in the automation projects right away.

This chapter will cover the following concepts:

  • An introduction to Java and its installation
  • Working with the Java editor tools
  • Writing your first executable program in Java

An introduction to Java and its installation


The first thing that comes to mind when we speak about Java is that it is platform-independent. This feature has made Java a hot programming tool in the market. So what exactly does platform-independent mean?

The code that we write is independent of the environment; be it Windows, Unix, Linux, or Solaris. Basically, when we write a Java program, the Java compiler converts the program to bytecode. When we run the Java code, the Java compiler converts the entire programming code to bytecode. For example, we are working on a Windows machine. When we run the program and the Java compiler runs and creates bytecode for us, this bytecode can be taken and executed on any other platform, such as Linux, macOS, and Unix. So this means we are developing a bytecode in Windows, and that bytecode is runnable on any other platform. So this is what we mean by platform-independent.

This is a really cool feature that we have in Java programming. Whenever you ask someone to download Java, the first question you will be asked is, is it JDK or JRE? People tend to get confused between these two terminologies. We need to be clear about this before we start downloading and configuring Java in our machines. Let's have a look at both JRE and JDK:

  • JRE stands for Java Runtime Environment: It is responsible for running our Java program. If our aim is to just run a plain Java core code then JRE is sufficient.
  • JDK stands for Java Development Kit: It is used to debug our Java code, or if we want to have the Java documents or something similar.

JDK contains JRE, Java documentation, and debugging tools, and other cool things. It's an entire Java kit from which we will get all of its components. So it's up to us what we download, but I would recommend we only have JDK to be on the safe side. If we want to just practice and run our program, JRE is also sufficient, but let's stick to JDK.

So now let's go back and download Java from the internet and try to configure it in our machines. To download Java, navigate to the following page: https://java.com/en/download/. When you click on the Free Java Download button, as shown in the following screenshot, the JRE version will be downloaded:

But we intend to use JDK for our program, so navigate to the following site: https://www.oracle.com/technetwork/java/javase/downloads/index.html. Here, there are multiple versions of JDK. The latest version out in the market right now is Java SE 10.0.2. Click on DOWNLOAD, as shown in the following screenshot, so that all the components are downloaded and configured in our machine:

It takes a while because there are quite a few steps to configure it. Keep clicking on Next so that Java is successfully configured in our machine. To check whether Java is installed in our machine, navigate to C:\Program Files. If we find the folder named Java there, it means that Java has been successfully installed in our machine. The Java folder is shown in the following screenshot:

An important point to remember here is that if we are on 64-bit, only then will we see this Java folder in Program Files. If our machine is on 32-bit, then we need to go back to Program Files (x86) to get the Java folder.

We can check our system type by going to Control Panel and clicking on System. The system I am working on is 64-bit, as displayed in the following screenshot:

After it is successfully downloaded, we enter the Java folder and observe that both JDK and JRE have been downloaded. We enter the JDK folder and copy the entire file path. We are doing this because we need to set environment variables. Setting environment variables means that we are letting our system know where the Java folder is located.

In our case, the Java folder is in C:/Program Files/Java/JDK, but Windows doesn't know where exactly it is. So to make our system aware of the location, we will place the JDK home path in our system variables. This will help our machine know where the Java folder is located, so that whenever we run our program, it will identify the exact JDK version and run our program. To update this in system environment variables, we copy the entire JDK path. Go to Control Panel, selectSystem and Security, selectSystem, and click on Advanced System Settings. In Advanced System Settings, select Environment Variables. When we click on Environment Variables, the following window appears:

When we click on New in the User variables for Rahul section, we will get a prompt to add a New User Variable. We put in the name as JAVA_HOME, paste the JDK path in the variable value text box, and click on OK, as shown in the following screenshot:

This is how we let our system know where exactly the Java folder is present. We will need to update another variable too. To do that, we go back to the JDK folder and enter the bin folder. We will see multiple .exe files, as shown in the following screenshot:

We copy the bin folder location path and return to our System Properties window. In System Variables, we will see a variable named Path. Double-clicking on it will show us a prompt to Edit System Variable, as shown in the following screenshot:

In the variable value, we go to the end, add a semicolon, and paste the bin folder path. This means we are setting the Path variable to the bin folder. We also create a new variable called JAVA_HOME, pointing to the Java folder. We need to set these two variables before we start working on Java. Once we set both the variables and click on OK, we will successfully set Environment Variables.

If we want to cross-check if the environment variables are properly configured or not, we use Command Prompt. In Command Prompt, we enter java -version and hit Enter. If we get an output as shown in the following screenshot, it means that Java has been successfully configured on our system:

Note

If we run the command before adding the variables, we will see that Java is not recognized. Only after setting the system environment variables will we be able to successfully configure Java.

The previous instructions take care of installing and configuring the system from our end. Next, we will try to download Eclipse, which is a Java editor tool where we write, run, and debug our code. As a pre-requisite to downloading Eclipse, we have to make sure that Java is properly configured in our machine. If any of the installation or configuration steps haven't been done correctly, Eclipse will not install properly.

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

Writing your first executable program in Java


Let's start with our basic coding in this section. If we want to print something in our output, there is a command in Java called System.out.println(). This command will print the output in the console. Let's say we would like to print hello world, and when we run the following code, hello world will be printed in our output console:

Firstclass.java

So let's run the code. There are two methods to run the code:

  • Right-click on the filename in the Project Explorer, click on Run As, and select Java Application.
  • Or, we could click on the run icon given in the toolbar and click on OK on the Save and Launch window. The icon looks like this:

This will run our code and print our output. The following screenshot shows the hello world message on our editor:

Output displaying hello world as per the code

In short, System.out.println() is used to print in our console. We will be using this in almost all our examples for our demonstration of practical examples. If we remove ln from the statement, it will not print the output in the next line.

Let's try printing a statement that will display the output of two print commands on the same line. Here, we add a System.out.println("hi") statement before the hello world statement. If we run the code, the output will be as follows:

Output is displayed on two separate lines

Observe how hi is displayed on one line and then hello world is displayed on the next line. Here, ln displays the output in the next line. If we remove ln from both the statements and run the code, the message will be displayed as follows:

Output is displayed on the same lines

We see, hihello world printed on the same line.

If we write our code, and then we want to check the output partially, we don't need to remove the line of code; all we need to do is just comment it out. We can comment it out by simply putting double slashes (//) at the beginning so that Java will not pick the line. This is shown in the following screenshot:

Commenting using double slashes

If you remove the slashes and the statement is just some random words, then it will throw an error. We will see a red underlined code. This means there is an error at the line with a cross mark. This is shown in the following screenshot:

Error is flagged with a cross mark besides the line number

Add the backslashes again to comment out the error.

Remember, here we are writing our actual code in the main block only. What if we want to print an integer?

Let's say we want to print the number 4. To print it, we first need to store it in a variable and then we will print the variable. So when we print the variable, the value presenting that variable will be printed automatically. For this example, we pick the number 4, and we assign the number in a variable called a. The problem here is that a does not know what data type is being assigned to it. So, we have to explicitly mention that a is an integer. If we do not mention that a is an integer, it throws an error.

In short, we are first creating a variable called a which only acts an integer and then places an integer value 4 into this. The following screenshot illustrates the example we are talking about:

Value 4 is assigned to the variable a

So, with this type of code, we can type it outside, but if we want to print it, we will have to type it in the main block. In this example, we want to print the value of a so we add another System.out.println(a) statement. The editor will throw an error for the variable a in the print statement. To know what the error is, we hover our mouse over the error and a pop up is displayed showing the error with a possible fix, as shown in the following screenshot:

Error details is displayed when the mouse is hovered over it

There will be an option to click on in the error detail. This will automatically resolve the error by adding the required content. This is an amazing feature that the editor has and it is very helpful as we move onto more complex examples.

In our example, when we click on Change 'a' to 'static' in the error detail pop up, static is added to the variable a and we are able to run the code. On running the code, the console will look like this:

Output displaying the value of a as per code

We will be getting into the details of what exactly static is in the later chapters

Summary


In this chapter, we had a short introduction to Java. We then installed and configured the various tools required to work with Java. Moving ahead, we took a look at the editor we will be using to write our own Java code.

Finally, we executed our first example and saw how the editor works and how errors are handled by it. 

In the next chapter, we will learn about some basic concepts, such as strings, variables and methods, and how are they different from each other using code.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn to use Java program logic in application testing
  • Understand various test-driven development concepts with Java tools
  • Master Java with lots of programming examples

Description

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.

What you will learn

Understand the practical usage of Java conditions and loops Write any Java program logic with strategies, tips, and tricks Leverage advanced topics in Java collections to solve Java-related problems Understand and use objects, classes, methods, and functions in Java Build Java automation frameworks from scratch Obtain knowledge of Java object-oriented programming (OOP) concepts with practical implementations

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Sep 29, 2018
Length 156 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781789534603
Category :

Table of Contents

17 Chapters
Title Page Chevron down icon Chevron up icon
Copyright and Credits Chevron down icon Chevron up icon
Packt Upsell Chevron down icon Chevron up icon
Contributors Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
First Programming Steps in Java Chevron down icon Chevron up icon
Understanding Classes, Objects, and Their Usage in Java Chevron down icon Chevron up icon
Handling Strings and Their Functions in Java Chevron down icon Chevron up icon
Building Blocks for Java Programs – Loops and Conditions Chevron down icon Chevron up icon
Everything You Need to Know About Interfaces and Inheritance Chevron down icon Chevron up icon
Learn Everything about Arrays Chevron down icon Chevron up icon
Understanding Date Class and Constructors in Java 11 Chevron down icon Chevron up icon
Importance of the super and this Keywords and Exceptions in Java Chevron down icon Chevron up icon
Understanding the Collections Framework Chevron down icon Chevron up icon
The Importance of the final Keyword, Packages, and Modifiers Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.