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();
Comments(0)