@@ -71,6 +71,19 @@ def save_screenshot_and_assert(source, path)
71
71
FileUtils . rm_rf ( path )
72
72
end
73
73
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
+
74
87
describe 'page size' do
75
88
before do
76
89
driver . navigate . to url_for ( 'printPage.html' )
@@ -85,28 +98,21 @@ def save_screenshot_and_assert(source, path)
85
98
viewport_height = driver . execute_script ( "return window.innerHeight;" )
86
99
87
100
screenshot = driver . save_screenshot path
101
+ width , height = png_size ( screenshot )
88
102
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
96
105
end
97
106
98
107
it 'takes full page screenshot' , exclusive : { browser : :firefox } do
99
108
viewport_width = driver . execute_script ( "return window.innerWidth;" )
100
109
viewport_height = driver . execute_script ( "return window.innerHeight;" )
101
110
102
111
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
110
116
end
111
117
112
118
it 'does not take full page screenshot' , except : { browser : :firefox } do
0 commit comments