0

I am using local storage to save the updatet variable value every time I run my test case but fore some reasons I get the error:

ReferenceError: localStorage is not defined at Object ReferenceError: localStorage is not defined at Object. (/app/Projects/Javascript/Mocha/27403_9907/Libraries/addNewBeneficiary.js:17:49) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:12) at Module.require (internal/modules/cjs/loader.js:974:19) at require (internal/modules/cjs/helpers.js:93:18) at Object. (/app/Projects/Javascript/Mocha/27403_9907/run/test.js:13:39) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:12) at ModuleWrap. (internal/modules/esm/translators.js:199:29) at ModuleJob.run (internal/modules/esm/module_job.js:183:25) at async Loader.import (internal/modules/esm/loader.js:178:24) at async formattedImport (/usr/lib/node_modules/mocha/lib/nodejs/esm-utils.js:7:14) at async Object.exports.requireOrImport (/usr/lib/node_modules/mocha/lib/nodejs/esm-utils.js:48:32) at async Object.exports.loadFilesAsync (/usr/lib/node_modules/mocha/lib/nodejs/esm-utils.js:88:20) at async singleRun (/usr/lib/node_modules/mocha/lib/cli/run-helpers.js:125:3) at async Object.exports.handler (/usr/lib/node_modules/mocha/lib/cli/run.js:374:5)

I have created a JS file and uploaded it in a customized platform called Momentum, which we use for automated test. the code file is as following:

const { wd } = require("../setup/v2setup");

                    var isStepFailed = false;

                    var assert = require('assert');
                    var should = require('should');
                    var windowSize = null;
                    const axios = require('axios').default;

let storedXPathIndex = localStorage.getItem("storedXPathIndex");
                    let xPathIndex = (storedXPathIndex) ? ++storedXPathIndex : 0;
                    localStorage.setItem("storedXPathIndex", xPathIndex);
                    const max = 5;

                    exports.setAddNewBeneficiaryMethods = (driver) =>{

wd.addPromiseChainMethod('SelectAcc', () => {


                    
   if( ++xPathIndex > max ) xPathIndex = 0;
    return 

driver.swipeUntilElement("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ScrollView/android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup["+ xPathIndex +"]")
})
}

I also tried a mock function as suggested below and now i get

window is not defined

the mock function and how I am calling it is

function storageMock() {
let storage = {};

return {
  setItem: function(key, value) {
    storage[key] = value || '';
  },
  getItem: function(key) {
    return key in storage ? storage[key] : null;
  },
  removeItem: function(key) {
    delete storage[key];
  },
  get length() {
    return Object.keys(storage).length;
  },
  key: function(i) {
    const keys = Object.keys(storage);
    return keys[i] || null;
  }
}
  }

wd.addPromiseChainMethod('SelectAcc', () => {

 let storedXPathIndex = window.localStorage.getItem("storedXPathIndex");
                let xPathIndex = (storedXPathIndex) ? ++storedXPathIndex : 0;
                window.localStorage.setItem("storedXPathIndex", xPathIndex);

Not sure where am I doing wrong const max = 5;

   if( ++xPathIndex > max ) xPathIndex = 0;
    return 

driver.swipeUntilElement("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ScrollView/android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup["+ xPathIndex +"]")
})
Art
  • 3
  • 4
  • [`localStorage` is on the web](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) not in Node.JS. Unless you have some extra library that adds it, however, I'd expect you to import it, if that's the case. – VLAZ Dec 09 '21 at 10:01

1 Answers1

0

localStorage is only available in browsers, So if you want to run tests, you need to mock the object as explained in this answer about how to use localStorage in tests, but here is a peek:

function storageMock() {
    let storage = {};

    return {
      setItem: function(key, value) {
        storage[key] = value || '';
      },
      getItem: function(key) {
        return key in storage ? storage[key] : null;
      },
      removeItem: function(key) {
        delete storage[key];
      },
      get length() {
        return Object.keys(storage).length;
      },
      key: function(i) {
        const keys = Object.keys(storage);
        return keys[i] || null;
      }
    };
  }

// mock the localStorage
window.localStorage = storageMock();
Farzin Farzanehnia
  • 1,040
  • 8
  • 12
  • i updated my question, with the mock you suggested but now i am getting the error "window is not defined". Maybe you can take a look and have any suggestion? @Farzin Farzanehnia – Art Dec 09 '21 at 10:46
  • Well that was just to explain, I don't see where you use `storageMock()` but before your tests run make sure to do something like `const localStorage = storageMock()` and that should work. – Farzin Farzanehnia Dec 09 '21 at 18:19