diff --git a/samples/cdp-tests/connectOverCDPScript.js b/samples/cdp-tests/connectOverCDPScript.js index 7e7fbf7..9f42df1 100644 --- a/samples/cdp-tests/connectOverCDPScript.js +++ b/samples/cdp-tests/connectOverCDPScript.js @@ -17,13 +17,49 @@ import { chromium } from 'playwright'; import { getCdpEndpoint } from './playwrightServiceClient.js'; +import { randomUUID } from 'crypto'; +import https from 'https'; async function main() { + // Generate a unique run ID + const runId = process.env.PLAYWRIGHT_RUN_ID || randomUUID(); + + // Create test run for tracking + const match = process.env.PLAYWRIGHT_SERVICE_URL?.match(/^wss:\/\/([^\.]+)\.api\.playwright\.microsoft\.com\/playwrightworkspaces\/([^\/]+)\//); + if (match) { + const url = new URL(`https://${match[1]}.reporting.api.playwright.microsoft.com/playwrightworkspaces/${match[2]}/test-runs/${runId}?api-version=2025-09-01`); + const payload = JSON.stringify({ displayName: runId, ciConfig: { providerName: 'GITHUB' } }); + const req = https.request(url, { + method: 'PATCH', + headers: { + 'Content-Type': 'application/merge-patch+json', + 'Authorization': `Bearer ${process.env.PLAYWRIGHT_SERVICE_ACCESS_TOKEN}` + } + }, res => { + let data = ''; + res.on('data', chunk => (data += chunk)); + res.on('end', () => { + if (res.statusCode >= 200 && res.statusCode < 300) { + console.log(`✅ Test run created: ${runId}`); + } else { + console.log(`⚠️ Test run creation failed: ${res.statusCode} - ${data}`); + } + }); + }); + req.on('error', err => console.log(`⚠️ Test run creation error: ${err.message}`)); + req.write(payload); + req.end(); + } + console.log('🔗 Connecting to Microsoft Playwright Service...'); + console.log(`📊 Run ID: ${runId}`); - // Step 1: Get CDP endpoint from the service + // Step 1: Get CDP endpoint from the service with run ID // This step will be simplified once OSS redirect support is added - const cdpUrl = await getCdpEndpoint(); + let cdpUrl = await getCdpEndpoint(); + // Append run ID to track this session + const separator = cdpUrl.includes('?') ? '&' : '?'; + cdpUrl = `${cdpUrl}${separator}runId=${runId}`; console.log('✅ Got CDP endpoint'); // Step 2: Connect to remote browser using Playwright diff --git a/samples/cdp-tests/connectOverCDPScript.py b/samples/cdp-tests/connectOverCDPScript.py index d432342..f73f588 100644 --- a/samples/cdp-tests/connectOverCDPScript.py +++ b/samples/cdp-tests/connectOverCDPScript.py @@ -33,23 +33,46 @@ """ import asyncio +import os +import uuid +import aiohttp from playwright.async_api import async_playwright from dotenv import load_dotenv # Load environment variables from .env file load_dotenv() -from playwright_service_client import get_cdp_endpoint +from playwright_service_client import get_cdp_endpoint, _parse_url async def main(): """Connect to a remote browser via CDP and perform basic operations.""" + # Generate a unique run ID + run_id = os.getenv('PLAYWRIGHT_RUN_ID', str(uuid.uuid4())) + + # Create test run for tracking + region, workspace_id = _parse_url(os.getenv('PLAYWRIGHT_SERVICE_URL', '')) + url = f'https://{region}.reporting.api.playwright.microsoft.com/playwrightworkspaces/{workspace_id}/test-runs/{run_id}?api-version=2025-09-01' + payload = {'displayName': run_id, 'ciConfig': {'providerName': 'GITHUB'}} + headers = {'Content-Type': 'application/merge-patch+json', 'Authorization': f'Bearer {os.getenv("PLAYWRIGHT_SERVICE_ACCESS_TOKEN", "")}'} + async with aiohttp.ClientSession() as session: + response = await session.patch(url, json=payload, headers=headers) + if response.status >= 200 and response.status < 300: + print(f'✅ Test run created: {run_id}') + else: + text = await response.text() + print(f'⚠️ Test run creation failed: {response.status} - {text}') + print('🔗 Connecting to Microsoft Playwright Service...') + print(f'📊 Run ID: {run_id}') - # Step 1: Get CDP endpoint from the service + # Step 1: Get CDP endpoint from the service with run ID # This step will be simplified once OSS redirect support is added cdp_url = await get_cdp_endpoint() + # Append run ID to track this session + separator = '&' if '?' in cdp_url else '?' + cdp_url = f"{cdp_url}{separator}runId={run_id}" print(f'✅ Got CDP endpoint') # Step 2: Connect to remote browser using Playwright