Latest post from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

In real-time automation projects, test execution is never limited to a single browser or a single environment . Applications must be validated across multiple browsers (Chrome, Firefox, Edge) and multiple environments such as QA, UAT, and Staging before going live. A well-designed Selenium + Java + Cucumber automation framework should allow testers to switch browsers and environments easily without changing test scripts . This blog explains how to manage test execution efficiently across different browsers and environments using best practices followed in real projects. Why Multi-Browser and Multi-Environment Testing Is Important Different users use different browsers QA, UAT, and Staging environments have different configurations Bugs may appear only in specific environments or browsers Same test cases must be validated everywhere before production release Common Challenges Testers Face Hardcoded browser names and URLs Maintaining separate test scripts for each environment Browse...

JavaScriptExecutor in Selenium:

What is JavaScriptExecutor: in Selenium allows you to run JavaScript in the browser. It's useful when WebDriver can't interact with certain elements or handle more complex tasks, like working with hidden or dynamic elements.

Why Use JavaScriptExecutor:
It's useful when WebDriver can't interact with certain elements or handle more complex tasks, like working with hidden or dynamic elements.

JavaScriptExecutor lets you make changes to the web page directly, providing more control over its behavior.

How to Implement JavaScriptExecutor:
To use JavaScriptExecutor, you cast your WebDriver instance to the JavascriptExecutor interface:

JavascriptExecutor js = (JavascriptExecutor) driver;

Running JavaScript for Custom Actions
Using executeScript(): method allows you to run JavaScript code within the browser. 

Syntax:
js.executeScript("alert('Hello Everyone.');");

You can also send WebElements or other parameters to your JavaScript.

Example:
WebElement element = driver.findElement(By.id("elementId"));
js.executeScript("arguments[0].click();", element)

Scrolling Pages Using JavaScript
When elements are not visible within the current viewport, you can use JavaScript to scroll the web page. For Example 

*Scroll to a Specific Element:: This scrolls the page until the specified element is visible.

WebElement element = driver.findElement(By.id("elementId"));
js.executeScript("arguments[0].scrollIntoView(true);", element);

*Scroll by a Number of Pixels: To scroll the page by a specific number of pixels vertically or horizontally.

// Scroll down by 1000 pixels
js.executeScript("window.scrollBy(0, 1000);");

// Scroll horizontally by 500 pixels
js.executeScript("window.scrollBy(500, 0);");

*Scroll to the Bottom of the Page: To go to the bottom of the page.

js.executeScript("window.scrollTo(0, document.body.scrollHeight);");

*Scrolling to the Top of the Page: To return to the top of the page.

js.executeScript("window.scrollTo(0, 0);");

Clicking Hidden Elements:
JavaScriptExecutor can be used to click elements that are hidden or overlapped by other elements, which WebDriver might fail to click.

Example:
WebElement element = driver.findElement(By.id("hiddenElement"));
js.executeScript("arguments[0].click();", element);

Fetching Web Element Properties with JavaScript
Extracting Element Attributes:
JavaScriptExecutor you can access and retrieve element attributes or properties that may not be easily accessible through standard WebDriver methods.

JavaScriptExecutor lets you get various properties of web elements:
*Get an Element's Attribute: If you need to access an attribute from a web element (such as value, id, name, or custom attributes), JavaScriptExecutor allows you to directly extract it from the DOM.

// Locating the element
WebElement element = driver.findElement(By.id("elementId"));

// Retrieving the attribute using JavaScriptExecutor
String attributeValue = (String) js.executeScript("return arguments[0].getAttribute('attributeName');", element);

*Retrieve Element Text:
getText() can capture visible text,  you might need to access text that is hidden or  presented differently in the DOM. In such scenarios, JavaScriptExecutor’s properties like innerText or textContent can be used to retrieve the text.

String elementText = (String) js.executeScript("return arguments[0].innerText;", element);

*innerText gives the visible text of the element.

*textContent retrieves all text inside the element, even if it’s hidden.

*Get Inner HTML:
String innerHTML = (String) js.executeScript("return arguments[0].innerHTML;", element);

*Get Window Size:
Long width = (Long) js.executeScript("return window.innerWidth;");
Long height = (Long) js.executeScript("return window.innerHeight;");

*Get Current URL:
String url = (String) js.executeScript("return document.URL;");

*Get Page Title:
String title = (String) js.executeScript("return document.title;");

Enabling Disabled Elements:
Sometimes, users may need to interact with buttons or input fields that are disabled. JavaScript Executor can modify the disabled property of these elements to make them interactable.

WebElement button = driver.findElement(By.id("disabledButton"));
js.executeScript("arguments[0].disabled = false;", button);

Triggering Events via JavaScript:
JavaScript Executor can be used to simulate various events on elements, including hovering or triggering custom JavaScript events.

WebElement element = driver.findElement(By.id("hoverElement"));
js.executeScript("var event = new MouseEvent('mouseover', { bubbles: true }); arguments[0].dispatchEvent(event);", element);

Conclusion
JavaScriptExecutor is a powerful tool in Selenium that enhances test automation capabilities by enabling direct interaction with the DOM. Its versatility makes it a valuable asset for handling complex scenarios that are otherwise challenging to achieve using standard WebDriver methods.


Comments

Popular posts from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

Purpose of the StepData Class in Selenium + Java Cucumber Automation Framework

Ensuring Thread Safety in Parallel Test Execution with Selenium, Cucumber, and Java