selenium-cucumber-js icon indicating copy to clipboard operation
selenium-cucumber-js copied to clipboard

How can I read localStorage in test?

Open uttarwarsandesh33 opened this issue 5 years ago • 2 comments
trafficstars

  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

uttarwarsandesh33 avatar Mar 31 '20 05:03 uttarwarsandesh33

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

dcmarti avatar Apr 02 '20 18:04 dcmarti

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

uttarwarsandesh33 avatar Apr 16 '20 04:04 uttarwarsandesh33