Forked from remarkablemark/webdriverjs_localstorage.js
Created
March 18, 2017 22:13
-
-
Save cdoremus/a82d287021b688571fb7912c256b9f3a to your computer and use it in GitHub Desktop.
Get and set localStorage with WebDriverJS `executeScript` and `executeAsyncScript`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* Module dependencies. | |
*/ | |
const webdriver = require('selenium-webdriver'); | |
/** | |
* Build driver. | |
*/ | |
const builder = new webdriver.Builder(); | |
const driver = builder.forBrowser('phantomjs').build(); | |
// make sure to open a page otherwise you will get the WebDriverError | |
// SecurityError: DOM Exception 18 | |
driver.get('https://httpbin.org'); | |
/** | |
* Set local storage item with `executeScript`. | |
*/ | |
driver | |
.executeScript('return window.localStorage.setItem("key", "value");'); | |
/** | |
* Get local storage item with `executeScript`. | |
*/ | |
driver | |
.executeScript('return window.localStorage.getItem("key");') | |
.then((itemValue) => { | |
console.log(itemValue); | |
}); | |
/** | |
* Set and get local storage item with `executeAsyncScript`. | |
*/ | |
driver | |
.executeAsyncScript(function(callback) { | |
window.localStorage.setItem('foo', 'bar'); | |
callback(window.localStorage.getItem('foo')); | |
}) | |
.then((itemValue) => { | |
console.log(itemValue); | |
}); | |
// close the driver | |
driver.quit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment