Best JavaScript code snippet using playwright-internal
selectorParser.js
Source: selectorParser.js
...109 const prefix = p.name === 'css' ? '' : p.name + '=';110 return `${i === selector.capture ? '*' : ''}${prefix}${p.source}`;111 }).join(' >> ');112}113function allEngineNames(selector) {114 const result = new Set();115 const visit = selector => {116 for (const part of selector.parts) {117 result.add(part.name);118 if (kNestedSelectorNames.has(part.name)) visit(part.body);119 }120 };121 visit(selector);122 return result;123}124function parseSelectorString(selector) {125 let index = 0;126 let quote;127 let start = 0;...
selectors.js
Source: selectors.js
1"use strict";2Object.defineProperty(exports, "__esModule", {3 value: true4});5exports.Selectors = void 0;6var _selectorParser = require("./common/selectorParser");7var _utils = require("../utils/utils");8/**9 * Copyright (c) Microsoft Corporation.10 *11 * Licensed under the Apache License, Version 2.0 (the "License");12 * you may not use this file except in compliance with the License.13 * You may obtain a copy of the License at14 *15 * http://www.apache.org/licenses/LICENSE-2.016 *17 * Unless required by applicable law or agreed to in writing, software18 * distributed under the License is distributed on an "AS IS" BASIS,19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.20 * See the License for the specific language governing permissions and21 * limitations under the License.22 */23class Selectors {24 constructor() {25 this._builtinEngines = void 0;26 this._builtinEnginesInMainWorld = void 0;27 this._engines = void 0;28 this.guid = `selectors@${(0, _utils.createGuid)()}`;29 // Note: keep in sync with InjectedScript class.30 this._builtinEngines = new Set(['css', 'css:light', 'xpath', 'xpath:light', '_react', '_vue', 'text', 'text:light', 'id', 'id:light', 'data-testid', 'data-testid:light', 'data-test-id', 'data-test-id:light', 'data-test', 'data-test:light', 'nth', 'visible', 'control', 'has']);31 this._builtinEnginesInMainWorld = new Set(['_react', '_vue']);32 this._engines = new Map();33 }34 async register(name, source, contentScript = false) {35 if (!name.match(/^[a-zA-Z_0-9-]+$/)) throw new Error('Selector engine name may only contain [a-zA-Z0-9_] characters'); // Note: we keep 'zs' for future use.36 if (this._builtinEngines.has(name) || name === 'zs' || name === 'zs:light') throw new Error(`"${name}" is a predefined selector engine`);37 if (this._engines.has(name)) throw new Error(`"${name}" selector engine has been already registered`);38 this._engines.set(name, {39 source,40 contentScript41 });42 }43 unregisterAll() {44 this._engines.clear();45 }46 async query(frame, info, scope) {47 const context = await frame._context(info.world);48 const injectedScript = await context.injectedScript();49 const handle = await injectedScript.evaluateHandle((injected, {50 parsed,51 scope,52 strict53 }) => {54 return injected.querySelector(parsed, scope || document, strict);55 }, {56 parsed: info.parsed,57 scope,58 strict: info.strict59 });60 const elementHandle = handle.asElement();61 if (!elementHandle) {62 handle.dispose();63 return null;64 }65 const mainContext = await frame._mainContext();66 return this._adoptIfNeeded(elementHandle, mainContext);67 }68 async _queryArrayInMainWorld(frame, info, scope) {69 const context = await frame._mainContext();70 const injectedScript = await context.injectedScript();71 const arrayHandle = await injectedScript.evaluateHandle((injected, {72 parsed,73 scope74 }) => {75 return injected.querySelectorAll(parsed, scope || document);76 }, {77 parsed: info.parsed,78 scope79 });80 return arrayHandle;81 }82 async _queryCount(frame, info, scope) {83 const context = await frame._context(info.world);84 const injectedScript = await context.injectedScript();85 return await injectedScript.evaluate((injected, {86 parsed,87 scope88 }) => {89 return injected.querySelectorAll(parsed, scope || document).length;90 }, {91 parsed: info.parsed,92 scope93 });94 }95 async _queryAll(frame, selector, scope, adoptToMain) {96 const info = typeof selector === 'string' ? frame._page.parseSelector(selector) : selector;97 const context = await frame._context(info.world);98 const injectedScript = await context.injectedScript();99 const arrayHandle = await injectedScript.evaluateHandle((injected, {100 parsed,101 scope102 }) => {103 return injected.querySelectorAll(parsed, scope || document);104 }, {105 parsed: info.parsed,106 scope107 });108 const properties = await arrayHandle.getProperties();109 arrayHandle.dispose(); // Note: adopting elements one by one may be slow. If we encounter the issue here,110 // we might introduce 'useMainContext' option or similar to speed things up.111 const targetContext = adoptToMain ? await frame._mainContext() : context;112 const result = [];113 for (const property of properties.values()) {114 const elementHandle = property.asElement();115 if (elementHandle) result.push(this._adoptIfNeeded(elementHandle, targetContext));else property.dispose();116 }117 return Promise.all(result);118 }119 async _adoptIfNeeded(handle, context) {120 if (handle._context === context) return handle;121 const adopted = handle._page._delegate.adoptElementHandle(handle, context);122 handle.dispose();123 return adopted;124 }125 parseSelector(selector, strict) {126 const parsed = typeof selector === 'string' ? (0, _selectorParser.parseSelector)(selector) : selector;127 let needsMainWorld = false;128 for (const name of (0, _selectorParser.allEngineNames)(parsed)) {129 const custom = this._engines.get(name);130 if (!custom && !this._builtinEngines.has(name)) throw new _selectorParser.InvalidSelectorError(`Unknown engine "${name}" while parsing selector ${(0, _selectorParser.stringifySelector)(parsed)}`);131 if (custom && !custom.contentScript) needsMainWorld = true;132 if (this._builtinEnginesInMainWorld.has(name)) needsMainWorld = true;133 }134 return {135 parsed,136 world: needsMainWorld ? 'main' : 'utility',137 strict138 };139 }140}...
empty.js
Source: empty.js
1const chalk = require('chalk');2const engines = require('../../engines');3const runtimeConfig = require('../../runtime/config');4const storageEngine = require('../../storage/engine');5const { printUserErrorAndDie, didYouMean } = require('../../utils');6const snapshots = require('../../storage/snapshots');7const utils = require('../../utils');8async function addEmptySnapshot(engineName, snapshotName, cliRuntimeConfig, persist) {9 const snapshotExists = await snapshots.getSnapshot(snapshotName);10 if (snapshotExists) {11 printUserErrorAndDie(`Snapshot ${chalk.yellow(snapshotName)} already exists`);12 }13 const allEngineNames = await engines.getAllEngineNames();14 const engineExists = allEngineNames.some(installedEngineName => installedEngineName === engineName);15 if (!engineExists) {16 didYouMean(engineName, await engines.getAllEngineNames(), `Engine`);17 }18 const engineRuntimeConfig = await storageEngine.getEngineRuntimeConfig(engineName);19 const snapshotRuntimeConfig = {20 ...engineRuntimeConfig.runtimeConfigSpec,21 ...cliRuntimeConfig,22 };23 const { port } = snapshotRuntimeConfig;24 console.log(`${chalk.green(`Starting`)} empty snapshot ${chalk.cyanBright(snapshotName)} on port ${chalk.cyanBright(port)}`);25 const newSnapshot = await snapshots.createEmptySnapshot(snapshotName, engineName, snapshotRuntimeConfig);26 console.log(`${chalk.green(`Successfully`)} shut down empty snapshot ${chalk.cyanBright(snapshotName)}`);27 if (persist) {28 await snapshots.appendRuntimeConfig(newSnapshot, cliRuntimeConfig);29 }30}31const usage = `32Usage: sider snapshot empty [options] <engine> <name> [parameters...]33Starts and then saves an empty snapshot34Options:35 -p, --persist Persist the parameters36 -h, --help output usage information37`;38async function processArgv(argv = []) {39 utils.printUsageIfHelp(argv, usage);40 const { hasArgument: persist, rest } = utils.containsArguments(argv, '-p', '--persist');41 const [engineName, snapshotName, ...runtimeConfigKeyValues] = rest;42 if (!snapshotName) {43 utils.printUserErrorAndDie(`Missing the name of the snapshot (parameter <name>)`);44 }45 if (persist && !runtimeConfigKeyValues.length) {46 utils.printWarning(`Persist flag set but no runtime parameters (e g -p but no port=666)`);47 }48 const cliRuntimeConfig = runtimeConfig.parseRuntimeConfigKeyValues(runtimeConfigKeyValues);49 return addEmptySnapshot(engineName, snapshotName, cliRuntimeConfig, persist);50}51module.exports = {52 processArgv,...
apiController.ts
Source: apiController.ts
1#!/usr/bin/env node2// use process.parameters here, first two args are the node command elements (node, script path)3import relayEgress from '../relays/egressRelay';4import relayIngress from '../relays/ingressRelay';5import { BaseEngine } from 'refinery-core';6import { ExpectedParametersEgress, ExpectedParametersIngress } from './interfaces';7import { BaseController } from './baseController';8export class ApiController extends BaseController {9 private _username: string | undefined;10 private _password: string | undefined;11 constructor(username?: string, password?: string) {12 super();13 this._username = username;14 this._password = password;15 }16 // config assumed to be specified on deployment of the server17 // or default used; no reconfiguration possibility from18 // client's level19 relayClosureEgress(20 engine: BaseEngine,21 parameters: Exclude<ExpectedParametersEgress, { config: string }>22 ) {23 return relayEgress(24 engine,25 parameters.path,26 parameters.batch,27 parameters.notebook,28 parameters.diff,29 parameters.flipped30 );31 }32 relayClosureIngress(33 engine: BaseEngine,34 parameters: Exclude<ExpectedParametersIngress, { config: string }> 35 & Required<Pick<ExpectedParametersIngress, "resource">>36 ) {37 return relayIngress(38 engine,39 parameters.resource,40 parameters.batch,41 parameters.notebook42 );43 }44 refineIn(parameters: Exclude<ExpectedParametersIngress, { config: string }> 45 & Required<Pick<ExpectedParametersIngress, "resource">>) {46 let idx = this.allEngineNames.findIndex((val)=>{return val === <string>parameters.what});47 this.relayClosureIngress(48 new this.allEngines[idx](this._username, this._password, this.config),49 parameters50 );51 }52 53 refineOut(parameters: Exclude<ExpectedParametersEgress, { config: string }>) {54 let idx = this.allEngineNames.findIndex((val)=>{return val === <string>parameters.what});55 this.relayClosureEgress(56 new this.allEngines[idx](this._username, this._password, this.config),57 parameters58 );59 }60}...
egressController.ts
Source: egressController.ts
1#!/usr/bin/env node2// use process.argv here, first two args are the node command elements (node, script path)3import relay from '../relays/egressRelay';4import yargs from 'yargs';5import { DEFAULT_CONFIG_PATH } from 'refinery-core';6import { BaseEngine } from 'refinery-core';7import { BaseController } from './baseController';8const baseCtl = new BaseController();9const argv = yargs.options(10 {11 what: baseCtl.whatEgress,12 path: {13 type: 'string',14 demandOption: true15 },16 diff: {17 type: 'number',18 demandOption: false,19 default: undefined20 },21 flipped: {22 type: 'boolean',23 demandOption: false,24 default: false25 },26 ...baseCtl.commonOptions27 }28).argv;29if (argv.config !== undefined) {30 var config = <string>argv.config;31} else {32 var config = DEFAULT_CONFIG_PATH33}34const relayClosure = (engine: BaseEngine) => {35 relay(36 engine,37 argv.path,38 <string>argv.batch,39 <string>argv.notebook,40 argv.diff,41 argv.flipped42 );43}44// the following finds the correct engine based on argv.what45// and relays instantiation and Egress call46let idx = baseCtl.allEngineNames.findIndex((val)=>{return val === <string>argv.what});47relayClosure(48 new baseCtl.allEngines[idx](<string>argv.user, <string>argv.password, config)...
ingressController.ts
Source: ingressController.ts
1#!/usr/bin/env node2import relay from '../relays/ingressRelay';3import { BaseEngine } from 'refinery-core';4import { DEFAULT_CONFIG_PATH } from 'refinery-core';5import yargs from 'yargs';6import { BaseController } from './baseController';7const baseCtl = new BaseController();8const argv = yargs.options(9 {10 what: baseCtl.whatIngress,11 resource: {12 type: 'string',13 demandOption: false14 }15 }16).argv;17if (argv.config !== undefined) {18 var config = <string>argv.config;19} else {20 var config = DEFAULT_CONFIG_PATH21}22const relayClosure = (engine: BaseEngine) => {23 relay(24 engine,25 <string>argv.resource,26 <string>argv.batch,27 <string>argv.notebook28 );29}30let idx = baseCtl.allEngineNames.findIndex((val)=>{return val === <string>argv.what});31relayClosure(32 new baseCtl.allEngines[idx](<string>argv.user, <string>argv.password, config)...
Using AI Code Generation
1const { Internal } = require('playwright');2console.log(Internal.allEngineNames());3const { Internal } = require('playwright');4console.log(Internal.allEngineNames());5const { Internal } = require('playwright');6console.log(Internal.allEngineNames());7const { Internal } = require('playwright');8console.log(Internal.allEngineNames());9const { Internal } = require('playwright');10console.log(Internal.allEngineNames());11const { Internal } = require('playwright');12console.log(Internal.allEngineNames());13const { Internal } = require('playwright');14console.log(Internal.allEngineNames());15const { Internal } = require('playwright');16console.log(Internal.allEngineNames());17const { Internal } = require('playwright');18console.log(Internal.allEngineNames());19const { Internal } = require('playwright');20console.log(Internal.allEngineNames());21const { Internal } = require('playwright');22console.log(Internal.allEngineNames());23const { Internal } = require('playwright');24console.log(Internal.allEngineNames());25const { Internal } = require('playwright');26console.log(Internal.allEngineNames());27const { Internal
Using AI Code Generation
1const { allEngineNames } = require('playwright/lib/server/browserType');2console.log(allEngineNames());3const { allEngineNames } = require('playwright/lib/server/browserType');4console.log(allEngineNames());5const { allEngineNames } = require('playwright/lib/server/browserType');6console.log(allEngineNames());7const { allEngineNames } = require('playwright/lib/server/browserType');8console.log(allEngineNames());9const { allEngineNames } = require('playwright/lib/server/browserType');10console.log(allEngineNames());11const { allEngineNames } = require('playwright/lib/server/browserType');12console.log(allEngineNames());13const { allEngineNames } = require('playwright/lib/server/browserType');14console.log(allEngineNames());15const { allEngineNames } = require('playwright/lib/server/browserType');16console.log(allEngineNames());17const { allEngineNames } = require('playwright/lib/server/browserType');18console.log(allEngineNames());19const { allEngineNames } = require('playwright/lib/server/browserType');20console.log(allEngineNames());21const { allEngineNames } = require('playwright/lib/server/browserType');22console.log(allEngineNames());23const { allEngineNames } = require('playwright/lib/server/browserType');24console.log(allEngineNames());
Using AI Code Generation
1const { allEngineNames } = require('playwright-core/lib/server/playwright');2console.log(allEngineNames());3const { allEngines } = require('playwright-core/lib/server/playwright');4console.log(allEngines());5const { createPlaywright } = require('playwright-core/lib/server/playwright');6console.log(createPlaywright());7const { createPlaywright } = require('playwright-core/lib/server/playwright');8console.log(createPlaywright());9const { createPlaywright } = require('playwright-core/lib/server/playwright');10console.log(createPlaywright());11const { createPlaywright } = require('playwright-core/lib/server/playwright');12console.log(createPlaywright());13const { createPlaywright } = require('playwright-core/lib/server/playwright');14console.log(createPlaywright());15const { createPlaywright } = require('playwright-core/lib/server/playwright');16console.log(createPlaywright());17const { createPlaywright } = require('playwright-core/lib/server/playwright');18console.log(createPlaywright());19const { createPlaywright } = require('playwright-core/lib/server/playwright');20console.log(createPlaywright());21const { createPlaywright } = require('playwright-core/lib/server/playwright');22console.log(createPlaywright());23const { createPlaywright } = require('playwright-core/lib/server/playwright');24console.log(createPlaywright());25const { createPlaywright } = require('playwright-core/lib/server/playwright');26console.log(createPlaywright());27const { createPlaywright } = require('playwright-core/lib/server/playwright');28console.log(createPlaywright());
Using AI Code Generation
1const { allEngineNames } = require('playwright-core/lib/server/registry');2const engines = allEngineNames();3console.log(engines);4const engines = require('playwright-core/lib/server/registry').allEngineNames();5console.log(engines);6const { allEngineNames } = require('playwright-core/lib/server/registry');7const engines = allEngineNames();8console.log(engines);9const engines = require('playwright-core/lib/server/registry').allEngineNames();10console.log(engines);11const { allEngineNames } = require('playwright-core/lib/server/registry');12const engines = allEngineNames();13console.log(engines);14const engines = require('playwright-core/lib/server/registry').allEngineNames();15console.log(engines);16const { allEngineNames } = require('playwright-core/lib/server/registry');17const engines = allEngineNames();18console.log(engines);19const engines = require('playwright-core/lib/server/registry').allEngineNames();20console.log(engines);21const { allEngineNames } = require('playwright-core/lib/server/registry');22const engines = allEngineNames();23console.log(engines);24const engines = require('playwright-core/lib/server/registry').allEngineNames();25console.log(engines);26const { allEngineNames } = require('playwright-core/lib/server/registry');27const engines = allEngineNames();28console.log(engines);
Using AI Code Generation
1const { allEngineNames } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');2const engines = allEngineNames();3console.log(engines);4const { allEngineNames } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');5const engines = allEngineNames();6console.log(engines);7const { allEngineNames } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');8const engines = allEngineNames();9console.log(engines);10const { allEngineNames } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');11const engines = allEngineNames();12console.log(engines);13const { allEngineNames } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');14const engines = allEngineNames();15console.log(engines);16const { allEngineNames } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');17const engines = allEngineNames();18console.log(engines);19const { allEngineNames } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');20const engines = allEngineNames();21console.log(engines);22const { allEngineNames } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');23const engines = allEngineNames();24console.log(engines);25const { allEngineNames } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');26const engines = allEngineNames();
Using AI Code Generation
1const { Playwright } = require('playwright');2const playwright = new Playwright();3playwright.allEngineNames().then((engines) => {4 console.log(engines);5});6const { Playwright } = require('playwright');7const playwright = new Playwright();8playwright.allEngineNames().then((engines) => {9 console.log(engines);10});11const { Playwright } = require('playwright');12const playwright = new Playwright();13playwright.allEngineNames().then((engines) => {14 console.log(engines);15});16const { Playwright } = require('playwright');17const playwright = new Playwright();18playwright.allEngineNames().then((engines) => {19 console.log(engines);20});21const { Playwright } = require('playwright');22const playwright = new Playwright();23playwright.allEngineNames().then((engines) => {24 console.log(engines);25});26const { Playwright } = require('playwright');27const playwright = new Playwright();28playwright.allEngineNames().then((engines) => {29 console.log(engines);30});31const { Playwright } = require('playwright');32const playwright = new Playwright();33playwright.allEngineNames().then((engines) => {34 console.log(engines);
Using AI Code Generation
1const { allEngineNames } = require('playwright-core/lib/server/playwright');2(async () => {3 console.log(await allEngineNames());4})();5const { allEngineNames } = require('playwright-core/lib/server/playwright');6(async () => {7 console.log(await allEngineNames());8})();9const { allEngineNames } = require('playwright-core/lib/server/playwright');10(async () => {11 console.log(await allEngineNames());12})();13const { allEngineNames } = require('playwright-core/lib/server/playwright');14(async () => {15 console.log(await allEngineNames());16})();17const { allEngineNames } = require('playwright-core/lib/server/playwright');18(async () => {19 console.log(await allEngineNames());20})();21const { allEngineNames } = require('playwright-core/lib/server/playwright');22(async () => {23 console.log(await allEngineNames());24})();25const { allEngineNames } = require('playwright-core/lib/server/playwright');26(async () => {27 console.log(await allEngineNames());28})();29const { allEngineNames } = require('playwright-core/lib/server/playwright');30(async () => {31 console.log(await allEngineNames());32})();33const { allEngineNames } = require('playwright-core/lib/server/playwright');34(async () => {35 console.log(await allEngineNames());36})();
Jest + Playwright - Test callbacks of event-based DOM library
firefox browser does not start in playwright
Is it possible to get the selector from a locator object in playwright?
How to run a list of test suites in a single file concurrently in jest?
Running Playwright in Azure Function
firefox browser does not start in playwright
This question is quite close to a "need more focus" question. But let's try to give it some focus:
Does Playwright has access to the cPicker object on the page? Does it has access to the window object?
Yes, you can access both cPicker and the window object inside an evaluate call.
Should I trigger the events from the HTML file itself, and in the callbacks, print in the DOM the result, in some dummy-element, and then infer from that dummy element text that the callbacks fired?
Exactly, or you can assign values to a javascript variable:
const cPicker = new ColorPicker({
onClickOutside(e){
},
onInput(color){
window['color'] = color;
},
onChange(color){
window['result'] = color;
}
})
And then
it('Should call all callbacks with correct arguments', async() => {
await page.goto(`http://localhost:5000/tests/visual/basic.html`, {waitUntil:'load'})
// Wait until the next frame
await page.evaluate(() => new Promise(requestAnimationFrame))
// Act
// Assert
const result = await page.evaluate(() => window['color']);
// Check the value
})
Check out the latest blogs from LambdaTest on this topic:
Native apps are developed specifically for one platform. Hence they are fast and deliver superior performance. They can be downloaded from various app stores and are not accessible through browsers.
One of the essential parts when performing automated UI testing, whether using Selenium or another framework, is identifying the correct web elements the tests will interact with. However, if the web elements are not located correctly, you might get NoSuchElementException in Selenium. This would cause a false negative result because we won’t get to the actual functionality check. Instead, our test will fail simply because it failed to interact with the correct element.
Smartphones have changed the way humans interact with technology. Be it travel, fitness, lifestyle, video games, or even services, it’s all just a few touches away (quite literally so). We only need to look at the growing throngs of smartphone or tablet users vs. desktop users to grasp this reality.
As part of one of my consulting efforts, I worked with a mid-sized company that was looking to move toward a more agile manner of developing software. As with any shift in work style, there is some bewilderment and, for some, considerable anxiety. People are being challenged to leave their comfort zones and embrace a continuously changing, dynamic working environment. And, dare I say it, testing may be the most ‘disturbed’ of the software roles in agile development.
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!!