How to install Playwright in Windows?

HOME

How to install node.js on windows 11

npm init playwright@latest

playwright.config.ts         # Test configuration
package.json
package-lock.json            # Or yarn.lock / pnpm-lock.yaml
tests/
  example.spec.ts            # Minimal example test

npx playwright test

npx playwright show-report

Playwright Tutorials

HOME

Why Choose Playwright for Browser Automation?

HOME

How to update XML Documents Using Java DOM Parser

HOME

package XML.DOM;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;

public class DOM_ModifyXMLDemo {

    public static void main(String[] args) {

        try {

            //Create a DocumentBuilderFactory and DocumentBuilder
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            // Parse the XML file into a Document
            Document document = builder.parse(new File("src/test/resources/Payloads/SimpleXML.xml"));

            // Normalize XML structure
            document.getDocumentElement().normalize();

            // Step 3: Update the XML content
            NodeList nodeList = document.getElementsByTagName("employee");

            for (int i = 0; i < nodeList.getLength(); i++) {
                Element employee = (Element) nodeList.item(i);
                String id = employee.getAttribute("id");

                if ("1".equals(id)) {
                    Element position = (Element) employee.getElementsByTagName("position").item(0);
                    position.setTextContent("Consultant");
                }
            }

            //Traverse the nodes to normalize spaces in text nodes
            Node rootNode = document.getDocumentElement();
            normalizeSpace(rootNode);

            //Write changes back to the XML file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

            //Writing updated content into the file
            DOMSource domSource = new DOMSource(document);

            StreamResult fileResult = new StreamResult(new File("src/test/resources/Payloads/UpdatedXML.xml"));
            transformer.transform(domSource, fileResult);

            // Print updated XML to console
            StreamResult consoleResult = new StreamResult(System.out);
            transformer.transform(domSource, consoleResult);

            System.out.println("\n XML file updated successfully!");

        } catch (ParserConfigurationException | TransformerException | IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            throw new RuntimeException(e);
        }
    }

    private static void normalizeSpace(Node node) {
        if (node.getNodeType() == Node.TEXT_NODE) {
            String trimmedText = node.getTextContent().trim().replaceAll("\\s+", " ");
            node.setTextContent(trimmedText);
        }
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            normalizeSpace(children.item(i));
        }
    }
}

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/test/resources/Payloads/SimpleXML.xml"));
 document.getDocumentElement().normalize();
  NodeList nodeList = document.getElementsByTagName("employee");

            for (int i = 0; i < nodeList.getLength(); i++) {
                Element employee = (Element) nodeList.item(i);
                String id = employee.getAttribute("id");

                if ("1".equals(id)) {
                    Element position = (Element) employee.getElementsByTagName("position").item(0);
                    position.setTextContent("Consultant");
                }
            }
private static void normalizeSpace(Node node) {
        if (node.getNodeType() == Node.TEXT_NODE) {
            String trimmedText = node.getTextContent().trim().replaceAll("\\s+", " ");
            node.setTextContent(trimmedText);
        }
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            normalizeSpace(children.item(i));
        }
    }

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // Pretty print the XML
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

DOMSource domSource = new DOMSource(document);

 // Write XML to file
 StreamResult fileResult = new StreamResult(new File("src/test/resources/Payloads/UpdatedXML.xml"));
transformer.transform(domSource, fileResult);

StreamResult consoleResult = new StreamResult(System.out);
transformer.transform(domSource, consoleResult);
try {
 } catch (ParserConfigurationException | TransformerException | IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            throw new RuntimeException(e);
        }
    }

package XML.DOM;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;

import static org.apache.commons.lang3.StringUtils.normalizeSpace;

public class DOM_ModifyXMLAddElementsDemo {

    public static void main(String[] args) {

        try {

            //Create a DocumentBuilderFactory and DocumentBuilder
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            // Parse the XML file into a Document
            Document document = builder.parse(new File("src/test/resources/Payloads/SimpleXML.xml"));

            // Normalize XML structure
            document.getDocumentElement().normalize();

            //Locate the root element
            Element rootElement = document.getDocumentElement();

            // Create a new employee element
            Element employee = document.createElement("employee");
            employee.setAttribute("id", "4");

            // Create name element
            Element name = document.createElement("name");
            name.appendChild(document.createTextNode("Milly Mathew"));
            employee.appendChild(name);

            // Create position element
            Element position = document.createElement("position");
            position.appendChild(document.createTextNode("Architect"));
            employee.appendChild(position);

            //Append the new employee node to the root element
            rootElement.appendChild(employee);

            //Traverse the nodes to normalize spaces in text nodes
            Node rootNode = document.getDocumentElement();
            normalizeSpace(rootNode);

            //Write changes back to the XML file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

            //Writing updated content into the file
            DOMSource domSource = new DOMSource(document);

            StreamResult fileResult = new StreamResult(new File("src/test/resources/Payloads/UpdatedXML1.xml"));
            transformer.transform(domSource, fileResult);

            // Print updated XML to console
            StreamResult consoleResult = new StreamResult(System.out);
            transformer.transform(domSource, consoleResult);

            System.out.println("\n XML file updated successfully!");

        } catch (ParserConfigurationException | TransformerException | IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            throw new RuntimeException(e);
        }
    }

    private static void normalizeSpace(Node node) {
        if (node.getNodeType() == Node.TEXT_NODE) {
            String trimmedText = node.getTextContent().trim().replaceAll("\\s+", " ");
            node.setTextContent(trimmedText);
        }
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            normalizeSpace(children.item(i));
        }
    }

}

Element rootElement = document.getDocumentElement();
 // Create a new employee element
 Element employee = document.createElement("employee");
employee.setAttribute("id", "4");

// Create name element
Element name = document.createElement("name");
name.appendChild(document.createTextNode("Milly Mathew"));
employee.appendChild(name);

// Create position element
Element position = document.createElement("position");
position.appendChild(document.createTextNode("Architect"));
employee.appendChild(position);

//Append the new employee node to the root element
rootElement.appendChild(employee);

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

How to Install Visual Studio Code on Windows

HOME

Highlighting Elements in Selenium Java

HOME

package Web;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.time.Duration;

public class HighlightText_Example {

    public static void main(String args[]) {

        ChromeOptions chromeOptions = new ChromeOptions();
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.manage().window().maximize();

        try {
            driver.get("https://www.selenium.dev/");

            // identify the element
            WebElement element = driver.findElement(By.xpath("//*[@id='main_navbar']/ul/li[7]/div/a"));

            // Optionally, we wait to observe the element before changing the color
            Thread.sleep(1000);

            // Set the background color to yellow and the border to red
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("arguments[0].style.backgroundColor = 'yellow'; arguments[0].style.border = '3px solid red';", element);

            // Optionally, we can wait to observe the highlight effect
            Thread.sleep(2000);
            System.out.println("Text is highlighted");

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally{
            // Closing browser
            driver.quit();
        }
    }
}

ChromeOptions chromeOptions = new ChromeOptions();
WebDriver driver = new ChromeDriver(chromeOptions);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();
driver.get("https://www.selenium.dev/");
WebElement element = driver.findElement(By.xpath("//*[@id='main_navbar']/ul/li[7]/div/a"));
  // Set the background color to yellow and the border to red
 JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.backgroundColor = 'yellow'; arguments[0].style.border = '3px solid red';", element);
 Thread.sleep(2000);
// Close the browser
driver.quit();

Retrieve Font Properties with Selenium in Java

HOME

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.IOException;
import java.time.Duration;

public class Font_Example {

    public static void main(String args[]) throws IOException {

        ChromeOptions chromeOptions = new ChromeOptions();
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        driver.manage().window().maximize();

        try {
            // Navigate to Url
            driver.get("https://www.selenium.dev/");

            WebElement element = driver.findElement(By.xpath("//*[@class='selenium-button-container']/a"));

            // Retrieve the font color
            String fontFamily  = element.getCssValue("font-family");
            System.out.println("fontFamily  : " + fontFamily );

            // Retrieve the font size
            String fontSize = element.getCssValue("font-size");
            System.out.println("fontSize : " + fontSize);

            // Retrieve the font weight
            String fontWeight = element.getCssValue("font-weight");
            System.out.println("fontWeight : " + fontWeight);

            // Retrieve the font style
            String fontStyle  = element.getCssValue("font-style");
            System.out.println("fontStyle : " + fontStyle);

            // Retrieve the font variation
            String fontVariant  = element.getCssValue("font-variant");
            System.out.println("fontVariant : " + fontVariant);

            // Retrieve the line height
            String lineHeight  = element.getCssValue("line-height");
            System.out.println("lineHeight : " + lineHeight);

            // Retrieve the letter spacing
            String letterSpacing  = element.getCssValue("letter-spacing");
            System.out.println("letterSpacing : " + letterSpacing);

            // Retrieve the text transform
            String textTransform  = element.getCssValue("text-transform");
            System.out.println("textTransform : " + textTransform);

            // Retrieve the text decoration
            String textDecoration  = element.getCssValue("text-decoration");
            System.out.println("textDecoration : " + textDecoration);

            System.out.println("####################### Verify the font properties #########################");

            // Verify the font family
            if (fontFamily.contains("Arial")) {
                System.out.println("fontFamily is as expected.");
            } else {
                System.out.println("fontFamily is not as expected. Found: " + fontFamily);
            }

            // Verify the font size
            if (fontSize.equals("16px")) {
                System.out.println("fontSize is as expected.");
            } else {
                System.out.println("fontSize is not as expected. Found: " + fontSize);
            }

            // Verify the font weight
            if (fontWeight.equals("500")) {
                System.out.println("fontWeight is as expected.");
            } else {
                System.out.println("fontWeight is not as expected. Found: " + fontWeight);
            }

            // Verify the font style
            if (fontStyle.equals("italic")) {
                System.out.println("fontStyle is as expected.");
            } else {
                System.out.println("fontStyle is not as expected. Found: " + fontStyle);
            }

            // Verify the font variant
            if (fontVariant.equals("small-caps")) {
                System.out.println("fontVariant is as expected.");
            } else {
                System.out.println("fontVariant is not as expected. Found: " + fontVariant);
            }

            // Verify the line height
            if (lineHeight.equals("24px")) {
                System.out.println("lineHeight is as expected.");
            } else {
                System.out.println("lineHeight is not as expected. Found: " + lineHeight);
            }

            // Verify the letter Spacing
            if (letterSpacing.equals("normal")) {
                System.out.println("letterSpacing is as expected.");
            } else {
                System.out.println("letterSpacing is not as expected. Found: " + letterSpacing);
            }

            // Verify the text Transform
            if (textTransform.equals("lowercase")) {
                System.out.println("textTransform is as expected.");
            } else {
                System.out.println("textTransform is not as expected. Found: " + textTransform);
            }

            // Verify the text Decoration
            if (textDecoration.equals("underline")) {
                System.out.println("textDecoration is as expected.");
            } else {
                System.out.println("textDecoration is not as expected. Found: " + textDecoration);
            }

        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

ChromeOptions chromeOptions = new ChromeOptions();
WebDriver driver = new ChromeDriver(chromeOptions);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.manage().window().maximize();
driver.get("https://www.selenium.dev/");
WebElement element = driver.findElement(By.xpath("//*[@class='selenium-button-container']/a"));
  1. text-transform – Modifies the capitalization of text. (e.g. uppercase, lowercase, capitalize)
// Retrieve the font color
String fontFamily  = element.getCssValue("font-family");
System.out.println("fontFamily  : " + fontFamily );

// Retrieve the font size
String fontSize = element.getCssValue("font-size");
System.out.println("fontSize : " + fontSize);

// Retrieve the font weight
String fontWeight = element.getCssValue("font-weight");
System.out.println("fontWeight : " + fontWeight);

// Retrieve the font style
String fontStyle  = element.getCssValue("font-style");
System.out.println("fontStyle : " + fontStyle);

// Retrieve the font variation
String fontVariant  = element.getCssValue("font-variant");
System.out.println("fontVariant : " + fontVariant);

// Retrieve the line height
String lineHeight  = element.getCssValue("line-height");
System.out.println("lineHeight : " + lineHeight);

// Retrieve the letter spacing
String letterSpacing  = element.getCssValue("letter-spacing");
System.out.println("letterSpacing : " + letterSpacing);

// Retrieve the text transform
String textTransform  = element.getCssValue("text-transform");
System.out.println("textTransform : " + textTransform);

// Retrieve the text decoration
String textDecoration  = element.getCssValue("text-decoration");
System.out.println("textDecoration : " + textDecoration);
// Verify the font family
if (fontFamily.contains("Arial")) {
    System.out.println("fontFamily is as expected.");
} else {
    System.out.println("fontFamily is not as expected. Found: " + fontFamily);
}

// Verify the font size
if (fontSize.equals("16px")) {
    System.out.println("fontSize is as expected.");
} else {
    System.out.println("fontSize is not as expected. Found: " + fontSize);
}

// Verify the font weight
if (fontWeight.equals("500")) {
    System.out.println("fontWeight is as expected.");
} else {
    System.out.println("fontWeight is not as expected. Found: " + fontWeight);
}

// Verify the font style
if (fontStyle.equals("italic")) {
    System.out.println("fontStyle is as expected.");
} else {
    System.out.println("fontStyle is not as expected. Found: " + fontStyle);
}

// Verify the font variant
if (fontVariant.equals("small-caps")) {
    System.out.println("fontVariant is as expected.");
} else {
    System.out.println("fontVariant is not as expected. Found: " + fontVariant);
}

// Verify the line height
if (lineHeight.equals("24px")) {
    System.out.println("lineHeight is as expected.");
} else {
    System.out.println("lineHeight is not as expected. Found: " + lineHeight);
}

// Verify the letter Spacing
if (letterSpacing.equals("normal")) {
    System.out.println("letterSpacing is as expected.");
} else {
    System.out.println("letterSpacing is not as expected. Found: " + letterSpacing);
}

// Verify the text Transform
if (textTransform.equals("lowercase")) {
    System.out.println("textTransform is as expected.");
} else {
    System.out.println("textTransform is not as expected. Found: " + textTransform);
}

// Verify the text Decoration
if (textDecoration.equals("underline")) {
    System.out.println("textDecoration is as expected.");
} else {
    System.out.println("textDecoration is not as expected. Found: " + textDecoration);
}
// Close the browser
driver.quit();

How to Retrieve Text and Background Colors with Selenium in Java

HOME

org.openqa.selenium.support.Color.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.Color;
import java.io.IOException;
import java.time.Duration;

public class Color_Example {

    public static void main(String args[]) throws IOException {

        String expectedColor = "#ffffff"; // White color in HEX

        ChromeOptions chromeOptions = new ChromeOptions();
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.manage().window().maximize();

        try {
            // Navigate to Url
            driver.get("https://www.selenium.dev/");

            WebElement element = driver.findElement(By.id("dev-partners"));

            // Retrieve the font color CSS property as RGBA
            String rgbaColor = element.getCssValue("color");
            System.out.println("rgbaColor : " + rgbaColor);

            // Convert the RGBA color to hex using the Color class
            String hexColor = Color.fromString(rgbaColor).asHex();
            System.out.println("hexColor : " + hexColor);

            // Convert the RGBA color to RGB using the Color class
            String rgbColor = Color.fromString(rgbaColor).asRgb();
            System.out.println("rgbColor : " + rgbColor);

            // Verify the color (example: verifying against expected #ffffff color)
            if (hexColor.equals(expectedColor)) {
                System.out.println("Hex color is as expected.");
            } else {
                System.out.println("Hex color is not as expected. Found: " + hexColor);
            }

        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

 String expectedColor = "#ffffff"; // White color in HEX
ChromeOptions chromeOptions = new ChromeOptions();
WebDriver driver = new ChromeDriver(chromeOptions);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();
driver.get("https://www.selenium.dev/");
WebElement element = driver.findElement(By.id("dev-partners"));

// Retrieve the font color CSS property as RGBA
String rgbaColor = element.getCssValue("color");
System.out.println("rgbaColor : " + rgbaColor);
 // Convert the RGBA color to hex using the Color class
String hexColor = Color.fromString(rgbaColor).asHex();
System.out.println("hexColor : " + hexColor);

// Convert the RGBA color to RGB using the Color class
String rgbColor = Color.fromString(rgbaColor).asRgb();
System.out.println("rgbColor : " + rgbColor);
 // Verify the color (example: verifying against expected #ffffff color)
 if (hexColor.equals(expectedColor)) {
          System.out.println("Hex color is as expected.");
} else {
          System.out.println("Hex color is not as expected. Found: " + hexColor);
}
// Close the browser
driver.quit();

getCssValue("background-color")

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.Color;

import java.io.IOException;
import java.time.Duration;

public class BackgroundColor_Example {

    public static void main(String args[]) throws IOException {

        ChromeOptions chromeOptions = new ChromeOptions();
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.manage().window().maximize();

        try {
            // Navigate to Url
            driver.get("https://www.selenium.dev/");

            WebElement element = driver.findElement(By.id("dev-partners"));

            // Retrieve the background color CSS property
            String bgColorValue = element.getCssValue("background-color");
            System.out.println("color : " + bgColorValue);

            // Convert the background color to HEX format
            String hexColor = Color.fromString(bgColorValue).asHex();
            System.out.println("hexColor : " + hexColor);

            // Convert the background color to RGB format
            String rgbColor = Color.fromString(bgColorValue).asRgb();
            System.out.println("rgbColor : " + rgbColor);

            // Verify the color (example: verifying against expected #43b02a color)
            String expectedColor = "#43b02a"; // Green color in HEX
            if (hexColor.equals(expectedColor)) {
                System.out.println("Background color is as expected.");
            } else {
                System.out.println("Background color is not as expected. Found: " + hexColor);
            }
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

Performance Testing – Multiple Choice Questions and Answers – MCQ1

HOME

























How to create XML Documents Using Java DOM Parser

HOME

An XML file contains data between the tags. This makes it complex to read compared to other file formats like docx and txt. There are two types of parsers which parse an XML file:

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import javax.xml.transform.OutputKeys;

public class CreateXMLDocument {

    public static void main(String[] args) {
        try {

            // Step 1: Create a DocumentBuilderFactory and DocumentBuilder
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            // Step 2: Create a new Document
            Document document = builder.newDocument();

            // Step 3: Create and append root element
            Element root = document.createElement("employees");
            document.appendChild(root);

            // Step 4: Create and append employee elements
            Element employee1 = document.createElement("employee");
            employee1.setAttribute("id", "1");
            Element name1 = document.createElement("Name");
            name1.appendChild(document.createTextNode("Jimmy Buffet"));
            Element id1 = document.createElement("Employee_Id");
            id1.appendChild(document.createTextNode(String.valueOf(10342256)));
            Element salary1 = document.createElement("Salary");
            salary1.appendChild(document.createTextNode(String.valueOf(5000.00)));
            employee1.appendChild(name1);
            employee1.appendChild(id1);
            employee1.appendChild(salary1);
            root.appendChild(employee1);

            Element employee2 = document.createElement("employee");
            employee2.setAttribute("id", "2");
            Element name2 = document.createElement("name");
            name2.appendChild(document.createTextNode("Jane Smith"));
            Element position2 = document.createElement("position");
            position2.appendChild(document.createTextNode("Project Manager"));
            employee2.appendChild(name2);
            employee2.appendChild(position2);
            root.appendChild(employee2);

            // Step 5: Write the content into an XML file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();

            transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // Pretty print the XML
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

            DOMSource domSource = new DOMSource(document);

            // Write XML to file
            StreamResult fileResult = new StreamResult(new File("src/test/resources/Payloads/output.xml"));
            transformer.transform(domSource, fileResult);

           // Print XML to console
            StreamResult consoleResult = new StreamResult(System.out);
            transformer.transform(domSource, consoleResult);

            System.out.println("XML file created successfully!");

        } catch (ParserConfigurationException | TransformerException e) {
            e.printStackTrace();
        }
    }
}

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
 Document document = builder.newDocument();
Element root = document.createElement("employees");
document.appendChild(root);
Element employee1 = document.createElement("employee");
employee1.setAttribute("id", "1");
Element name1 = document.createElement("Name");
name1.appendChild(document.createTextNode("Jimmy Buffet"));
Element id1 = document.createElement("Employee_Id");
id1.appendChild(document.createTextNode(String.valueOf(10342256)));
Element salary1 = document.createElement("Salary");
salary1.appendChild(document.createTextNode(String.valueOf(5000.00)));
employee1.appendChild(name1);
employee1.appendChild(id1);
employee1.appendChild(salary1);
root.appendChild(employee1);

Element employee2 = document.createElement("employee");
employee2.setAttribute("id", "2");
Element name2 = document.createElement("name");
name2.appendChild(document.createTextNode("Jane Smith"));
Element position2 = document.createElement("position");
position2.appendChild(document.createTextNode("Project Manager"));
employee2.appendChild(name2);
employee2.appendChild(position2);
root.appendChild(employee2);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // Pretty print the XML
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

DOMSource domSource = new DOMSource(document);

 // Write XML to file
 StreamResult fileResult = new StreamResult(new File("src/test/resources/Payloads/output.xml"));
transformer.transform(domSource, fileResult);

StreamResult consoleResult = new StreamResult(System.out);
transformer.transform(domSource, consoleResult);

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!