Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-iframe-emulation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wdio/image-comparison-core": patch
---

fix: prevent false emulation detection when checkElement is called inside an iframe after switchFrame
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,33 @@ describe('getScreenDimensions', () => {
expect(dimensions.dimensions.window.screenHeight).toBe(1800)
})

it('should not detect emulation when running inside an iframe', () => {
const originalSelf = window.self
Object.defineProperty(window, 'self', {
value: {} as Window,
configurable: true,
writable: true,
})

Object.defineProperty(window, 'devicePixelRatio', { value: 3, configurable: true })
Object.defineProperty(window, 'innerWidth', { value: 50, configurable: true })
Object.defineProperty(window, 'innerHeight', { value: 100, configurable: true })
Object.defineProperty(window, 'matchMedia', {
value: vi.fn().mockImplementation(() => ({ matches: false })),
...CONFIGURABLE,
})

const dimensions = getScreenDimensions(false)

Object.defineProperty(window, 'self', {
value: originalSelf,
configurable: true,
writable: true,
})

expect(dimensions.dimensions.window.isEmulated).toBe(false)
})

it('should handle zero devicePixelRatio', () => {
Object.defineProperty(window, 'devicePixelRatio', { value: 0, configurable: true })
Object.defineProperty(window, 'innerWidth', { value: 1920, configurable: true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export default function getScreenDimensions(isMobile: boolean): ScreenDimensions
const dpr = window.devicePixelRatio || 1
const minEdge = Math.min(width, height)
const maxEdge = Math.max(width, height)
const isInIframe = window.self !== window.top
const isLikelyEmulated =
!isMobile && // Only check for emulated on desktop
!isInIframe && // Skip emulation detection inside iframes
dpr >= 2 && // High-DPI signal
minEdge <= 800 && // Catch phones/tablets in portrait/landscape
maxEdge <= 1280 && // Conservative max for emulated tablet sizes
Expand Down