|
| 1 | +import config from "../../config.js"; |
| 2 | +import { assertOkResponse, maybeCompressBase64 } from "../../lib/utils.js"; |
| 3 | +import { SessionType } from "../../lib/constants.js"; |
| 4 | + |
| 5 | +//Extracts screenshot URLs from BrowserStack session logs |
| 6 | +async function extractScreenshotUrls(sessionId: string, sessionType: SessionType): Promise<string[]> { |
| 7 | + |
| 8 | + const credentials = `${config.browserstackUsername}:${config.browserstackAccessKey}`; |
| 9 | + const auth = Buffer.from(credentials).toString("base64"); |
| 10 | + |
| 11 | + const baseUrl = `https://api.browserstack.com/${sessionType === SessionType.Automate ? 'automate' : 'app-automate'}`; |
| 12 | + |
| 13 | + const url = `${baseUrl}/sessions/${sessionId}/logs`; |
| 14 | + const response = await fetch(url, { |
| 15 | + headers: { |
| 16 | + "Content-Type": "application/json", |
| 17 | + Authorization: `Basic ${auth}`, |
| 18 | + }, |
| 19 | + }); |
| 20 | + |
| 21 | + await assertOkResponse(response, "Session"); |
| 22 | + |
| 23 | + const text = await response.text(); |
| 24 | + |
| 25 | + const urls: string[] = []; |
| 26 | + const SCREENSHOT_PATTERN = /REQUEST.*GET.*\/screenshot/; |
| 27 | + const RESPONSE_VALUE_PATTERN = /"value"\s*:\s*"([^"]+)"/; |
| 28 | + |
| 29 | + // Split logs into lines and process them |
| 30 | + const lines = text.split("\n"); |
| 31 | + |
| 32 | + for (let i = 0; i < lines.length - 1; i++) { |
| 33 | + const currentLine = lines[i]; |
| 34 | + const nextLine = lines[i + 1]; |
| 35 | + |
| 36 | + if (SCREENSHOT_PATTERN.test(currentLine)) { |
| 37 | + const match = nextLine.match(RESPONSE_VALUE_PATTERN); |
| 38 | + if (match && match[1]) { |
| 39 | + urls.push(match[1]); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return urls; |
| 45 | +} |
| 46 | + |
| 47 | +//Converts screenshot URLs to base64 encoded images |
| 48 | +async function convertUrlsToBase64( |
| 49 | + urls: string[], |
| 50 | +): Promise<Array<{ url: string; base64: string }>> { |
| 51 | + const screenshots = await Promise.all( |
| 52 | + urls.map(async (url) => { |
| 53 | + const response = await fetch(url); |
| 54 | + const arrayBuffer = await response.arrayBuffer(); |
| 55 | + const base64 = Buffer.from(arrayBuffer).toString("base64"); |
| 56 | + |
| 57 | + // Compress the base64 image if needed |
| 58 | + const compressedBase64 = await maybeCompressBase64(base64); |
| 59 | + |
| 60 | + return { |
| 61 | + url, |
| 62 | + base64: compressedBase64, |
| 63 | + }; |
| 64 | + }), |
| 65 | + ); |
| 66 | + |
| 67 | + return screenshots; |
| 68 | +} |
| 69 | + |
| 70 | +//Fetches and converts screenshot URLs to base64 encoded images |
| 71 | +export async function fetchAutomationScreenshots(sessionId: string, sessionType: SessionType = SessionType.Automate) { |
| 72 | + const urls = await extractScreenshotUrls(sessionId, sessionType); |
| 73 | + if (urls.length === 0) { |
| 74 | + return []; |
| 75 | + } |
| 76 | + |
| 77 | + // Take only the last 5 URLs |
| 78 | + const lastFiveUrls = urls.slice(-5); |
| 79 | + return await convertUrlsToBase64(lastFiveUrls); |
| 80 | +} |
0 commit comments