Skip to content

Commit bab0e54

Browse files
kevinkammleiterKevin Kammleiter
andauthored
Added browser storage handling as seperate extension (#1025)
* feat(storage-handling): added handling of localStorage and sessionStorage of Browser object * feat(storage-handling): added tests for storage injection * feat(program.js): changed wording of added program option Co-authored-by: Kevin Kammleiter <kevin.kammleiter@exxeta.com>
1 parent 8494356 commit bab0e54

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

bin/program.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ function getProgram() {
6868
"--cookies-file <file>",
6969
"specifies the file name to store the persistent Cookies"
7070
)
71+
.option(
72+
"--local-storage <values>",
73+
'ability to set a local storage, key-value pairs (e.g. "bar=foo;domain=url")'
74+
)
75+
.option(
76+
"--session-storage <values>",
77+
'ability to set a session storage, key-value pairs (e.g. "bar=foo;domain=url")'
78+
)
7179
.option(
7280
"--ignore-ssl-errors",
7381
"ignores SSL errors, such as expired or self-signed certificate errors"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Support for session-/localStorage injection.
3+
*/
4+
"use strict";
5+
6+
module.exports = function (phantomas) {
7+
const SESSION_STORAGE = "session-storage",
8+
LOCAL_STORAGE = "local-storage";
9+
10+
phantomas.on("init", async (page) => {
11+
let sessionStorage = phantomas.getParam(SESSION_STORAGE, false);
12+
let localStorage = phantomas.getParam(LOCAL_STORAGE, false);
13+
14+
// Mapping given "storage-string" to json object if string has been given
15+
if (typeof sessionStorage == "string" || sessionStorage instanceof String) {
16+
sessionStorage = parseStorage(sessionStorage);
17+
}
18+
19+
if (typeof localStorage == "string" || localStorage instanceof String) {
20+
localStorage = parseStorage(localStorage);
21+
}
22+
23+
if (sessionStorage) {
24+
phantomas.log(
25+
"Injecting sessionStorage: %j",
26+
JSON.stringify(sessionStorage)
27+
);
28+
await injectStorage(page, sessionStorage, SESSION_STORAGE);
29+
}
30+
if (localStorage) {
31+
phantomas.log("Injecting localStorag: %j", JSON.stringify(localStorage));
32+
await injectStorage(page, localStorage, LOCAL_STORAGE);
33+
}
34+
});
35+
36+
function parseStorage(storageString) {
37+
// --sessionStorage='bar=foo;domain=url'
38+
// --localStorage='bar=fooLocal;domain=urlLocal'
39+
var storageMap = {};
40+
storageString.split(";").forEach(function (singleEntry) {
41+
var entryKeyValue = singleEntry.split("=");
42+
storageMap[entryKeyValue[0]] = entryKeyValue[1];
43+
});
44+
return storageMap;
45+
}
46+
47+
/**
48+
* Inject the given storage into the specified page storage.
49+
* Either localStorage or sessionStorage
50+
*
51+
* @param {Page} page in which page the storage should be injected
52+
* @param {Object} storage the JSON object consisting of the storage keys and values
53+
* @param {string} storageType either localStorage or sessionStorage
54+
*/
55+
async function injectStorage(page, storage, storageType) {
56+
if (!page || !storage || !storageType) {
57+
return;
58+
}
59+
60+
/* istanbul ignore next */
61+
await page.evaluateOnNewDocument(
62+
(storage, storageType, SESSION_STORAGE, LOCAL_STORAGE) => {
63+
const keys = Object.keys(storage);
64+
const values = Object.values(storage);
65+
if (storageType === SESSION_STORAGE) {
66+
for (let i = 0; i < keys.length; i++) {
67+
sessionStorage.setItem(keys[i], values[i]);
68+
}
69+
}
70+
if (storageType === LOCAL_STORAGE) {
71+
for (let i = 0; i < keys.length; i++) {
72+
localStorage.setItem(keys[i], values[i]);
73+
}
74+
}
75+
},
76+
storage,
77+
storageType,
78+
SESSION_STORAGE,
79+
LOCAL_STORAGE
80+
);
81+
}
82+
};

test/integration-spec.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,19 @@
578578
localStorageEntries: 2
579579
offenders:
580580
localStorageEntries: ["foo", "test"]
581+
582+
# local/session storage injection
583+
- url: "/local-storage-injection.html"
584+
options:
585+
"local-storage": "bar=foo"
586+
metrics:
587+
local-storage: '{"bar":"foo"}'
588+
- url: "/session-storage-injection.html"
589+
options:
590+
"session-storage": "bar=foo"
591+
metrics:
592+
session-storage: '{"bar":"foo"}'
593+
581594
# JS errors
582595
- url: "/js-errors.html"
583596
metrics:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<body>
2+
<script>
3+
console.log('Got local-storage: ' + JSON.stringify(window.localStorage));
4+
// for unit tests
5+
(function(phantomas) {
6+
phantomas.setMetric('local-storage', JSON.stringify(window.localStorage));
7+
})(window.__phantomas);
8+
</script>
9+
</body>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<body>
2+
<script>
3+
console.log('Got session-storage: ' + JSON.stringify(sessionStorage));
4+
// for unit tests
5+
(function(phantomas) {
6+
phantomas.setMetric('session-storage', JSON.stringify(sessionStorage));
7+
})(window.__phantomas);
8+
</script>
9+
</body>

0 commit comments

Comments
 (0)