john-doherty / selenium-cucumber-js

Browser automation framework written in pure JavaScript using official selenium-webdriver and cucumber-js
ISC License
120 stars 89 forks source link

How can I read localStorage in test? #99

Open uttarwarsandesh33 opened 4 years ago

uttarwarsandesh33 commented 4 years ago
  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 commented 4 years ago

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');

/**

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 commented 4 years ago

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