@@ -34,17 +34,83 @@ export async function getBrowserTabs(browser: WdioBrowser): Promise<BrowserTab[]
3434 }
3535}
3636
37- export async function getCurrentTabSnapshot ( browser : WdioBrowser ) : Promise < string | null > {
37+ export interface CaptureSnapshotOptions {
38+ includeTags ?: string [ ] ;
39+ includeAttrs ?: string [ ] ;
40+ excludeTags ?: string [ ] ;
41+ excludeAttrs ?: string [ ] ;
42+ truncateText ?: boolean ;
43+ maxTextLength ?: number ;
44+ }
45+
46+ async function captureSnapshot (
47+ browserOrElement : WebdriverIO . Element | WdioBrowser ,
48+ options : CaptureSnapshotOptions = { } ,
49+ context : { type : "browser" | "element" ; fallbackMethod ?: string } ,
50+ ) : Promise < string | null > {
3851 try {
39- const pageSource = await browser . getPageSource ( ) ;
52+ const snapshotResult = await ( browserOrElement as WdioBrowser ) . unstable_captureDomSnapshot ( options ) ;
53+
54+ const notes : string [ ] = [ ] ;
4055
41- if ( ! pageSource || pageSource . trim ( ) . length === 0 ) {
42- return null ;
56+ const omittedParts : string [ ] = [ ] ;
57+ if ( snapshotResult . omittedTags . length > 0 ) {
58+ omittedParts . push ( `tags: ${ snapshotResult . omittedTags . join ( ", " ) } ` ) ;
59+ }
60+ if ( snapshotResult . omittedAttributes . length > 0 ) {
61+ omittedParts . push ( `attributes: ${ snapshotResult . omittedAttributes . join ( ", " ) } ` ) ;
4362 }
4463
45- return pageSource ;
64+ if ( omittedParts . length > 0 ) {
65+ notes . push (
66+ `# Note: ${ omittedParts . join ( " and " ) } were omitted from this ${ context . type } snapshot. If you need them, request the ${ context . type } snapshot again and explicitly specify them as needed.` ,
67+ ) ;
68+ }
69+
70+ if ( snapshotResult . textWasTruncated ) {
71+ notes . push (
72+ `# Note: some text contents/attribute values were truncated. If you need full text contents, request a snapshot with truncateText: false.` ,
73+ ) ;
74+ }
75+
76+ return "```yaml\n" + notes . join ( "\n" ) + "\n" + snapshotResult . snapshot + "\n```" ;
4677 } catch ( error ) {
47- console . error ( "Error getting tab snapshot:" , error ) ;
48- return null ;
78+ console . error ( `Error getting ${ context . type } snapshot:` , error ) ;
79+
80+ if ( context . type === "browser" && browserOrElement . getPageSource ) {
81+ const pageSource = await browserOrElement . getPageSource ( ) ;
82+ return (
83+ "```html\n" +
84+ `<!-- Note: failed to get optimized ${ context . type } snapshot, below is raw page source as a fallback. The error was: ${ ( error as Error ) ?. stack } -->\n\n` +
85+ pageSource +
86+ "\n```"
87+ ) ;
88+ }
89+
90+ if ( context . type === "element" && ( browserOrElement as WebdriverIO . Element ) . getHTML ) {
91+ const elementHTML = await ( browserOrElement as WebdriverIO . Element ) . getHTML ( ) ;
92+ return (
93+ "```html\n" +
94+ `<!-- Note: failed to get ${ context . type } snapshot, below is element HTML as a fallback. The error was: ${ ( error as Error ) ?. message } -->\n\n` +
95+ elementHTML +
96+ "\n```"
97+ ) ;
98+ }
4999 }
100+
101+ return null ;
102+ }
103+
104+ export async function getCurrentTabSnapshot (
105+ browser : WdioBrowser ,
106+ options : CaptureSnapshotOptions = { } ,
107+ ) : Promise < string | null > {
108+ return captureSnapshot ( browser , options , { type : "browser" } ) ;
109+ }
110+
111+ export async function getElementSnapshot (
112+ element : WebdriverIO . Element ,
113+ options : CaptureSnapshotOptions = { } ,
114+ ) : Promise < string | null > {
115+ return captureSnapshot ( element , options , { type : "element" } ) ;
50116}
0 commit comments