@@ -689,4 +689,97 @@ describe('"captureDomSnapshot" command', () => {
689689 assert . deepEqual ( result . snapshot . split ( "\n" ) , expected . split ( "\n" ) ) ;
690690 } ) ;
691691 } ) ;
692+
693+ describe ( "selector-based capture" , ( ) => {
694+ beforeEach ( ( ) => {
695+ document . body . innerHTML = `
696+ <div class="container">
697+ <h1>Header</h1>
698+ <div class="content">
699+ <p>Paragraph content</p>
700+ <button id="test-button">Click me</button>
701+ </div>
702+ <footer class="footer">Footer content</footer>
703+ </div>
704+ ` ;
705+ } ) ;
706+
707+ it ( "should capture snapshot of specific element by selector" , ( ) => {
708+ const result = captureDomSnapshotInBrowser ( ".content" ) ;
709+
710+ assert . match ( result . snapshot , / d i v \. c o n t e n t .* @ h i d d e n .* : / ) ;
711+ assert . match ( result . snapshot , / p .* @ h i d d e n .* " P a r a g r a p h c o n t e n t " / ) ;
712+ assert . match ( result . snapshot , / b u t t o n # t e s t - b u t t o n .* @ h i d d e n .* " C l i c k m e " / ) ;
713+ assert . notMatch ( result . snapshot , / h 1 .* " H e a d e r " / ) ;
714+ assert . notMatch ( result . snapshot , / f o o t e r .* " F o o t e r c o n t e n t " / ) ;
715+ } ) ;
716+
717+ it ( "should return error message for non-existent selector" , ( ) => {
718+ const result = captureDomSnapshotInBrowser ( ".non-existent" ) ;
719+
720+ assert . equal ( result . snapshot , "# Element not found: .non-existent" ) ;
721+ assert . deepEqual ( result . omittedTags , [ ] ) ;
722+ assert . deepEqual ( result . omittedAttributes , [ ] ) ;
723+ assert . equal ( result . textWasTruncated , false ) ;
724+ } ) ;
725+
726+ it ( "should return error message for invalid selector" , ( ) => {
727+ const result = captureDomSnapshotInBrowser ( "[invalid selector" ) ;
728+
729+ assert . equal ( result . snapshot , "# Invalid selector: [invalid selector" ) ;
730+ assert . deepEqual ( result . omittedTags , [ ] ) ;
731+ assert . deepEqual ( result . omittedAttributes , [ ] ) ;
732+ assert . equal ( result . textWasTruncated , false ) ;
733+ } ) ;
734+
735+ it ( "should support options with selector" , ( ) => {
736+ const result = captureDomSnapshotInBrowser ( ".content" , {
737+ truncateText : false ,
738+ maxTextLength : 5 ,
739+ } ) ;
740+
741+ assert . match ( result . snapshot , / d i v \. c o n t e n t .* @ h i d d e n .* : / ) ;
742+ assert . match ( result . snapshot , / p .* @ h i d d e n .* " P a r a g r a p h c o n t e n t " / ) ;
743+ assert . equal ( result . textWasTruncated , false ) ;
744+ } ) ;
745+ } ) ;
746+
747+ describe ( "element-based capture" , ( ) => {
748+ let testElement : Element ;
749+
750+ beforeEach ( ( ) => {
751+ document . body . innerHTML = `
752+ <div class="container">
753+ <h1>Header</h1>
754+ <div class="content" id="target-content">
755+ <p>Paragraph content</p>
756+ <button id="test-button">Click me</button>
757+ </div>
758+ <footer class="footer">Footer content</footer>
759+ </div>
760+ ` ;
761+ testElement = document . querySelector ( ".content" ) as Element ;
762+ } ) ;
763+
764+ it ( "should capture snapshot of specific element passed directly" , ( ) => {
765+ const result = captureDomSnapshotInBrowser ( testElement as any ) ;
766+
767+ assert . match ( result . snapshot , / d i v \. c o n t e n t # t a r g e t - c o n t e n t .* @ h i d d e n .* : / ) ;
768+ assert . match ( result . snapshot , / p .* @ h i d d e n .* " P a r a g r a p h c o n t e n t " / ) ;
769+ assert . match ( result . snapshot , / b u t t o n # t e s t - b u t t o n .* @ h i d d e n .* " C l i c k m e " / ) ;
770+ assert . notMatch ( result . snapshot , / h 1 .* " H e a d e r " / ) ;
771+ assert . notMatch ( result . snapshot , / f o o t e r .* " F o o t e r c o n t e n t " / ) ;
772+ } ) ;
773+
774+ it ( "should support options with element" , ( ) => {
775+ const result = captureDomSnapshotInBrowser ( testElement as any , {
776+ maxTextLength : 5 ,
777+ truncateText : true ,
778+ } ) ;
779+
780+ assert . match ( result . snapshot , / d i v \. c o n t e n t # t a r g e t - c o n t e n t .* @ h i d d e n .* : / ) ;
781+ assert . match ( result . snapshot , / p .* @ h i d d e n .* " P a r a g \. \. \. " / ) ;
782+ assert . equal ( result . textWasTruncated , true ) ;
783+ } ) ;
784+ } ) ;
692785} ) ;
0 commit comments