How to use _collectEvents method in qawolf

Best JavaScript code snippet using qawolf

event_storage.js

Source:event_storage.js Github

copy

Full Screen

...56 // The first event we get will be sent immediatelly,57 // other will be sent when MAX_EVENTS is reached or when we reach the end of an interval.58 this._atFirstEvent = true;59 const collectEvents = () => {60 this._collectEvents(this._session.id())61 };62 // If persisted events have reached limit, submit them63 if (this._events.length >= MAX_EVENTS) {64 collectEvents()65 }66 // The code inside this conditional will work for browser environments,67 // these can be:68 // 1. Electron apps69 // 2. Web extensions70 // 3. Web apps71 // The are not:72 // 1. CLI tools73 // 2. Servers74 // 3. QML apps75 if (typeof window !== "undefined") {76 // Set up an interval to send evenst periodically77 // TODO: Make sure using setInterval is not a terrible idea78 this._interval = setInterval(collectEvents, EVENTS_PING_INTERVAL);79 // If the page unloads we want to collect any events80 // in case the user never comes back to this page.81 //82 // TODO: even though this *usually* (focus on the usually, sometimes is doesn't)83 // succeeds in uploading the ping after the page in unloaded,84 // it never succeeds on deleting the ping that was uploaded (if it was successfully uploaded).85 // It also doesn't deal with upload errors.86 window.addEventListener("beforeunload", collectEvents);87 }88 }89 /**90 * Records a new event in storage.91 *92 * @param {Number} timestamp The timestamp of when the event was recorded. This allows to order events from a single process run.93 * @param {String} category The event's category. This is defined by users in the metrics file.94 * @param {String} name The event's name. This is defined by users in the metrics file.95 * @param {Object} extra A map of all extra data values. The set of allowed extra keys is defined by users in the metrics file.96 */97 record(timestamp, category, name, extra) {98 this._pushEvent(new RecordedEvent(timestamp, category, name, extra));99 if (this._atFirstEvent) {100 this._collectEvents(this._session.id());101 this._atFirstEvent = false;102 }103 }104 /**105 * Collects currently stored events for uploading and clears storage.106 */107 _collectEvents(sessionId) {108 if (this._events && this._events.length > 0) {109 // Do the actual collection110 this._pingMaker.collect(this._snapshot(), sessionId);111 // Clear stores112 this._events = []113 setItem(EVENT_STORAGE_KEY, JSON.stringify(this._events));114 } else {115 console.info("Attempted to collect a new ping but there are no events to collect at this moment. Bailing out.")116 }117 }118 /**119 * Gets a snapshot of the current events.120 *121 * @returns {Object} An representing all events stored, with timestamps relative to the first event.122 */123 _snapshot() {124 let snapshot = [];125 const firstTimestamp = this._events && this._events[0].timestamp;126 for (const event of this._events) {127 snapshot.push(event.serializeRelative(firstTimestamp));128 }129 return snapshot;130 }131 /**132 * Adds a new event to `this._events` and triggers ping collection133 * in case MAX_EVENTS has been reached.134 *135 * @param {RecordedEvent} event The event to persist136 */137 _pushEvent(event) {138 this._events.push(event);139 setItem(EVENT_STORAGE_KEY, JSON.stringify(this._events));140 if (this._events.length >= MAX_EVENTS) {141 this._collectEvents(this._session.id());142 }143 }144 /**145 * Get the persisted events from storage.146 *147 * @returns {String[]} The parsed array of events found in localStorage or an empty array.148 */149 _getPersistedEvents() {150 try {151 const persisted = getItemWithDefault(EVENT_STORAGE_KEY, JSON.stringify([]));152 const parsed = JSON.parse(persisted);153 return parsed.map(e => new RecordedEvent(e));154 } catch(e) {155 console.error(`Unable to parse Glean events from storage: ${e}`);...

Full Screen

Full Screen

event_storage.test.js

Source:event_storage.test.js Github

copy

Full Screen

1/* This Source Code Form is subject to the terms of the Mozilla Public2 * License, v. 2.0. If a copy of the MPL was not distributed with this3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */4const EventStorage = require('../src/event_storage');5const constants = require('../src/constants');6const { getItem } = require('storage');7// JSDOM does not support crypto yet. Let's fake it.8const crypto = require('crypto');9Object.defineProperty(global.self, 'crypto', {10 value: {11 getRandomValues: arr => crypto.randomBytes(arr.length)12 }13});14global.fetch = jest.fn(() =>15 Promise.resolve({16 json: () => Promise.resolve({ }),17 status: 200,18 })19);20afterEach(() => {21 jest.clearAllMocks();22});23test('localStorage and _events are kept in sync', () => {24 const getLocalStorageEvents = () => {25 const contents = getItem(constants.EVENT_STORAGE_KEY);26 if (contents) {27 try {28 return JSON.parse(contents);29 } catch {30 throw contents;31 }32 } else {33 return contents;34 }35 }36 const storage = new EventStorage();37 const submitSpy = jest.spyOn(storage, "_collectEvents");38 // Check that initial state is cleared for both storages39 expect(storage._events.length).toBe(0);40 expect(getLocalStorageEvents().length).toBe(0);41 // Record the first event and be aware that this event will be submitted immediatelly.42 storage.record(Date.now(), "category", "name", { "extra": "key" });43 // Check that event was indeed submitted, just to be sure44 expect(submitSpy).toHaveBeenCalledTimes(1);45 // Check that stores are both clear after submitting the first event46 expect(storage._events.length).toBe(0);47 expect(getLocalStorageEvents().length).toBe(0);48 // Record some events, so taht something is stored49 storage.record(Date.now(), "category", "name", { "extra": "key" });50 storage.record(Date.now(), "category", "name", { "extra": "key" });51 storage.record(Date.now(), "category", "name", { "extra": "key" });52 // Check that stores are both hold the same amount of events53 expect(storage._events.length).toBe(3);54 expect(getLocalStorageEvents().length).toBe(3);55});56test('submits the first event immediatelly, and not the next ones', () => {57 const storage = new EventStorage();58 const submitSpy = jest.spyOn(storage, "_collectEvents");59 // Record three events, but only one of this should be immediatelly submitted60 storage.record(Date.now(), "category", "name", { "extra": "key" });61 storage.record(Date.now(), "category", "name", { "extra": "key" });62 storage.record(Date.now(), "category", "name", { "extra": "key" });63 // Check the ping was submitted64 expect(submitSpy).toHaveBeenCalledTimes(1);65 // Check that event submitted event is not in storage anymore66 expect(storage._events.length).toBe(2);67});68test('submits events when interval is reached, if there are events', () => {69 // Stub the interval constant, so that we don't have to wait too long on this test.70 const previousInterval = constants.EVENTS_PING_INTERVAL;71 constants.EVENTS_PING_INTERVAL = 3 * 1000; // 3s72 const storage = new EventStorage();73 // Record the first event before setting up the spy,74 // because it will trigger a _collectEvents that we don't care about75 storage.record(Date.now(), "category", "name", { "extra": "key" });76 const submitSpy = jest.spyOn(storage, "_collectEvents");77 // Record some events, so that we have something to submit when it is time78 storage.record(Date.now(), "category", "name", { "extra": "key" });79 storage.record(Date.now(), "category", "name", { "extra": "key" });80 storage.record(Date.now(), "category", "name", { "extra": "key" });81 // Check that nothing was submitted yet, we haven't reached the end of the interval82 expect(submitSpy).toHaveBeenCalledTimes(0);83 // All events should still be in storage84 expect(storage._events.length).toBe(3);85 setTimeout(() => {86 // Check that ping was submitted87 // Check that events storage was cleared88 expect(storage._events.length).toBe(0);89 }, constants.EVENTS_PING_INTERVAL)90 // Reset interval so as not to interfere with other tests91 constants.EVENTS_PING_INTERVAL = previousInterval;92});93test('submits events when limit is reached', () => {94 const storage = new EventStorage();95 // Record the first event before setting up the spy,96 // because it will trigger a _collectEvents that we don't care about97 storage.record(Date.now(), "category", "name", { "extra": "key" });98 const submitSpy = jest.spyOn(storage, "_collectEvents");99 // Submit the max number of events to trigger submission100 for (let i = 0; i < constants.MAX_EVENTS; i++) {101 storage.record(Date.now(), "category", "name", { "extra": "key" });102 }103 // Check that ping was submitted104 expect(submitSpy).toHaveBeenCalledTimes(1);105 // Check that events storage was cleared106 expect(storage._events.length).toBe(0);107});108test('gets persisted events on init', () => {109 const storage1 = new EventStorage();110 // Record the first event and be aware that this event will be submitted immediatelly.111 storage1.record(Date.now(), "category", "name", { "extra": "key" });112 // Record some events, so that we have something to persist when it is time113 storage1.record(Date.now(), "category", "name", { "extra": "key" });114 storage1.record(Date.now(), "category", "name", { "extra": "key" });115 storage1.record(Date.now(), "category", "name", { "extra": "key" });116 const storage2 = new EventStorage();117 const submitSpy = jest.spyOn(storage2, "_collectEvents");118 // Check that nothing was submitted, we are not at max119 expect(submitSpy).toHaveBeenCalledTimes(0);120 // Check that persisted events were loaded to _events121 expect(storage2._events.length).toBe(3);122});123test('correctly snapshots the storage', () => {124 const storage = new EventStorage();125 // Record the first event and be aware that this event will be submitted immediatelly.126 storage.record(Date.now(), "category", "name", { "extra": "key" });127 // Record some events, so that we have something to persist when it is time128 storage.record(Date.now(), "category", "name", { "extra": "key" });129 expect(storage._snapshot()[0].category).toBe("category");130 expect(storage._snapshot()[0].name).toBe("name");131 expect(storage._snapshot()[0].extra.extra).toBe("key");...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

...5class TogCollector extends TogCollector_p{6 constructor(option = {}) {7 super(option);8 if (window) {9 this._collectEvents();10 this._rwWindowEvents();11 this._collectDeviceInfo();12 this._collectErrors();13 this._collectCl_Tu();14 }15 }16 /**17 * 重写windows事件18 * */19 _rwWindowEvents() {20 window.localStorage.setItem = rwlc('setItem', 'l_setItem');21 window.sessionStorage.setItem = rwsc('setItem', 's_setItem');22 window.console.error = rwConsole('error', '_consoleError');23 this._collectUserInfo();24 }25 /**26 * 注册事件27 * */28 _collectEvents() {29 this.eventEmitter = new EventEmitter();30 this.eventEmitter.addListener('pageInfo', (data) => this._setPageInfo(data));31 const {eventEmitter, config: {isSingePage}, defaultData:{pageInfo}} = this;32 collectPage.init({eventEmitter, isSingePage, pageInfo});33 }34 /**35 * 设置页面信息36 * */37 _setPageInfo(data) {38 const {isSend, currentUrl, targetUrl} = data;39 delete data.isSend;40 this.data.pageInfo = {41 ...this.defaultData.pageInfo,42 ...data,...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { collectEvents } = require("qawolf");2const { chromium } = require("playwright");3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const events = await collectEvents(page);8 console.log(events);9 await browser.close();10})();11const { chromium } = require('playwright');12(async () => {13 const browser = await chromium.launch();14 const context = await browser.newContext();15 const page = await context.newPage();16 const page2 = await context.newPage();17 await browser.close();18})();19Error: Protocol error (Page.navigate): Cannot navigate to invalid URL20const { chromium } = require('playwright');21(async () => {22 const browser = await chromium.launch();23 const context = await browser.newContext();24 const page = await context.newPage();25 const page2 = await context.newPage();26 await browser.close();27})();28Error: Protocol error (Page.navigate): Cannot navigate to invalid URL

Full Screen

Using AI Code Generation

copy

Full Screen

1const { _collectEvents } = require("qawolf");2const { chromium } = require("playwright");3(async () => {4 const browser = await chromium.launch();5 const page = await browser.newPage();6 await page.click("input[name=q]");7 await page.fill("input[name=q]", "Hello World!");8 await page.press("input[name=q]", "Enter");9 const events = await _collectEvents(page);10 console.log(events);11 await browser.close();12})();13const { _collectEvents } = require("qawolf");14const { chromium } = require("playwright");15(async () => {16 const browser = await chromium.launch();17 const page = await browser.newPage();18 await page.click("input[name=q]");19 await page.fill("input[name=q]", "Hello World!");20 await page.press("input[name=q]", "Enter");21 const events = await _collectEvents(page);22 console.log(events);23 await browser.close();24})();25const { _collectEvents } = require("qawolf");26const { chromium } = require("playwright");27(async () => {28 const browser = await chromium.launch();29 const page = await browser.newPage();30 await page.click("input[name=q]");31 await page.fill("input[name=q]", "Hello World!");32 await page.press("input[name=q]", "Enter");33 const events = await _collectEvents(page);34 console.log(events);35 await browser.close();36})();37const { _collectEvents } = require("qawolf");38const { chromium } = require("playwright");39(async () => {40 const browser = await chromium.launch();41 const page = await browser.newPage();42 await page.click("input[name=q]");43 await page.fill("input[name=q]", "Hello World!");44 await page.press("input[name=q]", "Enter");45 const events = await _collectEvents(page);46 console.log(events);47 await browser.close();

Full Screen

Using AI Code Generation

copy

Full Screen

1const qawolf = require('qawolf');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const page = await browser.newPage();6 await qawolf.collectEvents(page, 'test');7 await browser.close();8})();9const qawolf = require('qawolf');10const { chromium } = require('playwright');11(async () => {12 const browser = await chromium.launch();13 const page = await browser.newPage();14 await qawolf.collectEvents(page, 'test');15 await browser.close();16})();17const qawolf = require('qawolf');18const { chromium } = require('playwright');19(async () => {20 const browser = await chromium.launch();21 const page = await browser.newPage();22 await qawolf.collectEvents(page, 'test');23 await browser.close();24})();25const qawolf = require('qawolf');26const { chromium } = require('playwright');27(async () => {28 const browser = await chromium.launch();29 const page = await browser.newPage();30 await qawolf.collectEvents(page, 'test');31 await browser.close();32})();33const qawolf = require('qawolf');34const { chromium } = require('playwright');35(async () => {36 const browser = await chromium.launch();37 const page = await browser.newPage();38 await qawolf.collectEvents(page, 'test');39 await browser.close();40})();41const qawolf = require('qawolf');42const { chromium } = require('playwright');43(async () => {

Full Screen

Using AI Code Generation

copy

Full Screen

1const qawolf = require("qawolf");2const { _collectEvents } = require("qawolf/src/collectEvents");3async function test() {4 const browser = await qawolf.launch();5 const page = await browser.newPage();6 await page.type('[name="q"]', "Hello World");7 await page.click('[name="btnK"]');8 const events = await _collectEvents(page, {9 });10 console.log(events);11 await browser.close();12}13test();14 {15 options: { text: "Hello World" },16 },17 { action: "click", selector: '[name="btnK"]' },18 { action: "click", selector: '[name="btnI"]' },19 {20 options: { text: "Hello World" },21 },22 { action: "click", selector: '[name="btnK"]' },23 { action: "click", selector: '[name="btnI"]' },24];

Full Screen

Using AI Code Generation

copy

Full Screen

1const qawolf = require("qawolf");2const { _collectEvents } = qawolf;3const events = _collectEvents();4console.log(events);5const qawolf = require("qawolf");6const { _collectEvents } = qawolf;7const events = _collectEvents();8console.log(events);9const qawolf = require("qawolf");10const { _collectEvents } = qawolf;11const events = _collectEvents();12console.log(events);13const qawolf = require("qawolf");14const { _collectEvents } = qawolf;15const events = _collectEvents();16console.log(events);17const qawolf = require("qawolf");18const { _collectEvents } = qawolf;19const events = _collectEvents();20console.log(events);21const qawolf = require("qawolf");22const { _collectEvents } = qawolf;23const events = _collectEvents();24console.log(events);25const qawolf = require("qawolf");26const { _collectEvents } = qawolf;27const events = _collectEvents();28console.log(events);29const qawolf = require("qawolf");30const { _collectEvents } = qawolf;31const events = _collectEvents();32console.log(events);33const qawolf = require("qawolf");34const { _collectEvents } = qawolf;35const events = _collectEvents();36console.log(events);

Full Screen

Using AI Code Generation

copy

Full Screen

1const { _collectEvents } = require("qawolf");2const { join } = require("path");3const { writeFileSync } = require("fs");4const launchBrowser = require("qawolf/src/launchBrowser");5const browser = await launchBrowser({ width: 1366, height: 768 });6const page = await browser.newPage();7const events = await _collectEvents(page);8const filename = join(__dirname, "events.json");9writeFileSync(filename, JSON.stringify(events, null, 2));10await browser.close();11 {12 "position": {13 }14 },15 {16 },17 {18 "position": {19 }20 },21 {22 "position": {23 }24 }25const { _generateCode } = require("qawolf");26const { join } = require("path");27const { readFileSync } = require("fs");28const filename = join(__dirname, "events.json");29const events = JSON.parse(readFileSync(filename, "utf8"));30const code = _generateCode(events);31console.log(code);32const { _generateCode } = require("qawolf");33const { join } = require("path");34const { readFileSync } = require("fs");

Full Screen

Using AI Code Generation

copy

Full Screen

1const { _collectEvents } = require("qawolf");2const fs = require("fs");3(async () => {4 });5 fs.writeFileSync("events.json", JSON.stringify(events, null, 2));6})();7 {8 "selector": "#tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input.gNO89b",9 "target": {10 "id": "tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input.gNO89b",11 "attributes": {12 "id": "tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input.gNO89b",13 }14 }15 },16 {17 "selector": "#tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input.gNO89b",18 "target": {19 "id": "tsf > div:nth-child(2) > div > div.FPdoLc.VlcLA

Full Screen

Using AI Code Generation

copy

Full Screen

1import { _collectEvents } from 'qawolf';2async function collectEvents() {3 const events = await _collectEvents();4 console.log(events);5}6collectEvents();

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run qawolf automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful