@@ -630,7 +630,7 @@ private Map<String, Object> processFrame(WebElement frameElement, Map<String, Ob
630630 driver .switchTo ().defaultContent ();
631631 } catch (Exception err ) {
632632 throw new RuntimeException (
633- "Fatal: could not exit iframe context after processing \" " + finalFrameUrl + "\" . Driver may be unstable."
633+ "Fatal: could not exit iframe context after processing \" " + finalFrameUrl + "\" . Driver may be unstable." , err
634634 );
635635 }
636636 }
@@ -681,6 +681,14 @@ private Map<String, Object> getSerializedDOM(JavascriptExecutor jse, Set<Cookie>
681681 }
682682 } catch (Exception e ) {
683683 log ("Skipping frame \" " + frameSrc + "\" due to error: " + e .getMessage (), "debug" );
684+ String message = e .getMessage ();
685+ if (message != null && message .contains ("Fatal" )) {
686+ if (e instanceof RuntimeException ) {
687+ throw (RuntimeException ) e ;
688+ } else {
689+ throw new RuntimeException ("Fatal error while processing iframe \" " + frameSrc + "\" " , e );
690+ }
691+ }
684692 }
685693 }
686694 if (!processedFrames .isEmpty ()) {
@@ -689,6 +697,15 @@ private Map<String, Object> getSerializedDOM(JavascriptExecutor jse, Set<Cookie>
689697 }
690698 } catch (Exception e ) {
691699 log ("Failed to process cross-origin iframes: " + e .getMessage (), "debug" );
700+ String message = e .getMessage ();
701+ if (message != null && message .contains ("Fatal" )) {
702+ // Propagate fatal iframe processing errors to avoid returning a corrupted DOM snapshot
703+ if (e instanceof RuntimeException ) {
704+ throw (RuntimeException ) e ;
705+ } else {
706+ throw new RuntimeException ("Fatal error while processing cross-origin iframes" , e );
707+ }
708+ }
692709 }
693710 return mutableSnapshot ;
694711 }
@@ -730,9 +747,7 @@ private static void changeWindowDimensionAndWait(WebDriver driver, int width, in
730747 log ("Resizing using CDP failed, falling back to driver for width " + width + ": " + e .getMessage (), "debug" );
731748 driver .manage ().window ().setSize (new Dimension (width , height ));
732749 }
733-
734750 // Wait for window resize event using WebDriverWait
735- // Made changes to handle handles the temporary null state of resizeCountObj during page reload
736751 try {
737752 WebDriverWait wait = new WebDriverWait (driver , Duration .ofSeconds (1 ));
738753 wait .until ((ExpectedCondition <Boolean >) d -> {
@@ -747,14 +762,36 @@ private static void changeWindowDimensionAndWait(WebDriver driver, int width, in
747762 }
748763 }
749764
750- // Capture responsive DOM for different widths
765+ private List <Integer > extractResponsiveWidths (Map <String , Object > options ) {
766+ if (options == null ) {
767+ return null ;
768+ }
769+ Object widthsOption = options .get ("widths" );
770+ if (!(widthsOption instanceof List <?>)) {
771+ return null ;
772+ }
773+ List <?> rawWidths = (List <?>) widthsOption ;
774+ List <Integer > coercedWidths = new ArrayList <>();
775+ for (Object value : rawWidths ) {
776+ if (value instanceof Number ) {
777+ coercedWidths .add (((Number ) value ).intValue ());
778+ } else if (value instanceof String ) {
779+ try {
780+ coercedWidths .add (Integer .parseInt ((String ) value ));
781+ } catch (NumberFormatException ignore ) {
782+ }
783+ }
784+ }
785+ return coercedWidths .isEmpty () ? null : coercedWidths ;
786+ }
787+
751788 public List <Map <String , Object >> captureResponsiveDom (WebDriver driver , Set <Cookie > cookies , Map <String , Object > options ) {
752- List <Map <String , Object >> widths = getResponsiveWidths ((List <Integer >) options .get ("widths" ));
789+ List <Integer > responsiveWidths = extractResponsiveWidths (options );
790+ List <Map <String , Object >> widths = getResponsiveWidths (responsiveWidths );
753791 List <Map <String , Object >> domSnapshots = new ArrayList <>();
754792 Dimension windowSize = driver .manage ().window ().getSize ();
755793 int currentWidth = windowSize .getWidth ();
756794 int currentHeight = windowSize .getHeight ();
757- log ("Initial window size: " + currentWidth + "x" + currentHeight , "debug" );
758795 int lastWindowWidth = currentWidth ;
759796 int resizeCount = 0 ;
760797 JavascriptExecutor jse = (JavascriptExecutor ) driver ;
@@ -775,7 +812,6 @@ public List<Map<String, Object>> captureResponsiveDom(WebDriver driver, Set<Cook
775812 Object result = jse .executeScript ("return window.outerHeight - window.innerHeight + " + minHeight );
776813 if (result instanceof Number ) {
777814 targetHeight = ((Number ) result ).intValue ();
778- log ("Calculated height for responsive capture using minHeight: " + targetHeight , "debug" );
779815 }
780816 } catch (NumberFormatException e ) {
781817 log ("Invalid minHeight value " + minHeightObj + "; expected integer, using current window height instead." , "debug" );
@@ -793,16 +829,13 @@ public List<Map<String, Object>> captureResponsiveDom(WebDriver driver, Set<Cook
793829 }
794830 int width = ((Number ) widthObj ).intValue ();
795831 Object heightObj = widthMap .get ("height" );
796- log ("Width entry: width=" + width + ", height from widths config=" + heightObj + ", targetHeight=" + targetHeight , "debug" );
797832 int heightForWidth = (heightObj instanceof Number )? ((Number ) heightObj ).intValue (): targetHeight ;
798833 if (lastWindowWidth != width ) {
799834 resizeCount ++;
800- log ("Resizing window to width=" + width + ", height=" + heightForWidth , "debug" );
801835 changeWindowDimensionAndWait (driver , width , heightForWidth , resizeCount );
802836 lastWindowWidth = width ;
803837 }
804838 if ("true" .equals (PERCY_RESPONSIVE_CAPTURE_RELOAD_PAGE )) {
805- log ("Reloading page for width: " + width , "debug" );
806839 driver .navigate ().refresh ();
807840 jse .executeScript (fetchPercyDOM ());
808841 jse .executeScript ("PercyDOM.waitForResize()" );
@@ -823,8 +856,9 @@ public List<Map<String, Object>> captureResponsiveDom(WebDriver driver, Set<Cook
823856
824857 return domSnapshots ;
825858 }
826- protected static void log (String message ) {
827- log (message , "info" );
859+
860+ protected static void log (String message ) {
861+ log (message , "info" );
828862 }
829863
830864 protected static void log (String message , String level ) {
0 commit comments