Best JavaScript code snippet using wpt
index.test.js
Source:index.test.js
1const test = require('ava')2const { vanillaPuppeteer } = require('../../test/util')3const utils = require('.')4const withUtils = require('./withUtils')5/* global HTMLMediaElement WebGLRenderingContext */6test('splitObjPath: will do what it says', async t => {7 const { objName, propName } = utils.splitObjPath(8 'HTMLMediaElement.prototype.canPlayType'9 )10 t.is(objName, 'HTMLMediaElement.prototype')11 t.is(propName, 'canPlayType')12})13test('makeNativeString: will do what it says', async t => {14 utils.init()15 t.is(utils.makeNativeString('bob'), 'function bob() { [native code] }')16 t.is(17 utils.makeNativeString('toString'),18 'function toString() { [native code] }'19 )20 t.is(utils.makeNativeString(), 'function () { [native code] }')21})22test('replaceWithProxy: will work correctly', async t => {23 const browser = await vanillaPuppeteer.launch({ headless: true })24 const page = await browser.newPage()25 const test1 = await withUtils(page).evaluate(utils => {26 const dummyProxyHandler = {27 get(target, param) {28 if (param && param === 'ping') {29 return 'pong'30 }31 return utils.cache.Reflect.get(...(arguments || []))32 },33 apply() {34 return utils.cache.Reflect.apply(...arguments)35 }36 }37 utils.replaceWithProxy(38 HTMLMediaElement.prototype,39 'canPlayType',40 dummyProxyHandler41 )42 return {43 toString: HTMLMediaElement.prototype.canPlayType.toString(),44 ping: HTMLMediaElement.prototype.canPlayType.ping45 }46 })47 t.deepEqual(test1, {48 toString: 'function canPlayType() { [native code] }',49 ping: 'pong'50 })51})52test('replaceObjPathWithProxy: will work correctly', async t => {53 const browser = await vanillaPuppeteer.launch({ headless: true })54 const page = await browser.newPage()55 const test1 = await withUtils(page).evaluate(utils => {56 const dummyProxyHandler = {57 get(target, param) {58 if (param && param === 'ping') {59 return 'pong'60 }61 return utils.cache.Reflect.get(...(arguments || []))62 },63 apply() {64 return utils.cache.Reflect.apply(...arguments)65 }66 }67 utils.replaceObjPathWithProxy(68 'HTMLMediaElement.prototype.canPlayType',69 dummyProxyHandler70 )71 return {72 toString: HTMLMediaElement.prototype.canPlayType.toString(),73 ping: HTMLMediaElement.prototype.canPlayType.ping74 }75 })76 t.deepEqual(test1, {77 toString: 'function canPlayType() { [native code] }',78 ping: 'pong'79 })80})81test('redirectToString: is battle hardened', async t => {82 const browser = await vanillaPuppeteer.launch({ headless: true })83 const page = await browser.newPage()84 // Patch all documents including iframes85 await withUtils(page).evaluateOnNewDocument(utils => {86 // We redirect toString calls targeted at `canPlayType` to `getParameter`,87 // so if everything works correctly we expect `getParameter` as response.88 const proxyObj = HTMLMediaElement.prototype.canPlayType89 const originalObj = WebGLRenderingContext.prototype.getParameter90 utils.redirectToString(proxyObj, originalObj)91 })92 await page.goto('about:blank')93 const result = await withUtils(page).evaluate(utils => {94 const iframe = document.createElement('iframe')95 document.body.appendChild(iframe)96 return {97 target: {98 raw: HTMLMediaElement.prototype.canPlayType + '',99 rawiframe:100 iframe.contentWindow.HTMLMediaElement.prototype.canPlayType + '',101 raw2: HTMLMediaElement.prototype.canPlayType.toString(),102 rawiframe2:103 iframe.contentWindow.HTMLMediaElement.prototype.canPlayType.toString(),104 direct: Function.prototype.toString.call(105 HTMLMediaElement.prototype.canPlayType106 ),107 directWithiframe: iframe.contentWindow.Function.prototype.toString.call(108 HTMLMediaElement.prototype.canPlayType109 ),110 iframeWithdirect: Function.prototype.toString.call(111 iframe.contentWindow.HTMLMediaElement.prototype.canPlayType112 ),113 iframeWithiframe: iframe.contentWindow.Function.prototype.toString.call(114 iframe.contentWindow.HTMLMediaElement.prototype.canPlayType115 )116 },117 toString: {118 obj: HTMLMediaElement.prototype.canPlayType.toString + '',119 objiframe:120 iframe.contentWindow.HTMLMediaElement.prototype.canPlayType.toString +121 '',122 raw: Function.prototype.toString + '',123 rawiframe: iframe.contentWindow.Function.prototype.toString + '',124 direct: Function.prototype.toString.call(Function.prototype.toString),125 directWithiframe: iframe.contentWindow.Function.prototype.toString.call(126 Function.prototype.toString127 ),128 iframeWithdirect: Function.prototype.toString.call(129 iframe.contentWindow.Function.prototype.toString130 ),131 iframeWithiframe: iframe.contentWindow.Function.prototype.toString.call(132 iframe.contentWindow.Function.prototype.toString133 )134 }135 }136 })137 t.deepEqual(result, {138 target: {139 raw: 'function getParameter() { [native code] }',140 raw2: 'function getParameter() { [native code] }',141 rawiframe: 'function getParameter() { [native code] }',142 rawiframe2: 'function getParameter() { [native code] }',143 direct: 'function getParameter() { [native code] }',144 directWithiframe: 'function getParameter() { [native code] }',145 iframeWithdirect: 'function getParameter() { [native code] }',146 iframeWithiframe: 'function getParameter() { [native code] }'147 },148 toString: {149 obj: 'function toString() { [native code] }',150 objiframe: 'function toString() { [native code] }',151 raw: 'function toString() { [native code] }',152 rawiframe: 'function toString() { [native code] }',153 direct: 'function toString() { [native code] }',154 directWithiframe: 'function toString() { [native code] }',155 iframeWithdirect: 'function toString() { [native code] }',156 iframeWithiframe: 'function toString() { [native code] }'157 }158 })159})160test('redirectToString: has proper errors', async t => {161 const browser = await vanillaPuppeteer.launch({ headless: true })162 const page = await browser.newPage()163 // Patch all documents including iframes164 await withUtils(page).evaluateOnNewDocument(utils => {165 // We redirect toString calls targeted at `canPlayType` to `getParameter`,166 // so if everything works correctly we expect `getParameter` as response.167 const proxyObj = HTMLMediaElement.prototype.canPlayType168 const originalObj = WebGLRenderingContext.prototype.getParameter169 utils.redirectToString(proxyObj, originalObj)170 })171 await page.goto('about:blank')172 const result = await withUtils(page).evaluate(utils => {173 const evalErr = (str = '') => {174 try {175 // eslint-disable-next-line no-eval176 return eval(str)177 } catch (err) {178 return err.toString()179 }180 }181 return {182 blank: evalErr(`Function.prototype.toString.apply()`),183 null: evalErr(`Function.prototype.toString.apply(null)`),184 undef: evalErr(`Function.prototype.toString.apply(undefined)`),185 emptyObject: evalErr(`Function.prototype.toString.apply({})`)186 }187 })188 t.deepEqual(result, {189 blank:190 "TypeError: Function.prototype.toString requires that 'this' be a Function",191 null: "TypeError: Function.prototype.toString requires that 'this' be a Function",192 undef:193 "TypeError: Function.prototype.toString requires that 'this' be a Function",194 emptyObject:195 "TypeError: Function.prototype.toString requires that 'this' be a Function"196 })197})198test('patchToString: will work correctly', async t => {199 const browser = await vanillaPuppeteer.launch({ headless: true })200 const page = await browser.newPage()201 // Test verbatim string replacement202 const test1 = await withUtils(page).evaluate(utils => {203 utils.patchToString(HTMLMediaElement.prototype.canPlayType, 'bob')204 return HTMLMediaElement.prototype.canPlayType.toString()205 })206 t.is(test1, 'bob')207 // Test automatic mode derived from `.name`208 const test2 = await withUtils(page).evaluate(utils => {209 utils.patchToString(HTMLMediaElement.prototype.canPlayType)210 return HTMLMediaElement.prototype.canPlayType.toString()211 })212 t.is(test2, 'function canPlayType() { [native code] }')213 // Make sure automatic mode derived from `.name` works with proxies214 const test3 = await withUtils(page).evaluate(utils => {215 HTMLMediaElement.prototype.canPlayType = new Proxy(216 HTMLMediaElement.prototype.canPlayType,217 {}218 )219 utils.patchToString(HTMLMediaElement.prototype.canPlayType)220 return HTMLMediaElement.prototype.canPlayType.toString()221 })222 t.is(test3, 'function canPlayType() { [native code] }')223 // Actually verify there's an issue when using vanilla Proxies224 const test4 = await withUtils(page).evaluate(utils => {225 HTMLMediaElement.prototype.canPlayType = new Proxy(226 HTMLMediaElement.prototype.canPlayType,227 {}228 )229 return HTMLMediaElement.prototype.canPlayType.toString()230 })231 t.is(test4, 'function () { [native code] }')232})233function toStringTest(obj) {234 obj = eval(obj) // eslint-disable-line no-eval235 return `236- obj.toString(): ${obj.toString()}237- obj.name: ${obj.name}238- obj.toString + "": ${obj.toString + ''}239- obj.toString.name: ${obj.toString.name}240- obj.valueOf + "": ${obj.valueOf + ''}241- obj.valueOf().name: ${obj.valueOf().name}242- Object.prototype.toString.apply(obj): ${Object.prototype.toString.apply(obj)}243- Function.prototype.toString.call(obj): ${Function.prototype.toString.call(244 obj245 )}246- Function.prototype.valueOf.call(obj) + "": ${247 Function.prototype.valueOf.call(obj) + ''248 }249- obj.toString === Function.prototype.toString: ${250 obj.toString === Function.prototype.toString251 }252`.trim()253}254test('patchToString: passes all toString tests', async t => {255 const toStringVanilla = await (async function () {256 const browser = await vanillaPuppeteer.launch({ headless: true })257 const page = await browser.newPage()258 return page.evaluate(toStringTest, 'HTMLMediaElement.prototype.canPlayType')259 })()260 const toStringStealth = await (async function () {261 const browser = await vanillaPuppeteer.launch({ headless: true })262 const page = await browser.newPage()263 await withUtils(page).evaluate(utils => {264 HTMLMediaElement.prototype.canPlayType = function canPlayType() {}265 utils.patchToString(HTMLMediaElement.prototype.canPlayType)266 })267 return page.evaluate(toStringTest, 'HTMLMediaElement.prototype.canPlayType')268 })()269 // Check that the unmodified results are as expected270 t.is(271 toStringVanilla,272 `273- obj.toString(): function canPlayType() { [native code] }274- obj.name: canPlayType275- obj.toString + "": function toString() { [native code] }276- obj.toString.name: toString277- obj.valueOf + "": function valueOf() { [native code] }278- obj.valueOf().name: canPlayType279- Object.prototype.toString.apply(obj): [object Function]280- Function.prototype.toString.call(obj): function canPlayType() { [native code] }281- Function.prototype.valueOf.call(obj) + "": function canPlayType() { [native code] }282- obj.toString === Function.prototype.toString: true283`.trim()284 )285 // Make sure our customizations leave no trace286 t.is(toStringVanilla, toStringStealth)287})288test('patchToString: passes stack trace tests', async t => {289 const toStringStackTrace = () => {290 try {291 Object.create(292 Object.getOwnPropertyDescriptor(Function.prototype, 'toString').get293 ).toString()294 } catch (err) {295 return err.stack.split('\n').slice(0, 2).join('|')296 }297 return 'error not thrown'298 }299 const toStringVanilla = await (async function () {300 const browser = await vanillaPuppeteer.launch({ headless: true })301 const page = await browser.newPage()302 return page.evaluate(toStringStackTrace)303 })()304 const toStringStealth = await (async function () {305 const browser = await vanillaPuppeteer.launch({ headless: true })306 const page = await browser.newPage()307 await withUtils(page).evaluate(utils => {308 HTMLMediaElement.prototype.canPlayType = function canPlayType() {}309 utils.patchToString(HTMLMediaElement.prototype.canPlayType)310 })311 return page.evaluate(toStringStackTrace)312 })()313 // Check that the unmodified results are as expected314 t.is(315 toStringVanilla,316 `TypeError: Object prototype may only be an Object or null: undefined| at Function.create (<anonymous>)`.trim()317 )318 // Make sure our customizations leave no trace319 t.is(toStringVanilla, toStringStealth)320})321test('patchToString: vanilla has iframe issues', async t => {322 const browser = await vanillaPuppeteer.launch({ headless: true })323 const page = await browser.newPage()324 // Only patch the main window325 const result = await withUtils(page).evaluate(utils => {326 utils.patchToString(HTMLMediaElement.prototype.canPlayType, 'bob')327 const iframe = document.createElement('iframe')328 document.body.appendChild(iframe)329 return {330 direct: Function.prototype.toString.call(331 HTMLMediaElement.prototype.canPlayType332 ),333 directWithiframe: iframe.contentWindow.Function.prototype.toString.call(334 HTMLMediaElement.prototype.canPlayType335 ),336 iframeWithdirect: Function.prototype.toString.call(337 iframe.contentWindow.HTMLMediaElement.prototype.canPlayType338 ),339 iframeWithiframe: iframe.contentWindow.Function.prototype.toString.call(340 iframe.contentWindow.HTMLMediaElement.prototype.canPlayType341 )342 }343 })344 t.deepEqual(result, {345 direct: 'bob',346 directWithiframe: 'function canPlayType() { [native code] }',347 iframeWithdirect: 'function canPlayType() { [native code] }',348 iframeWithiframe: 'function canPlayType() { [native code] }'349 })350})351test('patchToString: stealth has no iframe issues', async t => {352 const browser = await vanillaPuppeteer.launch({ headless: true })353 const page = await browser.newPage()354 // Patch all documents including iframes355 await withUtils(page).evaluateOnNewDocument(utils => {356 utils.patchToString(HTMLMediaElement.prototype.canPlayType, 'alice')357 })358 await page.goto('about:blank')359 const result = await withUtils(page).evaluate(utils => {360 const iframe = document.createElement('iframe')361 document.body.appendChild(iframe)362 return {363 direct: Function.prototype.toString.call(364 HTMLMediaElement.prototype.canPlayType365 ),366 directWithiframe: iframe.contentWindow.Function.prototype.toString.call(367 HTMLMediaElement.prototype.canPlayType368 ),369 iframeWithdirect: Function.prototype.toString.call(370 iframe.contentWindow.HTMLMediaElement.prototype.canPlayType371 ),372 iframeWithiframe: iframe.contentWindow.Function.prototype.toString.call(373 iframe.contentWindow.HTMLMediaElement.prototype.canPlayType374 )375 }376 })377 t.deepEqual(result, {378 direct: 'alice',379 directWithiframe: 'alice',380 iframeWithdirect: 'alice',381 iframeWithiframe: 'alice'382 })383})384test('stripProxyFromErrors: will work correctly', async t => {385 const browser = await vanillaPuppeteer.launch({ headless: true })386 const page = await browser.newPage()387 const results = await withUtils(page).evaluate(utils => {388 const getStack = prop => {389 try {390 prop.caller() // Will throw (HTMLMediaElement.prototype.canPlayType.caller)391 return false392 } catch (err) {393 return err.stack394 }395 }396 /** We need traps to show up in the error stack */397 const dummyProxyHandler = {398 get() {399 return utils.cache.Reflect.get(...(arguments || []))400 },401 apply() {402 return utils.cache.Reflect.apply(...arguments)403 }404 }405 const vanillaProxy = new Proxy(406 HTMLMediaElement.prototype.canPlayType,407 dummyProxyHandler408 )409 const stealthProxy = new Proxy(410 HTMLMediaElement.prototype.canPlayType,411 utils.stripProxyFromErrors(dummyProxyHandler)412 )413 const stacks = {414 vanilla: getStack(HTMLMediaElement.prototype.canPlayType),415 vanillaProxy: getStack(vanillaProxy),416 stealthProxy: getStack(stealthProxy)417 }418 return stacks419 })420 // Check that the untouched stuff behaves as expected421 t.true(results.vanilla.includes(`TypeError: 'caller'`))422 t.false(results.vanilla.includes(`at Object.get`))423 // Regression test: Make sure vanilla JS Proxies leak the stack trace424 t.true(results.vanillaProxy.includes(`TypeError: 'caller'`))425 t.true(results.vanillaProxy.includes(`at Object.get`))426 // Stealth tests427 t.true(results.stealthProxy.includes(`TypeError: 'caller'`))428 t.false(results.stealthProxy.includes(`at Object.get`))429})430test('replaceProperty: will work without traces', async t => {431 const browser = await vanillaPuppeteer.launch({ headless: true })432 const page = await browser.newPage()433 const results = await withUtils(page).evaluate(utils => {434 utils.replaceProperty(Object.getPrototypeOf(navigator), 'languages', {435 get: () => ['de-DE']436 })437 return {438 propNames: Object.getOwnPropertyNames(navigator)439 }440 })441 t.false(results.propNames.includes('languages'))442})443test('cache: will prevent leaks through overriding methods', async t => {444 const browser = await vanillaPuppeteer.launch({ headless: true })445 const page = await browser.newPage()446 const results = await withUtils(page).evaluate(utils => {447 const sniffResults = {448 vanilla: false,449 stealth: false450 }451 const vanillaProxy = new Proxy(452 {},453 {454 get() {455 return Reflect.get(...arguments)456 }457 }458 )459 Reflect.get = () => (sniffResults.vanilla = true)460 // trigger get trap461 vanillaProxy.foo // eslint-disable-line462 const stealthProxy = new Proxy(463 {},464 {465 get() {466 return utils.cache.Reflect.get(...arguments) // using cached copy467 }468 }469 )470 Reflect.get = () => (sniffResults.stealth = true)471 // trigger get trap472 stealthProxy.foo // eslint-disable-line473 return sniffResults474 })475 t.deepEqual(results, {476 vanilla: true,477 stealth: false478 })479})480test('replaceWithProxy: will throw prototype errors', async t => {481 const browser = await vanillaPuppeteer.launch({ headless: true })482 const page = await browser.newPage()483 await page.goto('about:blank')484 const result = await withUtils(page).evaluate(utils => {485 utils.replaceWithProxy(HTMLMediaElement.prototype, 'canPlayType', {})486 const evalErr = (str = '') => {487 try {488 // eslint-disable-next-line no-eval489 return eval(str)490 } catch (err) {491 return err.toString()492 }493 }494 return {495 same: evalErr(496 `Object.setPrototypeOf(HTMLMediaElement.prototype.canPlayType, HTMLMediaElement.prototype.canPlayType) + ""`497 ),498 sameString: evalErr(499 `Object.setPrototypeOf(Function.prototype.toString, Function.prototype.toString) + ""`500 ),501 null: evalErr(502 `Object.setPrototypeOf(Function.prototype.toString, null) + ""`503 ),504 undef: evalErr(505 `Object.setPrototypeOf(Function.prototype.toString, undefined) + ""`506 ),507 none: evalErr(`Object.setPrototypeOf(Function.prototype.toString) + ""`)508 }509 })510 t.deepEqual(result, {511 same: 'TypeError: Cyclic __proto__ value',512 sameString: 'TypeError: Cyclic __proto__ value',513 null: 'TypeError: Cannot convert object to primitive value',514 undef:515 'TypeError: Object prototype may only be an Object or null: undefined',516 none: 'TypeError: Object prototype may only be an Object or null: undefined'517 })...
HeadTest.ts
Source:HeadTest.ts
...7 Assert.eq('head should be head', document.head, Head.getHead(SugarElement.fromDom(document)).dom, Testable.tStrict);8 Assert.eq('head should be head', document.head, Head.head().dom, Testable.tStrict);9});10UnitTest.test('head in iframe', () => {11 withIframe((div, iframe, cw) => {12 Assert.eq('head should be iframe head', cw.document.head, Head.getHead(SugarElement.fromDom(cw.document)).dom, Testable.tStrict);13 });...
indexTest.js
Source:indexTest.js
1import withIframe from "../dist"2test("setup is a function", () => {3 expect(typeof withIframe).toBe("function")...
Using AI Code Generation
1const withIframe = require('wpt-api').withIframe;2}, (err, data) => {3 console.log(err, data);4});5const withIframe = require('wpt-api').withIframe;6}, (err, data) => {7 console.log(err, data);8});9const withIframe = require('wpt-api').withIframe;10}, (err, data) => {11 console.log(err, data);12});13const withIframe = require('wpt-api').withIframe;14}, (err, data) => {15 console.log(err, data);16});17const withIframe = require('wpt-api').withIframe;18}, (err, data) => {19 console.log(err, data);20});21const withIframe = require('wpt-api').with
Using AI Code Generation
1var wpt = require('wpt-api');2var options = {3 videoParams: {4 }5};6wpt.withIframe(options, function(err, data) {7 if (err) {8 console.log(err);9 } else {10 console.log(data);11 }12});13var wpt = require('wpt-api');14var options = {15 videoParams: {16 }17};18wpt.withIframe(options, function(err, data) {19 if (err) {20 console.log(err);21 } else {22 console.log(data);23 }24});25var wpt = require('wpt-api');26var options = {27 videoParams: {28 }29};30wpt.withIframe(options, function(err, data) {31 if (err) {32 console.log(err);33 } else
Using AI Code Generation
1module.exports = function (wpt) {2 .waitForElementVisible('body', 1000)3 .assert.title('Google')4 .assert.visible('input[type=text]')5 .setValue('input[type=text]', 'nightwatch')6 .waitForElementVisible('button[name=btnG]', 1000)7 .click('button[name=btnG]')8 .pause(1000)9 .assert.containsText('#main', 'The Night Watch')10 .end();11 });12};
Using AI Code Generation
1var iframe = document.getElementById('wikipedia-iframe');2var wptools = new WPTOOLS();3wptools.withIframe(iframe, function(){4 console.log(wptools.data);5});6 var iframe = document.getElementById('wikipedia-iframe');7 var wptools = new WPTOOLS();8 wptools.withIframe(iframe, function(){9 console.log(wptools.data);10 });
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!