|
| 1 | +import Container from '../container.js' |
| 2 | + |
| 3 | +const RESERVED_NAMES = new Set(['I', 'test', 'suite']) |
| 4 | +const SHORTHAND_PROPERTIES = new Set(['page', 'browser', 'browserContext', 'context']) |
| 5 | + |
| 6 | +const defaultConfig = { |
| 7 | + inject: {}, |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Exposes properties from helper instances as injectable test arguments. |
| 12 | + * Use it to access the underlying Playwright/Puppeteer `page`, the wdio `browser` client, |
| 13 | + * or any other helper internal directly from a Scenario: |
| 14 | + * |
| 15 | + * ```js |
| 16 | + * Scenario('listen for requests', async ({ I, page, browser }) => { |
| 17 | + * page.on('request', r => console.log(r.url())) |
| 18 | + * await page.evaluate(() => 1 + 1) |
| 19 | + * I.amOnPage('/') |
| 20 | + * }) |
| 21 | + * ``` |
| 22 | + * |
| 23 | + * The injected value is a live proxy: every property access reads the *current* |
| 24 | + * helper property, so mid-test reassignments (popups, `switchToNextTab`, |
| 25 | + * `openNewTab`) are reflected automatically. Calls are not wrapped as |
| 26 | + * CodeceptJS steps — `await page.evaluate(...)` runs as native Playwright. |
| 27 | + * |
| 28 | + * #### Configuration |
| 29 | + * |
| 30 | + * `inject` maps an injection name to a `HelperName.propertyName` string. A |
| 31 | + * value with no dot is shorthand for "first configured browser helper that |
| 32 | + * exposes this property" (allowed properties: `page`, `browser`, |
| 33 | + * `browserContext`, `context`). |
| 34 | + * |
| 35 | + * ```js |
| 36 | + * plugins: { |
| 37 | + * expose: { |
| 38 | + * enabled: true, |
| 39 | + * inject: { |
| 40 | + * page: 'Playwright.page', |
| 41 | + * browser: 'Playwright.browser', |
| 42 | + * browserContext: 'Playwright.browserContext', |
| 43 | + * frame: 'Playwright.context', // current frame set by switchTo |
| 44 | + * wdio: 'WebDriver.browser', |
| 45 | + * } |
| 46 | + * } |
| 47 | + * } |
| 48 | + * ``` |
| 49 | + * |
| 50 | + * Shorthand: |
| 51 | + * |
| 52 | + * ```js |
| 53 | + * plugins: { |
| 54 | + * expose: { |
| 55 | + * enabled: true, |
| 56 | + * inject: { |
| 57 | + * page: 'page', // resolves to Playwright.page or Puppeteer.page |
| 58 | + * } |
| 59 | + * } |
| 60 | + * } |
| 61 | + * ``` |
| 62 | + * |
| 63 | + * #### Caveats |
| 64 | + * |
| 65 | + * - The injected value is a `Proxy`, not the actual `Page`/`Browser` instance, |
| 66 | + * so `page instanceof Page` is `false`. Use duck typing instead. |
| 67 | + * - Cached method references lose the live binding. Call `page.click(...)`, |
| 68 | + * not `const click = page.click; click(...)`. |
| 69 | + * - In dry-run mode the underlying helper property is `undefined`; accessing |
| 70 | + * any property on the proxy returns `undefined` rather than throwing. |
| 71 | + */ |
| 72 | +export default function (config = {}) { |
| 73 | + config = { ...defaultConfig, ...config } |
| 74 | + |
| 75 | + const mappings = parseMappings(config.inject) |
| 76 | + |
| 77 | + const support = {} |
| 78 | + for (const [name, { helperName, property }] of Object.entries(mappings)) { |
| 79 | + support[name] = makeLiveProxy(helperName, property) |
| 80 | + } |
| 81 | + Container.append({ support }) |
| 82 | +} |
| 83 | + |
| 84 | +function parseMappings(inject) { |
| 85 | + const out = {} |
| 86 | + for (const [name, value] of Object.entries(inject || {})) { |
| 87 | + if (RESERVED_NAMES.has(name)) { |
| 88 | + throw new Error(`expose plugin: inject name '${name}' is reserved`) |
| 89 | + } |
| 90 | + if (typeof value !== 'string' || !value) { |
| 91 | + throw new Error(`expose plugin: inject value for '${name}' must be a non-empty string`) |
| 92 | + } |
| 93 | + |
| 94 | + let helperName |
| 95 | + let property |
| 96 | + |
| 97 | + if (value.includes('.')) { |
| 98 | + const dot = value.indexOf('.') |
| 99 | + helperName = value.slice(0, dot) |
| 100 | + property = value.slice(dot + 1) |
| 101 | + if (!helperName || !property) { |
| 102 | + throw new Error(`expose plugin: invalid inject value '${value}' for '${name}' (expected 'HelperName.propertyName')`) |
| 103 | + } |
| 104 | + if (!Container.helpers(helperName)) { |
| 105 | + throw new Error(`expose plugin: helper '${helperName}' is not configured (needed for inject '${name}')`) |
| 106 | + } |
| 107 | + } else { |
| 108 | + property = value |
| 109 | + if (!SHORTHAND_PROPERTIES.has(property)) { |
| 110 | + throw new Error(`expose plugin: shorthand '${property}' is not a known helper property for '${name}' (use 'HelperName.${property}' instead)`) |
| 111 | + } |
| 112 | + helperName = Container.STANDARD_ACTING_HELPERS.find(h => Container.helpers(h)) |
| 113 | + if (!helperName) { |
| 114 | + throw new Error(`expose plugin: no standard browser helper configured (needed for inject '${name}')`) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + out[name] = { helperName, property } |
| 119 | + } |
| 120 | + return out |
| 121 | +} |
| 122 | + |
| 123 | +function makeLiveProxy(helperName, property) { |
| 124 | + const resolve = () => Container.helpers(helperName)?.[property] |
| 125 | + return new Proxy(function () {}, { |
| 126 | + get(_, prop) { |
| 127 | + const target = resolve() |
| 128 | + if (target == null) return undefined |
| 129 | + const value = target[prop] |
| 130 | + if (typeof value === 'function') return value.bind(target) |
| 131 | + return value |
| 132 | + }, |
| 133 | + has(_, prop) { |
| 134 | + const target = resolve() |
| 135 | + return target != null && prop in target |
| 136 | + }, |
| 137 | + apply(_, thisArg, args) { |
| 138 | + const target = resolve() |
| 139 | + return target?.apply(thisArg, args) |
| 140 | + }, |
| 141 | + set(_, prop, value) { |
| 142 | + const target = resolve() |
| 143 | + if (target != null) target[prop] = value |
| 144 | + return true |
| 145 | + }, |
| 146 | + getPrototypeOf() { |
| 147 | + const target = resolve() |
| 148 | + return target != null ? Object.getPrototypeOf(target) : null |
| 149 | + }, |
| 150 | + ownKeys() { |
| 151 | + const target = resolve() |
| 152 | + return target != null ? Reflect.ownKeys(target) : [] |
| 153 | + }, |
| 154 | + getOwnPropertyDescriptor(_, prop) { |
| 155 | + const target = resolve() |
| 156 | + return target != null ? Object.getOwnPropertyDescriptor(target, prop) : undefined |
| 157 | + }, |
| 158 | + }) |
| 159 | +} |
0 commit comments