How to use checkEvent method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

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

events.js

Source: events.js Github

copy
1const request = require('supertest')
2const nock = require('nock')
3const cheerio = require('cheerio')
4const app = require('../../lib/app')
5
6describe('POST /events', () => {
7  jest.setTimeout(60 * 1000)
8
9  let csrfToken = ''
10  let agent
11
12  beforeEach(async () => {
13    process.env.AIRTABLE_API_KEY = '$AIRTABLE_API_KEY$'
14    process.env.AIRTABLE_BASE_KEY = '$AIRTABLE_BASE_KEY$'
15    process.env.HYDRO_SECRET = '$HYDRO_SECRET$'
16    process.env.HYDRO_ENDPOINT = 'http://example.com/hydro'
17    agent = request.agent(app)
18    const csrfRes = await agent.get('/en')
19    const $ = cheerio.load(csrfRes.text || '', { xmlMode: true })
20    csrfToken = $('meta[name="csrf-token"]').attr('content')
21    nock('http://example.com')
22      .post('/hydro')
23      .reply(200, {})
24  })
25
26  afterEach(() => {
27    delete process.env.AIRTABLE_API_KEY
28    delete process.env.AIRTABLE_BASE_KEY
29    delete process.env.HYDRO_SECRET
30    delete process.env.HYDRO_ENDPOINT
31    csrfToken = ''
32  })
33
34  async function checkEvent (data, code) {
35    return agent
36      .post('/events')
37      .send(data)
38      .set('Accept', 'application/json')
39      .set('csrf-token', csrfToken)
40      .expect(code)
41  }
42
43  const baseExample = {
44    context: {
45      // Primitives
46      event_id: 'a35d7f88-3f48-4f36-ad89-5e3c8ebc3df7',
47      user: '703d32a8-ed0f-45f9-8d78-a913d4dc6f19',
48      version: '1.0.0',
49      created: '2020-10-02T17:12:18.620Z',
50
51      // Content information
52      path: '/github/docs/issues',
53      hostname: 'github.com',
54      referrer: 'https://github.com/github/docs',
55      search: '?q=is%3Aissue+is%3Aopen+example+',
56      href: 'https://github.com/github/docs/issues?q=is%3Aissue+is%3Aopen+example+',
57      site_language: 'en',
58
59      // Device information
60      os: 'linux',
61      os_version: '18.04',
62      browser: 'chrome',
63      browser_version: '85.0.4183.121',
64      viewport_width: 1418,
65      viewport_height: 501,
66
67      // Location information
68      timezone: -7,
69      user_language: 'en-US'
70    }
71  }
72
73  describe('page', () => {
74    const pageExample = { ...baseExample, type: 'page' }
75
76    it('should record a page event', () =>
77      checkEvent(pageExample, 200)
78    )
79
80    it('should require a type', () =>
81      checkEvent(baseExample, 400)
82    )
83
84    it('should require an event_id in uuid', () =>
85      checkEvent({
86        ...pageExample,
87        context: {
88          ...pageExample.context,
89          event_id: 'asdfghjkl'
90        }
91      }, 400)
92    )
93
94    it('should require a user in uuid', () =>
95      checkEvent({
96        ...pageExample,
97        context: {
98          ...pageExample.context,
99          user: 'asdfghjkl'
100        }
101      }, 400)
102    )
103
104    it('should require a version', () =>
105      checkEvent({
106        ...pageExample,
107        context: {
108          ...pageExample.context,
109          version: undefined
110        }
111      }, 400)
112    )
113
114    it('should require created timestamp', () =>
115      checkEvent({
116        ...pageExample,
117        context: {
118          ...pageExample.context,
119          timestamp: 1234
120        }
121      }, 400)
122    )
123
124    it('should allow page_event_id', () =>
125      checkEvent({
126        ...pageExample,
127        context: {
128          ...pageExample.context,
129          page_event_id: baseExample.context.event_id
130        }
131      }, 200)
132    )
133
134    it('should not allow a honeypot token', () =>
135      checkEvent({
136        ...pageExample,
137        context: {
138          ...pageExample.context,
139          token: 'zxcv'
140        }
141      }, 400)
142    )
143
144    it('should path be uri-reference', () =>
145      checkEvent({
146        ...pageExample,
147        context: {
148          ...pageExample.context,
149          path: ' '
150        }
151      }, 400)
152    )
153
154    it('should hostname be uri-reference', () =>
155      checkEvent({
156        ...pageExample,
157        context: {
158          ...pageExample.context,
159          hostname: ' '
160        }
161      }, 400)
162    )
163
164    it('should referrer be uri-reference', () =>
165      checkEvent({
166        ...pageExample,
167        context: {
168          ...pageExample.context,
169          referrer: ' '
170        }
171      }, 400)
172    )
173
174    it('should search a string', () =>
175      checkEvent({
176        ...pageExample,
177        context: {
178          ...pageExample.context,
179          search: 1234
180        }
181      }, 400)
182    )
183
184    it('should href be uri', () =>
185      checkEvent({
186        ...pageExample,
187        context: {
188          ...pageExample.context,
189          href: '/example'
190        }
191      }, 400)
192    )
193
194    it('should site_language is a valid option', () =>
195      checkEvent({
196        ...pageExample,
197        context: {
198          ...pageExample.context,
199          site_language: 'nl'
200        }
201      }, 400)
202    )
203
204    it('should os a valid os option', () =>
205      checkEvent({
206        ...pageExample,
207        context: {
208          ...pageExample.context,
209          os: 'ubuntu'
210        }
211      }, 400)
212    )
213
214    it('should os_version a string', () =>
215      checkEvent({
216        ...pageExample,
217        context: {
218          ...pageExample.context,
219          os_version: 25
220        }
221      }, 400)
222    )
223
224    it('should browser a valid option', () =>
225      checkEvent({
226        ...pageExample,
227        context: {
228          ...pageExample.context,
229          browser: 'opera'
230        }
231      }, 400)
232    )
233
234    it('should browser_version a string', () =>
235      checkEvent({
236        ...pageExample,
237        context: {
238          ...pageExample.context,
239          browser_version: 25
240        }
241      }, 400)
242    )
243
244    it('should viewport_width a number', () =>
245      checkEvent({
246        ...pageExample,
247        context: {
248          ...pageExample.context,
249          viewport_width: -500
250        }
251      }, 400)
252    )
253
254    it('should viewport_height a number', () =>
255      checkEvent({
256        ...pageExample,
257        context: {
258          ...pageExample.context,
259          viewport_height: '53px'
260        }
261      }, 400)
262    )
263
264    it('should timezone in number', () =>
265      checkEvent({
266        ...pageExample,
267        context: {
268          ...pageExample.context,
269          timezone: 'GMT-0700'
270        }
271      }, 400)
272    )
273
274    it('should user_language is a string', () =>
275      checkEvent({
276        ...pageExample,
277        context: {
278          ...pageExample.context,
279          user_language: true
280        }
281      }, 400)
282    )
283  })
284
285  describe('exit', () => {
286    const exitExample = {
287      ...baseExample,
288      type: 'exit',
289      exit_render_duration: 0.9,
290      exit_first_paint: 0.1,
291      exit_dom_interactive: 0.2,
292      exit_dom_complete: 0.3,
293      exit_visit_duration: 5,
294      exit_scroll_length: 0.5
295    }
296
297    it('should record an exit event', () =>
298      checkEvent(exitExample, 200)
299    )
300
301    it('should exit_render_duration is a positive number', () =>
302      checkEvent({
303        ...exitExample,
304        exit_render_duration: -0.5
305      }, 400)
306    )
307
308    it('exit_first_paint is a number', () =>
309      checkEvent({ ...exitExample, exit_first_paint: 'afjdkl' }, 400)
310    )
311
312    it('exit_dom_interactive is a number', () =>
313      checkEvent({ ...exitExample, exit_dom_interactive: '202' }, 400)
314    )
315
316    it('exit_visit_duration is a number', () =>
317      checkEvent({ ...exitExample, exit_visit_duration: '75' }, 400)
318    )
319
320    it('exit_scroll_length is a number between 0 and 1', () =>
321      checkEvent({ ...exitExample, exit_scroll_length: 1.1 }, 400)
322    )
323  })
324
325  describe('link', () => {
326    const linkExample = {
327      ...baseExample,
328      type: 'link',
329      link_url: 'https://example.com'
330    }
331
332    it('should send a link event', () =>
333      checkEvent(linkExample, 200)
334    )
335
336    it('link_url is a required uri formatted string', () =>
337      checkEvent({ ...linkExample, link_url: 'foo' }, 400)
338    )
339  })
340
341  describe('search', () => {
342    const searchExample = {
343      ...baseExample,
344      type: 'search',
345      search_query: 'github private instances',
346      search_context: 'private'
347    }
348
349    it('should record a search event', () =>
350      checkEvent(searchExample, 200)
351    )
352
353    it('search_query is required string', () =>
354      checkEvent({ ...searchExample, search_query: undefined }, 400)
355    )
356
357    it('search_context is optional string', () =>
358      checkEvent({ ...searchExample, search_context: undefined }, 200)
359    )
360  })
361
362  describe('navigate', () => {
363    const navigateExample = {
364      ...baseExample,
365      type: 'navigate',
366      navigate_label: 'drop down'
367    }
368
369    it('should record a navigate event', () =>
370      checkEvent(navigateExample, 200)
371    )
372
373    it('navigate_label is optional string', () =>
374      checkEvent({ ...navigateExample, navigate_label: undefined }, 200)
375    )
376  })
377
378  describe('survey', () => {
379    const surveyExample = {
380      ...baseExample,
381      type: 'survey',
382      survey_vote: true,
383      survey_comment: 'I love this site.',
384      survey_email: '[email protected]'
385    }
386
387    it('should record a survey event', () =>
388      checkEvent(surveyExample, 200)
389    )
390
391    it('survey_vote is boolean', () =>
392      checkEvent({ ...surveyExample, survey_vote: undefined }, 400)
393    )
394
395    it('survey_comment is string', () => {
396      checkEvent({ ...surveyExample, survey_comment: 1234 }, 400)
397    })
398
399    it('survey_email is email', () => {
400      checkEvent({ ...surveyExample, survey_email: 'daisy' }, 400)
401    })
402  })
403
404  describe('experiment', () => {
405    const experimentExample = {
406      ...baseExample,
407      type: 'experiment',
408      experiment_name: 'change-button-copy',
409      experiment_variation: 'treatment',
410      experiment_success: true
411    }
412
413    it('should record an experiment event', () =>
414      checkEvent(experimentExample, 200)
415    )
416
417    it('experiment_name is required string', () =>
418      checkEvent({ ...experimentExample, experiment_name: undefined }, 400)
419    )
420
421    it('experiment_variation is required string', () =>
422      checkEvent({ ...experimentExample, experiment_variation: undefined }, 400)
423    )
424
425    it('experiment_success is optional boolean', () =>
426      checkEvent({ ...experimentExample, experiment_success: undefined }, 200)
427    )
428  })
429
430  describe('redirect', () => {
431    const redirectExample = {
432      ...baseExample,
433      type: 'redirect',
434      redirect_from: 'http://example.com/a',
435      redirect_to: 'http://example.com/b'
436    }
437
438    it('should record an redirect event', () =>
439      checkEvent(redirectExample, 200)
440    )
441
442    it('redirect_from is required url', () =>
443      checkEvent({ ...redirectExample, redirect_from: ' ' }, 400)
444    )
445
446    it('redirect_to is required url', () =>
447      checkEvent({ ...redirectExample, redirect_to: undefined }, 400)
448    )
449  })
450
451  describe('clipboard', () => {
452    const clipboardExample = {
453      ...baseExample,
454      type: 'clipboard',
455      clipboard_operation: 'copy'
456    }
457
458    it('should record an clipboard event', () =>
459      checkEvent(clipboardExample, 200)
460    )
461
462    it('clipboard_operation is required copy, paste, cut', () =>
463      checkEvent({ ...clipboardExample, clipboard_operation: 'destroy' }, 400)
464    )
465  })
466
467  describe('print', () => {
468    const printExample = {
469      ...baseExample,
470      type: 'print'
471    }
472
473    it('should record a print event', () =>
474      checkEvent(printExample, 200)
475    )
476  })
477})
478
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)