Selenium 4 JavaScript Pinning Feature - Codoid
Select Page
QA Automation

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.

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 = executor.pin("return arguments[0]");
executor.executeScript(hello, "cheese");

You can also unpin the store script anytime.

executor.unpin(hello);

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

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

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

Written By

Submit a Comment

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


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 = executor.pin("return arguments[0]");
executor.executeScript(hello, "cheese");

You can also unpin the store script anytime.

executor.unpin(hello);

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

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

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