44
44
import org .openqa .selenium .Dimension ;
45
45
import org .openqa .selenium .ImmutableCapabilities ;
46
46
import org .openqa .selenium .JavascriptExecutor ;
47
+ import org .openqa .selenium .OutputType ;
47
48
import org .openqa .selenium .Point ;
49
+ import org .openqa .selenium .TakesScreenshot ;
48
50
import org .openqa .selenium .WebDriver ;
49
51
import org .openqa .selenium .WebDriverException ;
50
52
import org .openqa .selenium .WebElement ;
@@ -285,9 +287,11 @@ public void afterPerform(WebDriver driver, Collection<Sequence> actions) {
285
287
286
288
@ Test
287
289
void shouldFireWebElementEvents () {
290
+ String result = "result" ;
288
291
WebDriver driver = mock (WebDriver .class );
289
292
WebElement element = mock (WebElement .class );
290
293
when (driver .findElement (any ())).thenReturn (element );
294
+ when (element .getScreenshotAs (OutputType .BASE64 )).thenReturn (result );
291
295
292
296
CollectorListener listener =
293
297
new CollectorListener () {
@@ -467,6 +471,20 @@ public void beforeGetCssValue(WebElement element, String propertyName) {
467
471
public void afterGetCssValue (WebElement element , String propertyName , String result ) {
468
472
acc .append ("afterGetCssValue" ).append ("\n " );
469
473
}
474
+
475
+ @ Override
476
+ public <X > void beforeGetScreenshotAs (WebElement element , OutputType <X > target ) {
477
+ acc .append ("beforeGetScreenshotAs " ).append (target ).append ("\n " );
478
+ }
479
+
480
+ @ Override
481
+ public <X > void afterGetScreenshotAs (WebElement element , OutputType <X > target , X result ) {
482
+ acc .append ("afterGetScreenshotAs " )
483
+ .append (target )
484
+ .append (" " )
485
+ .append (result )
486
+ .append ("\n " );
487
+ }
470
488
};
471
489
472
490
WebDriver decorated = new EventFiringDecorator <>(listener ).decorate (driver );
@@ -485,6 +503,7 @@ public void afterGetCssValue(WebElement element, String propertyName, String res
485
503
element1 .getSize ();
486
504
element1 .getCssValue ("test" );
487
505
element1 .clear ();
506
+ element1 .getScreenshotAs (OutputType .BASE64 );
488
507
489
508
assertThat (listener .acc .toString ().trim ())
490
509
.isEqualTo (
@@ -545,7 +564,11 @@ public void afterGetCssValue(WebElement element, String propertyName, String res
545
564
"beforeAnyWebElementCall clear" ,
546
565
"beforeClear" ,
547
566
"afterClear" ,
548
- "afterAnyWebElementCall clear" ));
567
+ "afterAnyWebElementCall clear" ,
568
+ "beforeAnyWebElementCall getScreenshotAs" ,
569
+ "beforeGetScreenshotAs OutputType.BASE64" ,
570
+ "afterGetScreenshotAs OutputType.BASE64 " + result ,
571
+ "afterAnyWebElementCall getScreenshotAs" ));
549
572
}
550
573
551
574
@ Test
@@ -973,6 +996,44 @@ public void afterAlert(WebDriver.TargetLocator targetLocator, Alert alert) {
973
996
"afterAnyCall alert" ));
974
997
}
975
998
999
+ @ Test
1000
+ void shouldFireWebDriverTakesScreenshotEvents () {
1001
+ String result = "result" ;
1002
+ WebDriver driver = mock (WebDriver .class , withSettings ().extraInterfaces (TakesScreenshot .class ));
1003
+ when (((TakesScreenshot ) driver ).getScreenshotAs (OutputType .BASE64 )).thenReturn (result );
1004
+
1005
+ CollectorListener listener =
1006
+ new CollectorListener () {
1007
+ @ Override
1008
+ public <X > void beforeGetScreenshotAs (WebDriver driver , OutputType <X > target ) {
1009
+ acc .append ("beforeGetScreenshotAs " ).append (target ).append ("\n " );
1010
+ }
1011
+
1012
+ @ Override
1013
+ public <X > void afterGetScreenshotAs (WebDriver driver , OutputType <X > target , X result ) {
1014
+ acc .append ("afterGetScreenshotAs " )
1015
+ .append (target )
1016
+ .append (" " )
1017
+ .append (result )
1018
+ .append ("\n " );
1019
+ }
1020
+ };
1021
+ WebDriver decorated = new EventFiringDecorator <>(listener ).decorate (driver );
1022
+
1023
+ ((TakesScreenshot ) decorated ).getScreenshotAs (OutputType .BASE64 );
1024
+
1025
+ assertThat (listener .acc .toString ().trim ())
1026
+ .isEqualTo (
1027
+ String .join (
1028
+ "\n " ,
1029
+ "beforeAnyCall getScreenshotAs" ,
1030
+ "beforeAnyWebDriverCall getScreenshotAs" ,
1031
+ "beforeGetScreenshotAs OutputType.BASE64" ,
1032
+ "afterGetScreenshotAs OutputType.BASE64 " + result ,
1033
+ "afterAnyWebDriverCall getScreenshotAs" ,
1034
+ "afterAnyCall getScreenshotAs" ));
1035
+ }
1036
+
976
1037
@ Test
977
1038
void shouldSuppressExceptionInBeforeAnyCall () {
978
1039
WebDriver driver = mock (WebDriver .class );
0 commit comments