Skip to content

Commit 8125df3

Browse files
committed
Refactor takes_screenshot_spec to work on Windows
We need to explicitly pass `mode: "rb"` - otherwise the file is not read as a binary.
1 parent 78d9e8e commit 8125df3

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

rb/spec/integration/selenium/webdriver/takes_screenshot_spec.rb

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ def save_screenshot_and_assert(source, path)
7171
FileUtils.rm_rf(path)
7272
end
7373

74+
def png_size(path)
75+
png = File.read(path, mode: 'rb')[0x10..0x18]
76+
width = png.unpack1('NN')
77+
height = png.unpack('NN').last
78+
79+
if !Platform.linux?
80+
width /= 2
81+
height /= 2
82+
end
83+
84+
[width, height]
85+
end
86+
7487
describe 'page size' do
7588
before do
7689
driver.navigate.to url_for('printPage.html')
@@ -85,28 +98,21 @@ def save_screenshot_and_assert(source, path)
8598
viewport_height = driver.execute_script("return window.innerHeight;")
8699

87100
screenshot = driver.save_screenshot path
101+
width, height = png_size(screenshot)
88102

89-
if Platform.linux?
90-
expect(File.read(screenshot)[0x10..0x18].unpack1('NN')).to be <= viewport_width
91-
expect(File.read(screenshot)[0x10..0x18].unpack('NN').last).to be <= viewport_height
92-
else
93-
expect(File.read(screenshot)[0x10..0x18].unpack1('NN') / 2).to be <= viewport_width
94-
expect(File.read(screenshot)[0x10..0x18].unpack('NN').last / 2).to be <= viewport_height
95-
end
103+
expect(width).to be <= viewport_width
104+
expect(height).to be <= viewport_height
96105
end
97106

98107
it 'takes full page screenshot', exclusive: {browser: :firefox} do
99108
viewport_width = driver.execute_script("return window.innerWidth;")
100109
viewport_height = driver.execute_script("return window.innerHeight;")
101110

102111
screenshot = driver.save_screenshot path, full_page: true
103-
if Platform.linux?
104-
expect(File.read(screenshot)[0x10..0x18].unpack1('NN')).to be >= viewport_width
105-
expect(File.read(screenshot)[0x10..0x18].unpack('NN').last).to be > viewport_height
106-
else
107-
expect(File.read(screenshot)[0x10..0x18].unpack1('NN') / 2).to be >= viewport_width
108-
expect(File.read(screenshot)[0x10..0x18].unpack('NN').last / 2).to be > viewport_height
109-
end
112+
width, height = png_size(screenshot)
113+
114+
expect(width).to be >= viewport_width
115+
expect(height).to be > viewport_height
110116
end
111117

112118
it 'does not take full page screenshot', except: {browser: :firefox} do

0 commit comments

Comments
 (0)