Best JavaScript code snippet using puppeteer
manager-subprocess-xsnap.js
Source:manager-subprocess-xsnap.js
1// @ts-check2import { assert, details as X, q } from '@agoric/assert';3import { ExitCode } from '@agoric/xsnap/api.js';4import { makeManagerKit } from './manager-helper.js';5import {6 insistVatSyscallObject,7 insistVatDeliveryResult,8} from '../../message.js';9import '../../types.js';10import './types.js';11// eslint-disable-next-line no-unused-vars12function parentLog(first, ...args) {13 // console.error(`--parent: ${first}`, ...args);14}15const encoder = new TextEncoder();16const decoder = new TextDecoder();17/**18 * @param {{19 * allVatPowers: VatPowers,20 * kernelKeeper: KernelKeeper,21 * kernelSlog: KernelSlog,22 * startXSnap: (name: string, handleCommand: AsyncHandler, metered?: boolean, snapshotHash?: string) => Promise<XSnap>,23 * testLog: (...args: unknown[]) => void,24 * }} tools25 * @returns { VatManagerFactory }26 *27 * @typedef { { moduleFormat: 'getExport', source: string } } ExportBundle28 * @typedef { (msg: Uint8Array) => Promise<Uint8Array> } AsyncHandler29 */30export function makeXsSubprocessFactory({31 kernelKeeper,32 kernelSlog,33 startXSnap,34 testLog,35}) {36 /**37 * @param { string } vatID38 * @param { unknown } bundle39 * @param { ManagerOptions } managerOptions40 * @param { (vso: VatSyscallObject) => VatSyscallResult } vatSyscallHandler41 */42 async function createFromBundle(43 vatID,44 bundle,45 managerOptions,46 vatSyscallHandler,47 ) {48 parentLog(vatID, 'createFromBundle', { vatID });49 const {50 consensusMode,51 vatParameters,52 virtualObjectCacheSize,53 enableDisavow,54 enableVatstore,55 gcEveryCrank = true,56 name,57 metered,58 compareSyscalls,59 useTranscript,60 liveSlotsConsole,61 vatConsole,62 } = managerOptions;63 assert(64 !managerOptions.enableSetup,65 'xs-worker: enableSetup not supported at all',66 );67 const mk = makeManagerKit(68 vatID,69 kernelSlog,70 kernelKeeper,71 vatSyscallHandler,72 true,73 compareSyscalls,74 useTranscript,75 );76 /** @type { (item: Tagged) => unknown } */77 function handleUpstream([type, ...args]) {78 parentLog(vatID, `handleUpstream`, type, args.length);79 switch (type) {80 case 'syscall': {81 parentLog(vatID, `syscall`, args[0], args.length);82 const vso = args[0];83 insistVatSyscallObject(vso);84 return mk.syscallFromWorker(vso);85 }86 case 'liveSlotsConsole':87 case 'console': {88 const [level, ...rest] = args;89 // Choose the right console.90 const myConsole =91 (type === 'liveSlotsConsole' && liveSlotsConsole) || vatConsole;92 if (typeof level === 'string' && level in myConsole) {93 myConsole[level](...rest);94 } else {95 console.error(`bad ${type} level`, level);96 }97 return ['ok'];98 }99 case 'testLog':100 testLog(...args);101 return ['OK'];102 default:103 assert.fail(X`unrecognized uplink message ${type}`);104 }105 }106 /** @type { (msg: Uint8Array) => Promise<Uint8Array> } */107 async function handleCommand(msg) {108 // parentLog('handleCommand', { length: msg.byteLength });109 const tagged = handleUpstream(JSON.parse(decoder.decode(msg)));110 return encoder.encode(JSON.stringify(tagged));111 }112 const vatKeeper = kernelKeeper.provideVatKeeper(vatID);113 const lastSnapshot = vatKeeper.getLastSnapshot();114 // start the worker and establish a connection115 const worker = await startXSnap(116 `${vatID}:${name}`,117 handleCommand,118 metered,119 lastSnapshot ? lastSnapshot.snapshotID : undefined,120 );121 /** @type { (item: Tagged) => Promise<CrankResults> } */122 async function issueTagged(item) {123 parentLog(item[0], '...', item.length - 1);124 const result = await worker.issueStringCommand(JSON.stringify(item));125 const reply = JSON.parse(result.reply);126 assert(Array.isArray(reply));127 const [tag, ...rest] = reply;128 return { ...result, reply: [tag, ...rest] };129 }130 if (lastSnapshot) {131 parentLog(vatID, `snapshot loaded. dispatch ready.`);132 } else {133 parentLog(vatID, `instructing worker to load bundle..`);134 const { reply: bundleReply } = await issueTagged([135 'setBundle',136 vatID,137 bundle,138 vatParameters,139 virtualObjectCacheSize,140 enableDisavow,141 enableVatstore,142 consensusMode,143 gcEveryCrank,144 ]);145 if (bundleReply[0] === 'dispatchReady') {146 parentLog(vatID, `bundle loaded. dispatch ready.`);147 } else {148 const [_tag, errName, message] = bundleReply;149 assert.fail(X`setBundle failed: ${q(errName)}: ${q(message)}`);150 }151 }152 /**153 * @param { VatDeliveryObject} delivery154 * @returns { Promise<VatDeliveryResult> }155 */156 async function deliverToWorker(delivery) {157 parentLog(vatID, `sending delivery`, delivery);158 let result;159 try {160 result = await issueTagged(['deliver', delivery, consensusMode]);161 } catch (err) {162 parentLog('issueTagged error:', err.code, err.message);163 let message;164 switch (err.code) {165 case ExitCode.E_TOO_MUCH_COMPUTATION:166 message = 'Compute meter exceeded';167 break;168 case ExitCode.E_STACK_OVERFLOW:169 message = 'Stack meter exceeded';170 break;171 case ExitCode.E_NOT_ENOUGH_MEMORY:172 message = 'Allocate meter exceeded';173 break;174 default:175 // non-metering failure. crash.176 throw err;177 }178 return harden(['error', message, null]);179 }180 parentLog(vatID, `deliverDone`, result.reply[0], result.reply.length);181 // Attach the meterUsage to the deliver result.182 const deliverResult = harden([183 result.reply[0], // 'ok' or 'error'184 result.reply[1] || null, // problem or null185 result.meterUsage || null, // meter usage statistics or null186 ]);187 insistVatDeliveryResult(deliverResult);188 return deliverResult;189 }190 mk.setDeliverToWorker(deliverToWorker);191 function shutdown() {192 return worker.close().then(_ => undefined);193 }194 /**195 * @param {SnapStore} snapStore196 * @returns {Promise<string>}197 */198 function makeSnapshot(snapStore) {199 return snapStore.save(fn => worker.snapshot(fn));200 }201 return mk.getManager(shutdown, makeSnapshot);202 }203 return harden({ createFromBundle });...
add-form.jsx
Source:add-form.jsx
1import React, { Component } from 'react'2import {3 Form,4 Select,5 Input6} from 'antd'7import PropTypes from 'prop-types'8const Item = Form.Item9const Option = Select.Option10/* 11æ·»å åç±»çformç»ä»¶12*/13export default class AddForm extends Component {14 // å建ä¸ä¸ªref15 formRef = React.createRef()16 static propTypes = {17 categorys: PropTypes.array.isRequired,//ä¸çº§åç±»çæ°ç»18 parentId: PropTypes.string.isRequired,//ç¶åç±»çid19 // setForm: PropTypes.func.isRequired,20 deliverForm: PropTypes.func.isRequired,21 // setClasses: PropTypes.func.isRequired,22 // setInput: PropTypes.func.isRequired23 }24 // ä¼ éæ°æ®25 deliverResult = () => {//ä¼åºç°ä¸ç§æ
åµå°±æ¯ï¼å¦æä¸è¿è¡ä¿®æ¹æ°æ®ï¼26 // å°±ä¸ä¼è§¦åï¼é£ä¹å°±ä¸ä¼ä¼ åºform对象ï¼åç»åºäºformçæä½å°±æ æ³å®ç°27 // const resultValue = this.formRef.current.getFieldsValue()28 // console.log('resultValue', resultValue);29 // this.props.setForm(resultValue)30 this.props.deliverForm(this.formRef.current)31 }32 componentDidMount() {33 // ç»åç±»ä¸æèåæ·»å åå§å¼value,classeræ¯selectç»ä»¶è¢«å
è£
æItemåçname34 // selectçoptionï¼valueå¨è¿éæ¯è®¾ç½®çc._idï¼æ以ä¼å
渲æ好æ¯ä¸ä¸ªoption35 // æ¤å¤çé¢è®¾åå§å¼ï¼æ¯æ ¹æ®valueæ¥æ¾å°å¯¹åºçoptionï¼è¯¥optionæ¯æ¸²æ好çï¼ä¼æ¾ç¤ºc.nameï¼ä¸ææ¡ä¼å¯¹åºè¯¥é¡¹ï¼å¹¶æ¾ç¤ºç°è²36 // ä½æ¯å¦ææ设置æ¤å¤çvalue为c.nameï¼ä½æ¯åæ¯éè¿parentIdæ¾ï¼é£å°±æ¾ä¸å°å¯¹åºçoption37 // é£å°±ä¼æ¾ç¤ºä¸ä¸ªæ°çoptionï¼valueåæ¾ç¤ºçé½æ¯parentIdï¼å¹¶ä¸å¨é¢å
渲æ好çä¸ææ¡ä¸æ¾ä¸å°ï¼38 const { parentId } = this.props39 this.formRef.current.setFieldsValue({40 classer: parentId41 })42 console.log(this.formRef.current);43 // ä¸ä¸æ¥å°±å¾æä¸ä¸ªformå¯¹è±¡ä¼ åºå»ï¼ä¸ç¶å¤é¢çæä½æ æ³è¿è¡44 this.props.deliverForm(this.formRef.current)45 //è½å·¥ä½ï¼ä½æ¯ä¸è½å¨didmountéé¢ä½¿ç¨ï¼å 为è¿ä¸ªæ¯ä¸ºäºæ¶é表åæ°æ® 46 // ä½æ¯ï¼didmountéé¢åªæ¯åæå¼è¿ä¸ªå¼¹çªï¼æ²¡æè¿è¡æä½ï¼æ以æ æ³å¾å°æ³è¦çæ°æ®ï¼47 // å¯ä»¥å
é¢è®¾å¼è¿å»ï¼ç¶åå¨didmountéç¨ï¼åºè¯¥å¯ä»¥ï¼æ³¨æå
å顺åº48 // const aa = this.formRef.current.getFieldsValue()49 // console.log('aa', aa);50 // this.props.setForm(this.formRef)51 }52 componentWillUnmount() {53 // console.log('æç»æäº');54 // const resultValue = this.formRef.current.getFieldsValue()55 // console.log('resultValue', resultValue);56 // this.props.setForm(resultValue)57 }58 UNSAFE_componentWillMount() {59 // this.props.setForm(this.formRef)60 }61 render() {62 const { categorys } = this.props63 return (64 <Form ref={this.formRef} onValuesChange={this.deliverResult}>65 <Item name='classer'>66 <Select >67 <Option value='0'>ä¸çº§åç±»</Option>68 {69 //getFieldsValue()å¾å°çæ¯valueçå¼70 categorys.map(c => <Option value={c._id} key={c._id}>{c.name}</Option>)71 }72 </Select>73 </Item>74 <Item name='input'75 rules={[76 {77 required: true,78 message: 'åç±»å称å¿
é¡»è¾å
¥'79 }80 ]}81 >82 <Input83 placeholder='请è¾å
¥åç±»å称'84 />85 </Item>86 </Form>87 )88 }...
index.js
Source:index.js
...50 item = constructAnswer(answer);51 answerList.appendChild(item);52 item.addEventListener("click", (e) => {53 userAnswer = e.target.id;54 deliverResult(userAnswer);55 moveOn();56 });57 item.addEventListener("keyup", (e) => {58 if (e.keyCode === 13) {59 userAnswer = e.target.id;60 deliverResult(userAnswer);61 moveOn();62 }63 });64 }65};66quizButton.addEventListener("click", () => {67 quiz.isActive = true;68 scoreHeading.textContent = "";69 responseHeading.textContent = `Test your knowledge.`;70 document.querySelectorAll(".res").forEach((item) => {71 item.remove();72 });73 moveOn();74 quizButton.classList.add("hidden");...
update-form.jsx
Source:update-form.jsx
1import React, { Component } from 'react'2import {3 Form,4 // Select,5 Input6} from 'antd'7import PropTypes from 'prop-types'8const Item = Form.Item9// const Option = Select.Option10/* 11æ´æ°åç±»çformç»ä»¶12*/13export default class UpdateForm extends Component {14 //为formå建ä¸ä¸ªref15 formRef = React.createRef()16 static propTypes = {17 setForm: PropTypes.func.isRequired,18 categoryName: PropTypes.string.isRequired,19 deliverForm: PropTypes.func.isRequired,20 }21 deliverResult = () => {22 // æformå¯¹è±¡ä¼ åºå»ï¼å®æ¶çï¼æ以æåç¨çæ¯å®æ´ç23 this.props.deliverForm(this.formRef.current)24 }25 // å¥ä¹æ²¡åï¼ä¸å æµè¯ä»£ç 26 UNSAFE_componentWillMount() {27 // å°form对象éè¿setForm()ä¼ éç»ç¶ç»ä»¶28 // this.props.setForm(Item.form)29 // console.log('???' + Form.form);30 // const { categoryName } = this.props31 // this.setState({32 // value: categoryName33 // })34 // form.setFieldsValue({35 // value: categoryName36 // })37 // this.formRef.current.setFieldsValue({38 // input: categoryName39 // })40 console.log('forRef', this.formRef);41 }42 componentDidMount() {43 console.log('forRef,after', this.formRef);44 const { categoryName } = this.props45 // 设置çæ¯name为inputçç»ä»¶çvalue46 this.formRef.current.setFieldsValue({47 input: categoryName48 })49 50 // ä¸ä¸æ¥å°±å¾æä¸ä¸ªformå¯¹è±¡ä¼ åºå»ï¼ä¸ç¶å¤é¢çæä½æ æ³è¿è¡51 this.props.deliverForm(this.formRef.current)52 }53 componentWillUnmount() {54 console.log('ææ¯updateï¼æç»æäº');55 }56 render() {57 console.log(this.props);58 // console.log(Form);59 return (60 <Form ref={this.formRef} onValuesChange={this.deliverResult}>61 <Item name='input'62 rules={[63 {64 required: true,65 message: 'åç±»å称å¿
é¡»è¾å
¥'66 }67 ]}>68 <Input69 ref={input => this.props.setForm(input)}70 >71 </Input>72 </Item>73 </Form>74 )75 }...
direct-pipeline.js
Source:direct-pipeline.js
...45 errorDetected = true;46 return this.emit('error', err);47 }48 if (pendingOutputFields.length === 0) {49 return deliverResult(null, result);50 }51 process.nextTick(() => {52 let outputField = pendingOutputFields.shift(); 53 outputField.call(this, result, loopOutputFields);54 });55 }56 };57 let deliverResult = (err, result) => {58 if (!errorDetected) {59 if (err) {60 errorDetected = true;61 return this.emit('error', err);62 }63 ForwardToDeliver.forward(result);...
mediawiki.page.mwsuggest.js
Source:mediawiki.page.mwsuggest.js
...40 var namespaces = getNamespaces();41 // We're caching queries for performance42 var term = request.term + namespaces;43 if ( term in cache ) {44 deliverResult( cache[term], response );45 return;46 }47 var params = {48 format : 'json',49 action : 'opensearch',50 search : request.term,51 namespace : namespaces52 };53 $.getJSON( url, params, function ( obj ) {54 // Save to cache55 cache[ term ] = obj;56 deliverResult( obj, response );57 });58 },59 select : function() {60 $( '#searchGoButton' ).click();61 },62 create : function() {63 $suggestionList = $container.find( 'ul' );64 },65 appendTo : '.open-search-suggestions',66 open : function() {67 maxRowWindow = Math.floor(68 ( $( window ).height() - $suggestionList.offset().top + $( window ).scrollTop() ) /69 $suggestionList.find( '.ui-menu-item' ).eq( 0 ).height()70 );...
waitForTransactionResult.js
Source:waitForTransactionResult.js
1const BlockchainListener = require('../BlockchainListener');2const TransactionErrorResult = require('./transactionResult/TransactionErrorResult');3const TransactionOkResult = require('./transactionResult/TransactionOkResult');4/**5 * @typedef {waitForTransactionResult}6 * @param {BlockchainListener} blockchainListener7 * @param {string} hashString - Transaction hash string8 * @return {{9 * promise: Promise<TransactionOkResult|TransactionErrorResult>,10 * detach: Function11 * }}12 */13function waitForTransactionResult(blockchainListener, hashString) {14 const topic = BlockchainListener.getTransactionEventName(hashString);15 let handler;16 const promise = new Promise((resolve) => {17 handler = ({ data: { value: { TxResult: txResult } } }) => {18 blockchainListener.off(topic, handler);19 const { result: deliverResult, tx, height } = txResult;20 const txBuffer = Buffer.from(tx, 'base64');21 let TransactionResultClass = TransactionOkResult;22 if (deliverResult && deliverResult.code !== undefined && deliverResult.code !== 0) {23 TransactionResultClass = TransactionErrorResult;24 }25 resolve(26 new TransactionResultClass(27 deliverResult,28 parseInt(height, 10),29 txBuffer,30 ),31 );32 };33 blockchainListener.on(topic, handler);34 });35 const detach = () => {36 blockchainListener.off(topic, handler);37 };38 return {39 promise,40 detach,41 };42}...
AbstractTransactionResult.js
Source:AbstractTransactionResult.js
1class AbstractTransactionResult {2 /**3 * @param {Object} result4 * @param {number} height5 * @param {Buffer} transaction6 */7 constructor(result, height, transaction) {8 this.deliverResult = result;9 this.height = height;10 this.transaction = transaction;11 }12 /**13 * Get TX result14 *15 * @return {Object}16 */17 getResult() {18 return this.deliverResult;19 }20 /**21 * Get transaction block height22 *23 * @return {number}24 */25 getHeight() {26 return this.height;27 }28 /**29 * Get transaction30 *31 * @return {Buffer}32 */33 getTransaction() {34 return this.transaction;35 }36}...
Using AI Code Generation
1const Apify = require('apify');2Apify.main(async () => {3 const requestList = new Apify.RequestList({4 });5 await requestList.initialize();6 const crawler = new Apify.PuppeteerCrawler({7 handlePageFunction: async ({ request, page }) => {8 await Apify.utils.puppeteer.injectJQuery(page);9 const title = await page.evaluate(() => {10 return $('title').text();11 });12 await crawler.deliverResult({13 });14 },15 });16 await crawler.run();17});
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch();4 const page = await browser.newPage();5 await page.screenshot({path: 'google.png'});6 await browser.close();7})();8const puppeteer = require('puppeteer');9(async () => {10 const browser = await puppeteer.launch();11 const page = await browser.newPage();12 await page.screenshot({path: 'google.png'});13 await browser.close();14})();15const puppeteer = require('puppeteer');16(async () => {17 const browser = await puppeteer.launch();18 const page = await browser.newPage();19 await page.screenshot({path: 'google.png'});20 await browser.close();21})();22const puppeteer = require('puppeteer');23(async () => {24 const browser = await puppeteer.launch();25 const page = await browser.newPage();26 await page.screenshot({path: 'google.png'});27 await browser.close();28})();29const puppeteer = require('puppeteer');30(async () => {31 const browser = await puppeteer.launch();32 const page = await browser.newPage();33 await page.screenshot({path: 'google.png'});34 await browser.close();35})();36const puppeteer = require('puppeteer');37(async () => {38 const browser = await puppeteer.launch();39 const page = await browser.newPage();40 await page.screenshot({path: 'google.png'});41 await browser.close();42})();43const puppeteer = require('puppeteer');44(async () => {45 const browser = await puppeteer.launch();
Using AI Code Generation
1const { PuppeteerCrawler } = require('apify');2const { utils: { log } } = Apify;3Apify.main(async () => {4 const requestList = await Apify.openRequestList('my-list', [5 ]);6 const crawler = new PuppeteerCrawler({7 handlePageFunction: async ({ page, request }) => {8 log.info(request.url);9 log.info('Title of the page: ' + await page.title());10 await crawler.deliverResult({11 title: await page.title(),12 });13 },14 handleFailedRequestFunction: async ({ request }) => {15 log.info(`Request ${request.url} failed too many times`);16 },17 });18 await crawler.run();19});20PuppeteerCrawler is a wrapper around [Puppeteer](
Using AI Code Generation
1const puppeteerCrawler = new Apify.PuppeteerCrawler({2 handlePageFunction: async ({ request, page }) => {3 await Apify.utils.puppeteer.injectJQuery(page);4 const title = await page.evaluate(() => {5 return $('title').text();6 });7 await Apify.pushData({8 });9 return;10 },11 handleFailedRequestFunction: async ({ request }) => {12 await Apify.pushData({13 });14 },15});16await puppeteerCrawler.run();17### Apify.PuppeteerCrawler(options)18The `PuppeteerCrawler` opens a new [Puppeteer](
Using AI Code Generation
1async function deliverResult(page, data) {2 await page.evaluate((data) => {3 window.deliverResult(data);4 }, data);5}6async function takeScreenshot(page, data) {7 await page.evaluate((data) => {8 window.takeScreenshot(data);9 }, data);10}11async function deliverError(page, data) {12 await page.evaluate((data) => {13 window.deliverError(data);14 }, data);15}16async function deliverLog(page, data) {17 await page.evaluate((data) => {18 window.deliverLog(data);19 }, data);20}21async function deliverConsole(page, data) {22 await page.evaluate((data) => {23 window.deliverConsole(data);24 }, data);25}26async function deliverAlert(page, data) {27 await page.evaluate((data) => {28 window.deliverAlert(data);29 }, data);30}31async function deliverConfirm(page, data) {32 await page.evaluate((data) => {33 window.deliverConfirm(data);34 }, data);35}36async function deliverPrompt(page, data) {37 await page.evaluate((data) => {38 window.deliverPrompt(data);39 }, data);40}41async function deliverDownload(page, data) {42 await page.evaluate((data) => {43 window.deliverDownload(data);44 }, data);45}46async function deliverUpload(page, data) {47 await page.evaluate((data) => {48 window.deliverUpload(data);49 }, data);50}51async function deliverNavigation(page, data) {52 await page.evaluate((data) => {53 window.deliverNavigation(data);54 }, data);55}56async function deliverRequest(page, data) {57 await page.evaluate((data) => {58 window.deliverRequest(data);59 }, data);60}61async function deliverResponse(page, data)
Using AI Code Generation
1const puppeteerCrawler = new Apify.PuppeteerCrawler({2 handlePageFunction: async ({ page, request }) => {3 await Apify.utils.puppeteer.injectJQuery(page);4 const result = await page.evaluate(() => {5 return $('body').text();6 });7 await Apify.pushData(result);8 },9});10await puppeteerCrawler.run();11const cheerioCrawler = new Apify.CheerioCrawler({12 handlePageFunction: async ({ $, request }) => {13 const result = $('body').text();14 await Apify.pushData(result);15 },16});17await cheerioCrawler.run();18const basicCrawler = new Apify.BasicCrawler({19 handleRequestFunction: async ({ request }) => {20 const result = 'result';21 await Apify.pushData(result);22 },23});24await basicCrawler.run();25const requestList = new Apify.RequestList({26});27await requestList.initialize();28const requestQueue = new Apify.RequestQueue();29const dataset = await Apify.openDataset();30await dataset.pushData({ test: 'test' });
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!!