Core Java Concepts for Selenium Automation: Java Utilities
1. Java Time API
The Java Time API provides classes to handle date and time operations. These classes are immutable and thread-safe, making them ideal for operations involving dates and times during testing (e.g., waiting, scheduling).
LocalDate
- Represents a date (e.g., 2024-09-04).LocalTime
- Represents a time (e.g., 12:30).LocalDateTime
- Represents a combination of date and time (e.g., 2024-09-04T12:30).
Example: Getting the Current Date and Time
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
public class TimeExample {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
LocalTime currentTime = LocalTime.now();
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current Date: " + currentDate);
System.out.println("Current Time: " + currentTime);
System.out.println("Current Date and Time: " + currentDateTime);
}
}
2. File Handling
Java provides several classes for handling files, including reading and writing operations, which are useful in Selenium for managing test data, configuration files, etc.
File
- Represents a file or directory path.FileReader
/BufferedReader
- For reading data from files.FileWriter
/BufferedWriter
- For writing data to files.
Example: Reading a File
import java.io.*;
public class FileReadingExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. Properties Class
The Properties
class in Java is used to handle configuration properties, such as reading browser types, URLs, and other configurations from a file.
Example: Reading a Properties File
import java.io.*;
import java.util.Properties;
public class PropertiesExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("config.properties")) {
Properties prop = new Properties();
prop.load(fis);
String browser = prop.getProperty("browser");
String url = prop.getProperty("url");
System.out.println("Browser: " + browser);
System.out.println("URL: " + url);
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. Logging Utilities
Logging is essential for debugging tests. Java provides logging classes to create log files or log messages to the console.
Logger
- Used to create logs.FileHandler
- Used to log messages to a file.ConsoleHandler
- Used to log messages to the console.
Example: Simple Logging
import java.util.logging.*;
public class LoggingExample {
private static final Logger logger = Logger.getLogger(LoggingExample.class.getName());
public static void main(String[] args) {
logger.info("This is an info message.");
logger.warning("This is a warning message.");
}
}
5. Regular Expressions (Regex)
Java’s Pattern
and Matcher
classes are used to handle regular expressions, which are useful for validating and extracting specific information from strings (e.g., extracting numbers from text).
Example: Extracting Numbers from a String
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
String text = "Order12345";
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
System.out.println("Found number: " + matcher.group());
}
}
}
6. Random Class
The Random
class is useful for generating random data, such as test inputs for usernames, emails, etc.
Example: Generating Random Numbers
import java.util.Random;
public class RandomExample {
public static void main(String[] args) {
Random random = new Random();
int randomNumber = random.nextInt(100); // Generate a random number between 0 and 99
System.out.println("Random Number: " + randomNumber);
}
}
7. Scanner Class
The Scanner
class is used for reading input from various sources such as files, strings, or user input from the console.
Example: Reading Input from a File
import java.io.*;
import java.util.Scanner;
public class ScannerExample {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(new File("data.txt"))) {
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}