UNPKG

expect-webdriverio

Version:

WebdriverIO Assertion Library

37 lines (36 loc) 1.4 kB
import { waitUntil, enhanceError, compareText } from '../../utils.js'; import { DEFAULT_OPTIONS } from '../../constants.js'; export async function toHaveLocalStorageItem(browser, key, expectedValue, options = DEFAULT_OPTIONS) { const isNot = this.isNot; const { expectation = 'localStorage item', verb = 'have' } = this; await options.beforeAssertion?.({ matcherName: 'toHaveLocalStorageItem', expectedValue: expectedValue ? [key, expectedValue] : key, options, }); let actual; const pass = await waitUntil(async () => { actual = await browser.execute((storageKey) => { return localStorage.getItem(storageKey); }, key); if (expectedValue === undefined) { return actual !== null; } if (actual === null) { return false; } return compareText(actual, expectedValue, options).result; }, isNot, options); const message = enhanceError('browser', expectedValue !== undefined ? expectedValue : `localStorage item "${key}"`, actual, this, verb, expectation, key, options); const result = { pass, message: () => message }; await options.afterAssertion?.({ matcherName: 'toHaveLocalStorageItem', expectedValue: expectedValue ? [key, expectedValue] : key, options, result }); return result; }