|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { |
| 3 | + PERFORMANCE_NAVIGATION_EVENT_TYPE, |
| 4 | + PERFORMANCE_RESOURCE_EVENT_TYPE, |
| 5 | + PAGE_VIEW_EVENT_TYPE, |
| 6 | + SESSION_START_EVENT_TYPE |
| 7 | +} from '../plugins/utils/constant'; |
| 8 | + |
| 9 | +// Environment variables set through CLI command |
| 10 | +const ENDPOINT = process.env.ENDPOINT; |
| 11 | +const MONITOR_ID = process.env.MONITOR; |
| 12 | +const TEST_URL = process.env.URL || 'http://localhost:8080/smoke.html'; |
| 13 | + |
| 14 | +const TARGET_URL = ENDPOINT + MONITOR_ID + '/'; |
| 15 | + |
| 16 | +function getEventsByType(requestBody, eventType) { |
| 17 | + return requestBody.RumEvents.filter((e) => e.type === eventType); |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Returns true if the request is a successful PutRumEvents request |
| 22 | + */ |
| 23 | +function isDataPlaneRequest(response): boolean { |
| 24 | + const request = response.request(); |
| 25 | + return ( |
| 26 | + request.method() === 'POST' && |
| 27 | + response.status() === 200 && |
| 28 | + response.url() === TARGET_URL |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +// Run the tests in parallel |
| 33 | +test.describe.configure({ mode: 'parallel' }); |
| 34 | + |
| 35 | +test('when web client calls PutRumEvents then the response code is 200', async ({ |
| 36 | + page |
| 37 | +}) => { |
| 38 | + // Open page |
| 39 | + await page.goto(TEST_URL); |
| 40 | + |
| 41 | + // Test will timeout if no successful dataplane request is found |
| 42 | + await page.waitForResponse(async (response) => |
| 43 | + isDataPlaneRequest(response) |
| 44 | + ); |
| 45 | +}); |
| 46 | + |
| 47 | +test('when web client calls PutRumEvents then the payload contains all events', async ({ |
| 48 | + page |
| 49 | +}) => { |
| 50 | + // Expected number of events per type |
| 51 | + const SESSION_START_COUNT = 1; |
| 52 | + const PAGEVIEW_COUNT = 1; |
| 53 | + const NAVIGATION_COUNT = 1; |
| 54 | + const RESOURCE_EVENT_COUNT = 1; |
| 55 | + |
| 56 | + // Open page |
| 57 | + await page.goto(TEST_URL); |
| 58 | + |
| 59 | + // Test will timeout if no successful dataplane request is found |
| 60 | + const response = await page.waitForResponse(async (response) => |
| 61 | + isDataPlaneRequest(response) |
| 62 | + ); |
| 63 | + |
| 64 | + // Parse payload to verify event count |
| 65 | + const requestBody = JSON.parse(response.request().postData()); |
| 66 | + |
| 67 | + const session = getEventsByType(requestBody, SESSION_START_EVENT_TYPE); |
| 68 | + const navigation = getEventsByType( |
| 69 | + requestBody, |
| 70 | + PERFORMANCE_NAVIGATION_EVENT_TYPE |
| 71 | + ); |
| 72 | + const pageView = getEventsByType(requestBody, PAGE_VIEW_EVENT_TYPE); |
| 73 | + const resource = getEventsByType( |
| 74 | + requestBody, |
| 75 | + PERFORMANCE_RESOURCE_EVENT_TYPE |
| 76 | + ); |
| 77 | + |
| 78 | + // Verify no events are dropped |
| 79 | + expect(session.length).toEqual(SESSION_START_COUNT); |
| 80 | + expect(pageView.length).toEqual(PAGEVIEW_COUNT); |
| 81 | + expect(navigation.length).toEqual(NAVIGATION_COUNT); |
| 82 | + expect(resource.length).toEqual(RESOURCE_EVENT_COUNT); |
| 83 | +}); |
0 commit comments