Best JavaScript code snippet using playwright-internal
fixtures.js
Source:fixtures.js
...105 }106 const fn = value;107 const previous = this.registrations.get(name);108 if (previous && options) {109 if (previous.scope !== options.scope) throw errorWithLocations(`Fixture "${name}" has already been registered as a { scope: '${previous.scope}' } fixture.`, {110 location,111 name112 }, previous);113 if (previous.auto !== options.auto) throw errorWithLocations(`Fixture "${name}" has already been registered as a { auto: '${previous.scope}' } fixture.`, {114 location,115 name116 }, previous);117 } else if (previous) {118 options = {119 auto: previous.auto,120 scope: previous.scope121 };122 } else if (!options) {123 options = {124 auto: false,125 scope: 'test'126 };127 }128 if (options.scope !== 'test' && options.scope !== 'worker') throw errorWithLocations(`Fixture "${name}" has unknown { scope: '${options.scope}' }.`, {129 location,130 name131 });132 if (options.scope === 'worker' && disallowWorkerFixtures) throw errorWithLocations(`Cannot use({ ${name} }) in a describe group, because it forces a new worker.\nMake it top-level in the test file or put in the configuration file.`, {133 location,134 name135 });136 const deps = fixtureParameterNames(fn, location);137 const registration = {138 id: '',139 name,140 location,141 scope: options.scope,142 fn,143 auto: options.auto,144 deps,145 super: previous146 };147 registrationId(registration);148 this.registrations.set(name, registration);149 }150 }151 this.digest = this.validate();152 }153 validate() {154 const markers = new Map();155 const stack = [];156 const visit = registration => {157 markers.set(registration, 'visiting');158 stack.push(registration);159 for (const name of registration.deps) {160 const dep = this.resolveDependency(registration, name);161 if (!dep) {162 if (name === registration.name) throw errorWithLocations(`Fixture "${registration.name}" references itself, but does not have a base implementation.`, registration);else throw errorWithLocations(`Fixture "${registration.name}" has unknown parameter "${name}".`, registration);163 }164 if (registration.scope === 'worker' && dep.scope === 'test') throw errorWithLocations(`Worker fixture "${registration.name}" cannot depend on a test fixture "${name}".`, registration, dep);165 if (!markers.has(dep)) {166 visit(dep);167 } else if (markers.get(dep) === 'visiting') {168 const index = stack.indexOf(dep);169 const regs = stack.slice(index, stack.length);170 const names = regs.map(r => `"${r.name}"`);171 throw errorWithLocations(`Fixtures ${names.join(' -> ')} -> "${dep.name}" form a dependency cycle.`, ...regs);172 }173 }174 markers.set(registration, 'visited');175 stack.pop();176 };177 const hash = crypto.createHash('sha1');178 const names = Array.from(this.registrations.keys()).sort();179 for (const name of names) {180 const registration = this.registrations.get(name);181 visit(registration);182 if (registration.scope === 'worker') hash.update(registration.id + ';');183 }184 return hash.digest('hex');185 }186 validateFunction(fn, prefix, location) {187 const visit = registration => {188 for (const name of registration.deps) visit(this.resolveDependency(registration, name));189 };190 for (const name of fixtureParameterNames(fn, location)) {191 const registration = this.registrations.get(name);192 if (!registration) throw errorWithLocations(`${prefix} has unknown parameter "${name}".`, {193 location,194 name: prefix,195 quoted: false196 });197 visit(registration);198 }199 }200 resolveDependency(registration, name) {201 if (name === registration.name) return registration.super;202 return this.registrations.get(name);203 }204}205exports.FixturePool = FixturePool;206class FixtureRunner {207 constructor() {208 this.testScopeClean = true;209 this.pool = void 0;210 this.instanceForId = new Map();211 }212 setPool(pool) {213 if (!this.testScopeClean) throw new Error('Did not teardown test scope');214 if (this.pool && pool.digest !== this.pool.digest) throw new Error('Digests do not match');215 this.pool = pool;216 }217 async teardownScope(scope) {218 let error; // Teardown fixtures in the reverse order.219 const fixtures = Array.from(this.instanceForId.values()).reverse();220 for (const fixture of fixtures) {221 if (fixture.registration.scope === scope) {222 try {223 await fixture.teardown();224 } catch (e) {225 if (error === undefined) error = e;226 }227 }228 }229 if (scope === 'test') this.testScopeClean = true;230 if (error !== undefined) throw error;231 }232 async resolveParametersAndRunHookOrTest(fn, workerInfo, testInfo, paramsStepCallback) {233 // Install all automatic fixtures.234 for (const registration of this.pool.registrations.values()) {235 const shouldSkip = !testInfo && registration.scope === 'test';236 if (registration.auto && !shouldSkip) await this.setupFixtureForRegistration(registration, workerInfo, testInfo);237 } // Install used fixtures.238 const names = fixtureParameterNames(fn, {239 file: '<unused>',240 line: 1,241 column: 1242 });243 const params = {};244 for (const name of names) {245 const registration = this.pool.registrations.get(name);246 const fixture = await this.setupFixtureForRegistration(registration, workerInfo, testInfo);247 params[name] = fixture.value;248 } // Report fixture hooks step as completed.249 paramsStepCallback === null || paramsStepCallback === void 0 ? void 0 : paramsStepCallback();250 return fn(params, testInfo || workerInfo);251 }252 async setupFixtureForRegistration(registration, workerInfo, testInfo) {253 if (registration.scope === 'test') this.testScopeClean = false;254 let fixture = this.instanceForId.get(registration.id);255 if (fixture) return fixture;256 fixture = new Fixture(this, registration);257 this.instanceForId.set(registration.id, fixture);258 await fixture.setup(workerInfo, testInfo);259 return fixture;260 }261 dependsOnWorkerFixturesOnly(fn, location) {262 const names = fixtureParameterNames(fn, location);263 for (const name of names) {264 const registration = this.pool.registrations.get(name);265 if (registration.scope !== 'worker') return false;266 }267 return true;268 }269}270exports.FixtureRunner = FixtureRunner;271const signatureSymbol = Symbol('signature');272function fixtureParameterNames(fn, location) {273 if (typeof fn !== 'function') return [];274 if (!fn[signatureSymbol]) fn[signatureSymbol] = innerFixtureParameterNames(fn, location);275 return fn[signatureSymbol];276}277function innerFixtureParameterNames(fn, location) {278 const text = fn.toString();279 const match = text.match(/(?:async)?(?:\s+function)?[^(]*\(([^)]*)/);280 if (!match) return [];281 const trimmedParams = match[1].trim();282 if (!trimmedParams) return [];283 const [firstParam] = splitByComma(trimmedParams);284 if (firstParam[0] !== '{' || firstParam[firstParam.length - 1] !== '}') throw errorWithLocations('First argument must use the object destructuring pattern: ' + firstParam, {285 location286 });287 const props = splitByComma(firstParam.substring(1, firstParam.length - 1)).map(prop => {288 const colon = prop.indexOf(':');289 return colon === -1 ? prop : prop.substring(0, colon).trim();290 });291 return props;292}293function splitByComma(s) {294 const result = [];295 const stack = [];296 let start = 0;297 for (let i = 0; i < s.length; i++) {298 if (s[i] === '{' || s[i] === '[') {299 stack.push(s[i] === '{' ? '}' : ']');300 } else if (s[i] === stack[stack.length - 1]) {301 stack.pop();302 } else if (!stack.length && s[i] === ',') {303 const token = s.substring(start, i).trim();304 if (token) result.push(token);305 start = i + 1;306 }307 }308 const lastToken = s.substring(start).trim();309 if (lastToken) result.push(lastToken);310 return result;311} // name + superId, fn -> id312const registrationIdMap = new Map();313let lastId = 0;314function registrationId(registration) {315 if (registration.id) return registration.id;316 const key = registration.name + '@@@' + (registration.super ? registrationId(registration.super) : '');317 let map = registrationIdMap.get(key);318 if (!map) {319 map = new Map();320 registrationIdMap.set(key, map);321 }322 if (!map.has(registration.fn)) map.set(registration.fn, String(lastId++));323 registration.id = map.get(registration.fn);324 return registration.id;325}326function errorWithLocations(message, ...defined) {327 for (const {328 name,329 location,330 quoted331 } of defined) {332 let prefix = '';333 if (name && quoted === false) prefix = name + ' ';else if (name) prefix = `"${name}" `;334 message += `\n ${prefix}defined at ${(0, _util.formatLocation)(location)}`;335 }336 return new Error(message);...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!