Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I read localStorage in test? #99

Open
uttarwarsandesh33 opened this issue Mar 31, 2020 · 2 comments
Open

How can I read localStorage in test? #99

uttarwarsandesh33 opened this issue Mar 31, 2020 · 2 comments

Comments

@uttarwarsandesh33
Copy link

uttarwarsandesh33 commented Mar 31, 2020

  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

@dcmarti
Copy link

dcmarti commented Apr 2, 2020

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

@uttarwarsandesh33
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants