Select Page
Selenium Testing

Selenium 4 JavaScript Pinning Feature

We, as a Selenium Testing Service Provider company, are excited to explore the new features of Selenium 4. In this blog article, you will learn what is Script Pinning in Selenium 4.

Selenium 4 JavaScript Pinning Feature - Blog
Listen to this blog

In Selenium 4, we have another feature which is useful to pin the commonly used JavaScript snippets to a WebDriver session. In Selenium 4 alpha version 7, you can try the below snippets at your end to see how script pinning works. Once a script is pinned, you can use it until your WebDriver session is alive. Let’s say you want to run a series of JavaScript snippets multiple times. You need to store each of the snippets in a String variable and execute them one by one.

However, with the script pinning feature, you can store & run the commonly used snippets using the WebDriver session instead of storing in String variables.

WebDriver driver = new ChromeDriver();
JavascriptExecutor executor = (JavascriptExecutor) driver;

ScriptKey hello ="return arguments[0]");
executor.executeScript(hello, "cheese");

You can also unpin the store script anytime.


If you would like to store and retrieve multiple pins, you can use Set to save the pins.

Set<ScriptKey> expected = ImmutableSet.of("return arguments[0];"),"return 'cheese';"),"return 42;"));

Set<ScriptKey> pinned = executor.getPinnedScripts();


Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us