john-doherty/selenium-cucumber-js

How can I read localStorage in test?

uttarwarsandesh33 opened this issue · 2 comments

  1. I want to check localStorage in test how Can I do that?

  2. Am I able to access the window object?

  3. How can run this with Headless

Thanks

Hello uttarwarsandesh33, your questions has little to do with this package and answers can be eaisly found on google, but let me help you:

  1. API doesn't provide a way to directly read/write the local storage, but it can be done with execute_script:
    let value = await driver.executeScript("return window.localStorage.getItem(arguments[0]);", key)
    or
    driver.executeScript("window.localStorage.setItem(arguments[0], arguments[1]);", key, value)

  2. The same way as above - by manipulating it with executeScript.

  3. Due to many possible configurations the best way would be to create customDriver.js file in your home directory:

'use strict';
var { Builder } = require('selenium-webdriver');

/**
 * Creates a Selenium WebDriver using Selenium grid with Chrome as the browser
 * @returns {ThenableWebDriver} Selenium web driver
 */
module.exports = function() {

const screen = {
  width: 640,
  height: 480
};

let driver = new Builder()
    .forBrowser('chrome')
    .setChromeOptions(new chrome.Options().headless().windowSize(screen))
    .setFirefoxOptions(new firefox.Options().headless().windowSize(screen))
    .build();
}

and run it with:
node ./node_modules/selenium-cucumber-es6/index.js -s ./step-definitions -b customDriver.js

getting this error while using
let value = await driver.executeScript("return window.localStorage.getItem(arguments[0]);", key)

JavascriptError: SecurityError: The operation is insecure.
at Object.throwDecodedError