File tree 10 files changed +70
-0
lines changed
examples/stubbing-spying__window-print
10 files changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -83,6 +83,7 @@ Recipe | Description
83
83
[ Stubbing Functions] ( ./examples/stubbing-spying__functions ) | Use ` cy.spy() ` and ` cy.stub() ` to test function calls
84
84
[ Stubbing ` window.fetch ` ] ( ./examples/stubbing-spying__window-fetch ) | Work around the ` window.fetch ` limitation
85
85
[ Stubbing ` window.open ` and ` console.log ` ] ( ./examples/stubbing-spying__window ) | Use ` cy.stub() ` and ` cy.spy() ` to test application behavior
86
+ [ Stubbing ` window.print ` ] ( ./examples/stubbing-spying__window-print ) | Use ` cy.stub() ` to test ` window.print ` call made from the application
86
87
[ Stubbing Google Analytics] ( ./examples/stubbing-spying__google-analytics ) | Use ` cy.stub() ` to test Google Analytics calls
87
88
[ Spying and stubbing methods on ` console ` object] ( ./examples/stubbing-spying__console ) | Use ` cy.spy() ` and ` cy.stub() ` on ` console.log `
88
89
[ Stub resource loading] ( ./examples/stubbing__resources ) | Use ` MutationObserver ` to stub resource loading like ` img ` tags
Original file line number Diff line number Diff line change @@ -222,6 +222,8 @@ jobs:
222
222
<< : *defaults
223
223
stubbing-spying__window-fetch :
224
224
<< : *defaults
225
+ stubbing-spying__window-print :
226
+ << : *defaults
225
227
stubbing-spying__google-analytics :
226
228
<< : *defaults
227
229
stubbing-spying__navigator :
@@ -407,6 +409,9 @@ all_jobs: &all_jobs
407
409
- stubbing-spying__window-fetch :
408
410
requires :
409
411
- build
412
+ - stubbing-spying__window-print :
413
+ requires :
414
+ - build
410
415
- stubbing-spying__console :
411
416
requires :
412
417
- build
@@ -536,6 +541,7 @@ all_jobs: &all_jobs
536
541
- server-communication__stream-tests
537
542
- stubbing-spying__functions
538
543
- stubbing-spying__window-fetch
544
+ - stubbing-spying__window-print
539
545
- stubbing-spying__google-analytics
540
546
- stubbing-spying__navigator
541
547
- stubbing-spying__window
Original file line number Diff line number Diff line change
1
+ # Stubbing ` window.print `
2
+
3
+ This is an example showing how to stub ` window.print ` method call
4
+
5
+ ## The application
6
+
7
+ The page [ index.html] ( index.html ) calls ` window.print ` on button click. Without stubbing, the test would click and then a system print dialog would block the rest of the test.
8
+
9
+ ![ System print dialog] ( images/print-dialog.png )
10
+
11
+ By stubbing the ` window.print ` the [ spec.js] ( cypress/integration/spec.js ) can confirm the call has happened.
12
+
13
+ ![ Stub window print test] ( images/stub-print.png )
Original file line number Diff line number Diff line change
1
+ /* eslint-env browser */
2
+ document . getElementById ( 'print-window' )
3
+ . addEventListener ( 'click' , ( ) => {
4
+ window . print ( )
5
+ } )
Original file line number Diff line number Diff line change
1
+ {
2
+ "fixturesFolder" : false ,
3
+ "pluginsFile" : false ,
4
+ "supportFile" : false
5
+ }
Original file line number Diff line number Diff line change
1
+ /// <reference types="cypress" />
2
+
3
+ describe ( 'window.print' , ( ) => {
4
+ it ( 'can be stubbed' , ( ) => {
5
+ cy . visit ( 'index.html' )
6
+ cy . window ( ) . then ( ( win ) => {
7
+ cy . stub ( win , 'print' ) . as ( 'print' )
8
+ } )
9
+
10
+ cy . contains ( 'button' , 'Print' ) . click ( )
11
+ cy . get ( '@print' ) . should ( 'have.been.calledOnce' )
12
+ } )
13
+ } )
Original file line number Diff line number Diff line change
1
+ <!doctype html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="utf-8 ">
5
+ < title > Stubbing Window Print</ title >
6
+ </ head >
7
+ < body >
8
+ < h1 > Application</ h1 >
9
+ < ul >
10
+ < li >
11
+ < button id ="print-window "> Print 🖨</ button >
12
+ </ li >
13
+ </ ul >
14
+
15
+ < script src ="./app.js "> </ script >
16
+ </ body >
17
+ </ html >
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " stubbing-window-print" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " Stubbing window.print from test" ,
5
+ "scripts" : {
6
+ "cypress:open" : " ../../node_modules/.bin/cypress open" ,
7
+ "cypress:run" : " ../../node_modules/.bin/cypress run" ,
8
+ "test:ci" : " npm run cypress:run"
9
+ }
10
+ }
You can’t perform that action at this time.
0 commit comments