Hands-On Automation Testing with Java for Beginners
eBook - ePub

Hands-On Automation Testing with Java for Beginners

Build automation testing frameworks from scratch with Java

  1. 156 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Automation Testing with Java for Beginners

Build automation testing frameworks from scratch with Java

About this book

Learn Java programming concepts to design automation testing frameworks

Key Features

  • 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

Book 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

Who this book is for

Hands-On Automation Testing with Java for Beginners is for software developers who want to step into the world of software quality assurance and perform automation testing using various testing frameworks. Prior experience of writing tests in Java is assumed.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Building Blocks for Java Programs – Loops and Conditions

Loops and conditions are the building blocks of Java programs. This chapter will help us understand the important loops and conditions through examples. Learning these loops and conditions in Java will make writing code easier.
In this chapter, we will cover the following topics:
  • The for loop
  • The if...else condition
  • The while loop
  • Nested loops

The for loop

Let's see how the for loop works. The for loop is one of the most commonly used loops in Java programs, and it it is very important to understand how it works internally. So, let's say we want to print the numbers from 1 to 100 using the forloop. For the syntax to execute the numbers from 1 to 100 in a sequence and to write that in a for loop, we will simply write:
// 1 to 100

/* for(initialization;condition;increment)
{
} */
for (int i=0;i<100;i++)
{
system.out.println(i);
}
}

Since we want to print 0, 1, 2, 3, we use i++. This means for every loop, it increments only by 1. And while looping, each time, it also checks whether the preceding condition is satisfied. So, if 1 is less than 100, it goes inside; if 2 is less than 100, it goes inside. Until this condition is satisfied, it will keep on looping. When the value of i reaches 100, 100 is less than 100, which is false. At that time, it terminates the loop and comes out of it. We will use a basic example here:
for (int i=0;i<5;i++)
{
system.out.println(i);
}
To run test cases in debug mode in the IDE, double-click at the location shown in the following screenshot:
Line from which the debugging begins
When you see the blue icon, run that in the debug mode by clicking the insects-like symbol. It will ask you to launch in debug mode. Just click on Save to do so:
Debug icon at the top of the editor
You will see all the variable values here. Step by step, we'll go inside the loop, and will execute the next step of the program:
Variable value while debugging
Finally, when it reaches the value 4, and is incremented by 1 again, it is 5. Note that it comes out of the loop without going inside that after the value becomes 5. So, that means the condition is no longer satisfied and the loop will run five times. The output is shown in the following screenshot:
Final output as per the code
So, that's how the for loop works.
Now, if we set the condition to the following, it will not go inside the for loop, even for the first time since, the condition is false.:
for (int i=5;i<3;i++)
On running the preceding condition in in the debug mode, the complete loop is skipped,and nothing is seen in the output.
Let's see another example:
for (int i=0;i<10;i+2 )
The output will be:
0
2
4
6
8
This is how the for loop works internally.
In the next section, we will learn about the if...else and do...while loops.

if...else condition

Before we learn the while and do...while loops, we will discuss the if condition in this section. In a Java program, when the if conditional statement is used, the statement in the if block is executed only if the condition is satisfied. Otherwise the statement from else block is run. Also this execution is just takes place once. In a for loop, a variable is initiated and the loop runs till the condition is satisfied.
However, in the if case, it will not keep on looping. It will just go inside the loop once the if condition is satisfied; otherwise, it will go into the else block. So, control will execute the statements present in this else block, as shown in the following screenshot:
Output of the if...else condition as per the code
But all this happens only once, unlike the for loop, where a condition is satisfied until it goes back and executes.
Let's take a look at the following example:
 if(5>2)
{
System.out.println("success");
}
else
{
System.out.println("fail");
}
The following screenshot displays those errors:
Quick fixes drop down with suggestions to correct the code error
The first error is to remove the including condition, which can be ignored. On running the preceding program, you will see the output as success because the condition 5 greater than 2 that went inside is true:
Output displays success as per the code
If we change the condition and make 5 less than 2, making the condition false, it will skip to the else block and execute the statement present in else.
code to receive fail as the output
This time the output should be fail, as shown in the following screenshot:
Output displays success as per the code
This is how the if condition works.
Note...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Packt Upsell
  4. Contributors
  5. Preface
  6. First Programming Steps in Java
  7. Understanding Classes, Objects, and Their Usage in Java
  8. Handling Strings and Their Functions in Java
  9. Building Blocks for Java Programs &#x2013; Loops and Conditions
  10. Everything You Need to Know About Interfaces and Inheritance
  11. Learn Everything about Arrays
  12. Understanding Date Class and Constructors in Java 11
  13. Importance of the super and this Keywords and Exceptions in Java
  14. Understanding the Collections Framework
  15. The Importance of the final Keyword, Packages, and Modifiers
  16. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Hands-On Automation Testing with Java for Beginners by Rahul Shetty in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming. We have over one million books available in our catalogue for you to explore.