-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture-webapi.js
More file actions
109 lines (89 loc) · 3.04 KB
/
capture-webapi.js
File metadata and controls
109 lines (89 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Script to capture Web API screenshots (Swagger UI)
const { chromium } = require('playwright');
const path = require('path');
const fs = require('fs');
// Create webapi images directory
const imagesDir = path.join(__dirname, '..', 'docs', 'images', 'webapi');
if (!fs.existsSync(imagesDir)) {
fs.mkdirSync(imagesDir, { recursive: true });
}
async function captureWebAPIScreenshots() {
const browser = await chromium.launch({
headless: false,
slowMo: 1000,
});
const context = await browser.newContext({
viewport: { width: 1920, height: 1080 },
ignoreHTTPSErrors: true,
});
const page = await context.newPage();
async function toggleResource(tagName) {
const headerSelectors = [
`.opblock-tag-section:has(.opblock-tag:has-text("${tagName}")) .opblock-tag`,
`.opblock-tag:has-text("${tagName}")`,
`h3.opblock-tag:has-text("${tagName}")`,
`[data-tag="${tagName}"] .opblock-tag`,
];
for (const selector of headerSelectors) {
const target = page.locator(selector).first();
if (await target.count() > 0) {
await target.click();
return true;
}
}
return false;
}
async function captureExpandedResource(tagName, outputFile) {
const clicked = await toggleResource(tagName);
if (!clicked) {
throw new Error(`Could not find Swagger resource tag: ${tagName}`);
}
await page.waitForTimeout(1500);
await page.screenshot({
path: path.join(imagesDir, outputFile),
fullPage: true,
});
}
function toKebabCase(value) {
return value
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
}
try {
console.log('Capturing Web API screenshots...\n');
// 1. Swagger UI landing
console.log('1) Opening Swagger UI...');
await page.goto('https://localhost:44378/swagger/index.html', { waitUntil: 'networkidle', timeout: 30000 });
await page.waitForTimeout(2000);
const resourceTags = await page.locator('.opblock-tag').allTextContents();
const tags = resourceTags
.map((tag) => tag.trim())
.filter((tag) => tag.length > 0);
if (tags.length === 0) {
throw new Error('No Swagger resources were found.');
}
let previousTag = null;
let step = 2;
for (const tagName of tags) {
if (previousTag) {
await toggleResource(previousTag);
await page.waitForTimeout(300);
}
const outputFile = `swagger-${toKebabCase(tagName)}-resource-expanded.png`;
console.log(`${step}) Capturing expanded ${tagName} resource...`);
await captureExpandedResource(tagName, outputFile);
console.log(` Saved: ${outputFile}\n`);
previousTag = tagName;
step += 1;
}
console.log('All Web API screenshots captured successfully!');
console.log(`Screenshots saved to: ${imagesDir}\n`);
} catch (error) {
console.error('Error taking screenshots:', error.message);
console.error('\nStack trace:', error.stack);
} finally {
await browser.close();
}
}
captureWebAPIScreenshots().catch(console.error);