Learning Selenium With Java
Learning Selenium With Java
1. Download JDK:
Visit the official Oracle website or an OpenJDK distribution to download the latest
version of JDK.Follow the installation instructions provided for your operating system
(Windows, macOS, or Linux).
2. Install JDK:
Run the installer and follow the on-screen instructions. During the installation, set the
path for the JDK. This path is used by your operating system to find the Java
executable.
3. Verify JDK Installation:
Open a command prompt (Windows) or terminal (macOS/Linux). Type the following
command and press Enter: java -version You should see information about the
installed Java version.
1. Download Eclipse:
Visit the official Eclipse website and download the Eclipse IDE for Java Developers.
Choose the appropriate version for your operating system.
2. Install Eclipse:
Extract the downloaded Eclipse archive to a location on your computer. Inside the
Eclipse folder, you'll find the Eclipse executable. Run it.
3. Configure Eclipse:
When you first run Eclipse, it will prompt you to select a workspace (a folder where
your projects will be stored). After selecting a workspace, click "Launch" to open
Eclipse.
4. Create a Java Project:In Eclipse, go to "File" -> "New" -> "Java Project." Give your
project a name and click "Finish."
5. Create a Java Class: Right-click on the ‘src’ folder of your project in the "Package
Explorer" on the left. Go to "New" -> "Class." Enter a class name (e.g., HelloWorld)
and check the option to include the public static void main(String[] args) method.
Click "Finish."
6. Write a Simple Java Program: public class HelloWorld { public static void
main(String[] args) { System.out.println("Hello, Eclipse!"); }}
7. Run the Java Program:Right-click on the HelloWorld class file in the "Package
Explorer."Select "Run As" -> "Java Application."Output: Hello, Eclipse! This basic
setup allows you to write, compile, and run Java programs in Eclipse.
1.2 Variables and Data Types
1. Understanding Variables:
Variables are like containers used to store data in a program. They have a name
(identifier) and a data type that determines what kind of data they can hold. In Java,
variables must be declared before they can be used.
2. Declaring Variables:
Declaration: It's like telling the computer, "Hey, I'm going to need a spot to store some
data."
Syntax: dataType variableName;
int myNumber;
3. Initializing Variables:
Initialization: Giving an initial value to the variable.
Syntax: variableName = value;
myNumber = 42;
5. Example Code:
package myFirstPackage;
public class VariablesExample {
public static void main(String[] args) {
//Declaration
int myNumber;
//Initialization
myNumber = 10;
6. Output:
My Number: 10
My Age: 27
Conclusion:
Declaration: Creating a variable's placeholder.
Initialization: Assigning an initial value to the variable.
Combining: You often declare and initialize variables together for simplicity.
Data Types
7. Example Code:
package myFirstPackage;
public class Variables {
public static void main(String[] args) {
int myNumber = 10;
float myFloatNumber = 3.14f;
double myDoubleNumber = 3.1451691529;
char myChar = 'A';
boolean isJavaFun = true;
8. Output:
My Number: 10
My Float Number: 3.14
My Double Number: 3.1451691529
My character: A
Is Java Fun? true
int myInt = (int) myDouble; // The decimal part is truncated, myInt becomes 10
4. Example Code:
package myFirstPackage;
public class TypeCastingExample {
public static void main(String[] args) {
//Implicit Casting (Widening)
int myInt = 10;
double myDouble = myInt;
System.out.println("Implicit Casting (Widening): " + myDouble);
Conclusion:
● Implicit casting is done automatically by the compiler.
● Explicit casting requires manual intervention and may result in data loss if not done
carefully.
● Always be mindful of potential information loss when narrowing data types.
2. if Statement:
Usage: Used to execute a block of code if a specified condition is true.
Syntax:
if (condition) {
// Code to be executed if the condition is true
}
3. else Statement:
Usage: Used with the if statement to execute a block of code if the condition is false.
Syntax:
if (condition) {
// Code to be executed if the condition is true
}
else {
// Code to be executed if the condition is false
}
4. Example Code:
package myFirstPackage;
public class ControlFlowExample {
public static void main(String[] args) {
int age = 20;
// Example of if statement
if (age >= 18) {
System.out.println("You are an adult");
}
5. Output:
Conclusion:
● Control flow structures like if and else statements allow you to control the flow of your
program based on conditions.
● The if statement is used for basic conditional execution.
● The if-else statement is used when you want to execute different code blocks based
on whether a condition is true or false.
1. Introduction to Loops:
Definition: Loops are structures in programming that allow a set of instructions to be
repeated multiple times.
Purpose: They help in executing repetitive tasks and avoid code duplication.
2. for Loop:
Usage: Executes a block of code a specific number of times.
Syntax:
Example:
package myFirstPackage;
import java.util.Scanner;
public class ForLoopExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get the number for which you want to calculate the factorial
System.out.println("Enter a non-negative integer: ");
int number = scanner.nextInt();
long factorial = 1;
Output:
3. while Loop:
Usage: Executes a block of code as long as a specified condition is true.
Syntax:
while (condition) {
// Code to be repeated
}
Example:
package myFirstPackage;
public class WhileLoopExample {
public static void main(String[] args) {
// Set the desired outcome - rolling a 6
int targetnumber = 6;
// Initialize variables
int rolls = 0, result = 0;
// Simulate the dice rolling game with while loop
while (result != targetnumber) {
// Roll the six sided die
result = (int) (Math.random() *6) + 1;
Output:
Roll 1: 5
Roll 2: 3
Roll 3: 3
Roll 4: 5
Roll 5: 6
It took 5 rolls to get a 6
4. do-while Loop:
Usage: Similar to the while loop but guarantees at least one execution of the block of
code.
Syntax:
do {
// Code to be repeated
} while (condition);
Example:
package myFirstPackage;
import java.util.Scanner;
public class DoWhileExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Output:
5. Nested Loops:
Loops can be nested inside each other.
Example:
package myFirstPackage;
public class NestedLoopsExample {
public static void main(String[] args) {
int rows = 6;
Output:
1
12
123
1234
12345
123456
Conclusion:
● Loops are essential for repetitive execution of code in Java.
● The for loop is suitable when the number of iterations is known.
● The while loop is used when the condition is known at the beginning.
● The do-while loop guarantees at least one execution.
1. Introduction:
Purpose: Break and continue statements are control flow statements in Java that
alter the flow of a loop.
Usage: They provide more control over loop execution based on certain conditions.
2. Break Statement:
Usage: Used to terminate the loop prematurely, skipping the rest of the loop's code.
Scenario: Helpful when a specific condition is met, and you want to exit the loop.
Example:
package myFirstPackage;
import java.util.Iterator;
import java.util.Scanner;
public class BreakExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = {25, 43, 56, 71, 94, 68, 32, 12, 58, 80};
if (!found) {
System.out.println("Your number is not found.");
}
}
}
Output:
3. Continue Statement:
Usage: Skips the rest of the loop's code for the current iteration and moves to the
next iteration.
Scenario: Useful when you want to skip specific iterations based on a condition.
Example:
package myFirstPackage;
import java.util.Iterator;
import java.util.Scanner;
// Program to add all even numbers in a given range.
public class ContinueExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Output:
Conclusion:
● Break: Terminates the loop when a specific condition is met.
● Continue: Skips the rest of the code for the current iteration and moves to the next
iteration.
● Use Cases: Useful for adding flexibility to loop structures and handling specific
cases.
● Note: While these statements provide control, they should be used judiciously to
ensure the logic remains clear and understandable
1.4 Introduction to Object-Oriented Programming (OOP)
1.6: Inheritance
Overriding methods
1.7: Polymorphism
Understanding ArrayList
Introduction to HashMap
Learn how to interact with various types of web elements, like buttons,
text fields, checkboxes, and radio buttons.
Practice creating and running simple tests with both TestNG and JUnit.