# KeyInitializers

**Overview**

The `KeyInitializers` class is a foundational component of our automated testing framework. It provides a means for users creating custom classes and methods to access and manipulate various objects and data within the framework. This class acts as a bridge between custom user-defined functionality and the core features of the framework, enabling extensive customization and flexibility in test automation.

**Key Features and Usage**

`KeyInitializers` contains multiple `ThreadLocal` instances, each serving a unique purpose. ThreadLocal is used to maintain variable values that are individual to a thread, ensuring thread safety in concurrent execution environments.

1. **Platform, Browser, and Environment Information**
   * Contains information about the platform, browser name, and environment (`PLATFORM`, `browserName`, `ENV`).
   * Usage Example: Access browser-specific or environment-specific settings for tests.
2. **MainWindow and Driver**
   * Handles the main window handle (`getMainWindow`) and the WebDriver (`driver`).
   * Usage Example: Facilitates browser operations like opening URLs.
3. **MailingServices and Response**
   * Provides access to mailing services (`mailingServices`) and REST API responses (`response`).
   * Usage Example: Useful for tests involving email interactions and API testing.
4. **DevToolsListener and CustomSoftAssert**
   * Offers listeners for browser DevTools (`devToolsListener`) and custom assertion capabilities (`customSoftAssert`).
   * Usage Example: Enables advanced browser interactions and flexible assertion mechanisms in tests.
5. **GlobalVariables**
   * A central repository for storing and retrieving global variables (`globalVariables`).
   * Usage Example: Store and access test data across different test cases and steps.
6. **Personal Information Generators**
   * Generates and stores personal information like first name (`firstName`), last name (`lastName`), and phone number (`phoneNumber`).
   * Usage Example: Create mock personal data for form-filling operations in tests.
7. **TestRunUrl**
   * Stores the URL associated with the current test run (`testRunUrl`).
   * Usage Example: Retrieve or set the test run's URL for reference or reporting.

**Custom Class Integration**

Users can create custom classes that leverage `KeyInitializers` to interact with the framework's core functionalities. For instance, in the provided `CustomClass`, methods are defined to perform actions like loading a URL, fetching data from global variables, and printing saved variables.

**Custom Class Methods Usage Example**

* **LoadString**: Opens a specific URL using the WebDriver from `KeyInitializers`.
* **fetchBody**, **getEmail**, **getRandomEmailBody**, **getSavedVariables**: Retrieves and prints data from `globalVariables`.

**Implementing in Test Cases**

To use these custom class methods in test scenarios, you can specify them in your test configuration as shown in the example:

Web Template

```
PAGE_NAME,WAIT_TYPE,SCROLL_TYPE,ELEMENT_NAME,ACTIONS,SEND_KEYS,VALIDATION,VALIDATION_TYPE
NONE,NONE,NONE,NONE,LOAD_URL,https://demo.automationtesting.in/Frames.html,NONE,NONE
iframe,NONE,NONE,nestedFrame,CLICK,NONE,NONE,NONE
NONE,NONE,NONE,NONE,SWITCH_FRAME_WEB_ELEMENT,iframe:firstFrame,NONE,NONE
iframe,NONE,NONE,firstFrameElement,SAVE,testFrameName,NONE,NONE
NONE,NONE,NONE,NONE,CUSTOM,CustomClass:getSavedVariables,NONE,NONE
```

Custom Class

```
package test.java;

import main.java.com.precisiontestautomation.automation.utils.KeyInitializers;

public class CustomClass {

    public void getSavedVariables(){
        System.out.println(KeyInitializers.getGlobalVariables().get().get("testFrameName"));
    }
}

```

**Conclusion**

`KeyInitializers` is a versatile and essential class in our testing framework, enabling extensive customization and integration of user-defined functionalities. By understanding and utilizing its capabilities, users can significantly enhance the scope and effectiveness of their automated testing strategies.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.precisiontestautomation.in/scriptlessautomation/automation-features/keyinitializers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
