@@ -19,7 +19,7 @@ public async Task ShouldWorkWithFile()
1919
2020 using ( var page = await Browser . NewPageAsync ( ) )
2121 {
22- await page . SetViewport ( new ViewPortOptions
22+ await page . SetViewportAsync ( new ViewPortOptions
2323 {
2424 Width = 500 ,
2525 Height = 500
@@ -36,7 +36,7 @@ await page.SetViewport(new ViewPortOptions
3636
3737 fileInfo = new FileInfo ( outputFile ) ;
3838 Assert . True ( new FileInfo ( outputFile ) . Length > 0 ) ;
39- Assert . True ( PixelMatch ( "screenshot-sanity.png" , outputFile ) ) ;
39+ Assert . True ( ScreenshotHelper . PixelMatch ( "screenshot-sanity.png" , outputFile ) ) ;
4040
4141 if ( fileInfo . Exists )
4242 {
@@ -50,14 +50,14 @@ public async Task ShouldWork()
5050 {
5151 using ( var page = await Browser . NewPageAsync ( ) )
5252 {
53- await page . SetViewport ( new ViewPortOptions
53+ await page . SetViewportAsync ( new ViewPortOptions
5454 {
5555 Width = 500 ,
5656 Height = 500
5757 } ) ;
5858 await page . GoToAsync ( TestConstants . ServerUrl + "/grid.html" ) ;
5959 var screenshot = await page . ScreenshotStreamAsync ( ) ;
60- Assert . True ( PixelMatch ( "screenshot-sanity.png" , screenshot ) ) ;
60+ Assert . True ( ScreenshotHelper . PixelMatch ( "screenshot-sanity.png" , screenshot ) ) ;
6161 }
6262 }
6363
@@ -66,7 +66,7 @@ public async Task ShouldClipRect()
6666 {
6767 using ( var page = await Browser . NewPageAsync ( ) )
6868 {
69- await page . SetViewport ( new ViewPortOptions
69+ await page . SetViewportAsync ( new ViewPortOptions
7070 {
7171 Width = 500 ,
7272 Height = 500
@@ -82,7 +82,7 @@ await page.SetViewport(new ViewPortOptions
8282 Height = 100
8383 }
8484 } ) ;
85- Assert . True ( PixelMatch ( "screenshot-clip-rect.png" , screenshot ) ) ;
85+ Assert . True ( ScreenshotHelper . PixelMatch ( "screenshot-clip-rect.png" , screenshot ) ) ;
8686 }
8787 }
8888
@@ -91,7 +91,7 @@ public async Task ShouldWorkForOffscreenClip()
9191 {
9292 using ( var page = await Browser . NewPageAsync ( ) )
9393 {
94- await page . SetViewport ( new ViewPortOptions
94+ await page . SetViewportAsync ( new ViewPortOptions
9595 {
9696 Width = 500 ,
9797 Height = 500
@@ -107,7 +107,7 @@ await page.SetViewport(new ViewPortOptions
107107 Height = 100
108108 }
109109 } ) ;
110- Assert . True ( PixelMatch ( "screenshot-offscreen-clip.png" , screenshot ) ) ;
110+ Assert . True ( ScreenshotHelper . PixelMatch ( "screenshot-offscreen-clip.png" , screenshot ) ) ;
111111 }
112112 }
113113
@@ -116,7 +116,7 @@ public async Task ShouldRunInParallel()
116116 {
117117 using ( var page = await Browser . NewPageAsync ( ) )
118118 {
119- await page . SetViewport ( new ViewPortOptions
119+ await page . SetViewportAsync ( new ViewPortOptions
120120 {
121121 Width = 500 ,
122122 Height = 500
@@ -139,7 +139,7 @@ await page.SetViewport(new ViewPortOptions
139139 }
140140
141141 await Task . WhenAll ( tasks ) ;
142- Assert . True ( PixelMatch ( "grid-cell-1.png" , tasks [ 0 ] . Result ) ) ;
142+ Assert . True ( ScreenshotHelper . PixelMatch ( "grid-cell-1.png" , tasks [ 0 ] . Result ) ) ;
143143 }
144144 }
145145
@@ -148,7 +148,7 @@ public async Task ShouldTakeFullPageScreenshots()
148148 {
149149 using ( var page = await Browser . NewPageAsync ( ) )
150150 {
151- await page . SetViewport ( new ViewPortOptions
151+ await page . SetViewportAsync ( new ViewPortOptions
152152 {
153153 Width = 500 ,
154154 Height = 500
@@ -158,7 +158,7 @@ await page.SetViewport(new ViewPortOptions
158158 {
159159 FullPage = true
160160 } ) ;
161- Assert . True ( PixelMatch ( "screenshot-grid-fullpage.png" , screenshot ) ) ;
161+ Assert . True ( ScreenshotHelper . PixelMatch ( "screenshot-grid-fullpage.png" , screenshot ) ) ;
162162 }
163163 }
164164
@@ -200,7 +200,7 @@ public async Task ShouldRunInParallelInMultiplePages()
200200
201201 for ( var i = 0 ; i < n ; i ++ )
202202 {
203- Assert . True ( PixelMatch ( $ "grid-cell-{ i } .png", screenshotTasks [ i ] . Result ) ) ;
203+ Assert . True ( ScreenshotHelper . PixelMatch ( $ "grid-cell-{ i } .png", screenshotTasks [ i ] . Result ) ) ;
204204 }
205205
206206 var closeTasks = new List < Task > ( ) ;
@@ -217,7 +217,7 @@ public async Task ShouldAllowTransparency()
217217 {
218218 using ( var page = await Browser . NewPageAsync ( ) )
219219 {
220- await page . SetViewport ( new ViewPortOptions
220+ await page . SetViewportAsync ( new ViewPortOptions
221221 {
222222 Width = 100 ,
223223 Height = 100
@@ -228,7 +228,7 @@ await page.SetViewport(new ViewPortOptions
228228 OmitBackground = true
229229 } ) ;
230230
231- Assert . True ( PixelMatch ( "transparent.png" , screenshot ) ) ;
231+ Assert . True ( ScreenshotHelper . PixelMatch ( "transparent.png" , screenshot ) ) ;
232232 }
233233 }
234234
@@ -248,58 +248,8 @@ public async Task ShouldWorkWithOddClipSizeOnRetinaDisplays()
248248 }
249249 } ) ;
250250
251- Assert . True ( PixelMatch ( "screenshot-clip-odd-size.png" , screenshot ) ) ;
251+ Assert . True ( ScreenshotHelper . PixelMatch ( "screenshot-clip-odd-size.png" , screenshot ) ) ;
252252 }
253253 }
254-
255- private bool PixelMatch ( string screenShotFile , string fileName )
256- {
257- using ( Stream stream = File . Open ( fileName , FileMode . Open ) )
258- {
259- return PixelMatch ( screenShotFile , stream ) ;
260- }
261- }
262-
263- private bool PixelMatch ( string screenShotFile , Stream screenshot )
264- {
265- const int pixelThreshold = 10 ;
266- const decimal totalTolerance = 0.05m ;
267-
268- var baseImage = Image . Load ( Path . Combine ( TestUtils . FindParentDirectory ( "Screenshots" ) , screenShotFile ) ) ;
269- var compareImage = Image . Load ( screenshot ) ;
270-
271- //Just for debugging purpose
272- compareImage . Save ( Path . Combine ( TestUtils . FindParentDirectory ( "Screenshots" ) , "test.png" ) ) ;
273-
274- if ( baseImage . Width != compareImage . Width || baseImage . Height != compareImage . Height )
275- {
276- return false ;
277- }
278-
279- var rgb1 = default ( Rgb24 ) ;
280- var rgb2 = default ( Rgb24 ) ;
281- var invalidPixelsCount = 0 ;
282-
283- for ( int y = 0 ; y < baseImage . Height ; y ++ )
284- {
285- for ( int x = 0 ; x < baseImage . Width ; x ++ )
286- {
287- var pixelA = baseImage [ x , y ] ;
288- var pixelB = compareImage [ x , y ] ;
289-
290- pixelA . ToRgb24 ( ref rgb1 ) ;
291- pixelB . ToRgb24 ( ref rgb2 ) ;
292-
293- if ( Math . Abs ( rgb1 . R - rgb2 . R ) > pixelThreshold ||
294- Math . Abs ( rgb1 . G - rgb2 . G ) > pixelThreshold ||
295- Math . Abs ( rgb1 . B - rgb2 . B ) > pixelThreshold )
296- {
297- invalidPixelsCount ++ ;
298- }
299- }
300- }
301-
302- return ( invalidPixelsCount / ( baseImage . Height * baseImage . Width ) ) < totalTolerance ;
303- }
304254 }
305255}
0 commit comments