-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Browser } from "../types"; | ||
import logger from "../../utils/logger"; | ||
|
||
export = async (browser: Browser): Promise<void> => { | ||
const { publicAPI: session } = browser; | ||
|
||
const clearStorage = async (storageName: "localStorage" | "sessionStorage"): Promise<void> => { | ||
try { | ||
await session.execute(storageName => window[storageName].clear(), storageName); | ||
} catch (e) { | ||
const message = (e as Error).message || ""; | ||
|
||
if (message.startsWith(`Failed to read the '${storageName}' property from 'Window'`)) { | ||
logger.warn(`Couldn't clear ${storageName}: ${message}`); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}; | ||
|
||
session.addCommand("clearSession", async function (): Promise<void> { | ||
await session.deleteAllCookies(); | ||
|
||
await clearStorage("localStorage"); | ||
await clearStorage("sessionStorage"); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
module.exports = [ | ||
"assert-view", | ||
"clearSession", | ||
"getConfig", | ||
"getPuppeteer", | ||
"setOrientation", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import webdriverio from "webdriverio"; | ||
import sinon, { SinonStub } from "sinon"; | ||
|
||
import clientBridge from "src/browser/client-bridge"; | ||
import logger from "src/utils/logger"; | ||
import { mkExistingBrowser_ as mkBrowser_, mkSessionStub_ } from "../utils"; | ||
|
||
import type ExistingBrowser from "src/browser/existing-browser"; | ||
|
||
describe('"clearSession" command', () => { | ||
const sandbox = sinon.sandbox.create(); | ||
|
||
const initBrowser_ = ({ browser = mkBrowser_(), session = mkSessionStub_() } = {}): Promise<ExistingBrowser> => { | ||
(webdriverio.attach as SinonStub).resolves(session); | ||
|
||
return browser.init({ sessionId: session.sessionId, sessionCaps: session.capabilities, sessionOpts: {} }); | ||
}; | ||
|
||
beforeEach(() => { | ||
sandbox.stub(webdriverio, "attach"); | ||
sandbox.stub(clientBridge, "build").resolves(); | ||
sandbox.stub(logger, "warn"); | ||
|
||
global.window = { | ||
localStorage: { clear: sinon.stub() } as unknown as Storage, | ||
sessionStorage: { clear: sinon.stub() } as unknown as Storage, | ||
} as unknown as Window & typeof globalThis; | ||
}); | ||
|
||
afterEach(() => { | ||
global.window = undefined as unknown as Window & typeof globalThis; | ||
sandbox.restore(); | ||
}); | ||
|
||
it("should add command", async () => { | ||
const session = mkSessionStub_(); | ||
|
||
await initBrowser_({ session }); | ||
|
||
assert.calledWith(session.addCommand, "clearSession", sinon.match.func); | ||
}); | ||
|
||
it("should delete all cookies", async () => { | ||
const session = mkSessionStub_(); | ||
|
||
await initBrowser_({ session }); | ||
await session.clearSession(); | ||
Check failure on line 47 in test/src/browser/commands/clearSession.ts GitHub Actions / build (18.x)
Check failure on line 47 in test/src/browser/commands/clearSession.ts GitHub Actions / build (20.x)
|
||
|
||
assert.calledOnce(session.deleteAllCookies); | ||
}); | ||
|
||
["localStorage", "sessionStorage"].forEach(storageName => { | ||
describe(storageName, () => { | ||
it("should clear", async () => { | ||
const session = mkSessionStub_(); | ||
session.execute.callsFake((cb: (storageName: string) => void, storageName: string) => cb(storageName)); | ||
|
||
await initBrowser_({ session }); | ||
await session.clearSession(); | ||
Check failure on line 59 in test/src/browser/commands/clearSession.ts GitHub Actions / build (18.x)
Check failure on line 59 in test/src/browser/commands/clearSession.ts GitHub Actions / build (20.x)
|
||
|
||
assert.calledOnce(global.window[storageName].clear); | ||
Check failure on line 61 in test/src/browser/commands/clearSession.ts GitHub Actions / build (18.x)
|
||
}); | ||
|
||
it("should not throw is storage is not available on the page", async () => { | ||
const err = new Error( | ||
`Failed to read the '${storageName}' property from 'Window': Storage is disabled inside 'data:' URLs.`, | ||
); | ||
global.window[storageName].clear.throws(err); | ||
Check failure on line 68 in test/src/browser/commands/clearSession.ts GitHub Actions / build (18.x)
|
||
|
||
const session = mkSessionStub_(); | ||
session.execute.callsFake((cb: (storageName: string) => void, storageName: string) => cb(storageName)); | ||
|
||
await initBrowser_({ session }); | ||
|
||
await assert.isFulfilled(session.clearSession()); | ||
Check failure on line 75 in test/src/browser/commands/clearSession.ts GitHub Actions / build (18.x)
Check failure on line 75 in test/src/browser/commands/clearSession.ts GitHub Actions / build (20.x)
|
||
assert.calledOnceWith(logger.warn, `Couldn't clear ${storageName}: ${err.message}`); | ||
}); | ||
|
||
it("should throw if clear storage fails with not handled error", async () => { | ||
const err = new Error("o.O"); | ||
global.window[storageName].clear.throws(err); | ||
Check failure on line 81 in test/src/browser/commands/clearSession.ts GitHub Actions / build (18.x)
|
||
|
||
const session = mkSessionStub_(); | ||
session.execute.callsFake((cb: (storageName: string) => void, storageName: string) => cb(storageName)); | ||
|
||
await initBrowser_({ session }); | ||
|
||
await assert.isRejected(session.clearSession(), /o.O/); | ||
Check failure on line 88 in test/src/browser/commands/clearSession.ts GitHub Actions / build (18.x)
Check failure on line 88 in test/src/browser/commands/clearSession.ts GitHub Actions / build (20.x)
|
||
}); | ||
}); | ||
}); | ||
}); |