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 +"]")
})