File tree 4 files changed +111
-17
lines changed
tests/test-folder/mocha-events/commands
4 files changed +111
-17
lines changed Original file line number Diff line number Diff line change
1
+ describe ( 'suite' , ( ) => {
2
+ describe ( 'should pass' , ( ) => {
3
+ beforeEach ( ( ) => {
4
+ cy . intercept ( 'mytest.com**' , {
5
+ body : `<html>
6
+ <head></head>
7
+ <body>
8
+ <a href="#2" data-qa-id="link-2">My link</a>
9
+ <a href="#3" data-qa-id="link-3">My link 3</a>
10
+ </body>
11
+ </html>
12
+ ` ,
13
+ } ) ;
14
+
15
+ cy . visit ( 'mytest.com' ) ;
16
+ } ) ;
17
+
18
+ it ( 'should click when added during chain (no custom commands)' , ( ) => {
19
+ cy . get ( '[data-qa-id=link-2]' )
20
+ . doSyncCommand ( s => {
21
+ expect ( s . text ( ) ) . eq ( 'My link' ) ;
22
+ } )
23
+ . click ( ) ;
24
+ } ) ;
25
+
26
+ it ( 'should click when added during chain' , ( ) => {
27
+ cy . qaId ( 'link-2' )
28
+ . doSyncCommand ( s => {
29
+ expect ( s . text ( ) ) . eq ( 'My link' ) ;
30
+ } )
31
+ . click ( ) ;
32
+ } ) ;
33
+
34
+ it ( 'should click when added during chain (several)' , ( ) => {
35
+ cy . qaId ( 'link-2' )
36
+ . doSyncCommand ( s => {
37
+ expect ( s . text ( ) ) . eq ( 'My link' ) ;
38
+ } )
39
+ . doSyncCommand ( s => {
40
+ expect ( s . text ( ) ) . eq ( 'My link' ) ;
41
+ } )
42
+ . click ( ) ;
43
+ } ) ;
44
+
45
+ it ( 'should succeed when added before should' , ( ) => {
46
+ cy . qaId ( 'link-60' )
47
+ . doSyncCommand ( ( ) => {
48
+ console . log ( 'hello' ) ;
49
+ } )
50
+ . should ( 'not.exist' ) ;
51
+ } ) ;
52
+
53
+ it ( 'should succeed when cy commands inside' , ( ) => {
54
+ cy . qaId ( 'link-60' )
55
+ . doSyncCommand ( s => {
56
+ cy . log ( 'hello' ) ;
57
+ cy . wrap ( s ) ;
58
+ } )
59
+ . should ( 'not.exist' ) ;
60
+ } ) ;
61
+
62
+ it ( 'should succeed when between cy commands with cy commands inside' , ( ) => {
63
+ cy . get ( 'body' )
64
+ . find ( 'a' )
65
+ . doSyncCommand ( s => {
66
+ cy . log ( 'hello' ) ;
67
+ cy . wrap ( s , { log : false } ) ;
68
+ } )
69
+ . eq ( 1 )
70
+ . should ( 'have.text' , 'My link 3' ) ;
71
+ } ) ;
72
+ } ) ;
73
+ } ) ;
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ declare namespace Cypress {
10
10
myLog ( message : string ) : Chainable < void > ;
11
11
otherCmd ( message : string ) : Chainable < void > ;
12
12
fileExists ( filePath : string ) : Chainable < boolean > ;
13
- qaId ( selector : string ) : Chainable < Element > ;
13
+ qaId ( selector : string ) : Chainable < JQuery > ;
14
14
promiseTest ( delay ?: number ) : Chainable < any > ;
15
15
}
16
16
}
Original file line number Diff line number Diff line change @@ -81,14 +81,8 @@ export const registerCommands = () => {
81
81
}
82
82
83
83
// eslint-disable-next-line @typescript-eslint/no-explicit-any
84
- const getPrevSubjects = ( cmd : any , arr : any [ ] = [ ] ) : any [ ] => {
85
- arr . unshift ( cmd ?. attributes ?. subject ) ;
86
-
87
- if ( cmd ?. attributes ?. prev ) {
88
- return getPrevSubjects ( cmd . attributes . prev , arr ) ;
89
- }
90
-
91
- return arr ;
84
+ const getLastSubject = ( cmd : any ) : any => {
85
+ return cmd ?. get ( 'prev' ) ?. attributes ?. subject ;
92
86
} ;
93
87
94
88
// not changing the subject
@@ -98,12 +92,13 @@ export const registerCommands = () => {
98
92
// const queue = () => (cy as any).queue.queueables;
99
93
// const commandsCount = queue().length;
100
94
// eslint-disable-next-line @typescript-eslint/no-explicit-any
101
- const subjs = getPrevSubjects ( ( cy as any ) . state ( ) ?. current ) ;
95
+ const subj = getLastSubject ( ( cy as any ) . state ( ' current' ) ) ;
102
96
let prevSubj = undefined ;
103
97
104
- if ( subjs . length > 1 ) {
105
- prevSubj = subjs [ subjs . length - 2 ] ;
98
+ if ( subj ) {
99
+ prevSubj = subj ;
106
100
}
101
+
107
102
syncFn ( prevSubj ) ;
108
103
109
104
/* if (queue().length > commandsCount) {
Original file line number Diff line number Diff line change @@ -31,10 +31,31 @@ describe('do sync command', () => {
31
31
cy.visit('mytest.com');
32
32
});
33
33
34
+ it('should click when added during chain (no custom commands)', () => {
35
+ cy.get('[data-qa-id=link-2]')
36
+ .doSyncCommand(s => {
37
+ expect(s.text()).eq('My link');
38
+ })
39
+ .click();
40
+ });
41
+
34
42
it('should click when added during chain', () => {
35
- cy.qaId('link-2').doSyncCommand(() => {
36
- console.log('hello')
37
- }).click();
43
+ cy.qaId('link-2')
44
+ .doSyncCommand(s => {
45
+ expect(s.text()).eq('My link');
46
+ })
47
+ .click();
48
+ });
49
+
50
+ it('should click when added during chain (several)', () => {
51
+ cy.qaId('link-2')
52
+ .doSyncCommand(s => {
53
+ expect(s.text()).eq('My link');
54
+ })
55
+ .doSyncCommand(s => {
56
+ expect(s.text()).eq('My link');
57
+ })
58
+ .click();
38
59
});
39
60
40
61
it('should succeed when added before should', () => {
@@ -70,12 +91,12 @@ describe('do sync command', () => {
70
91
it ( 'should have results' , ( ) => {
71
92
// should not fail run
72
93
checkCyResults ( res ?. result ?. res , {
73
- totalPassed : 4 ,
94
+ totalPassed : 6 ,
74
95
totalFailed : 0 ,
75
96
totalPending : 0 ,
76
97
totalSkipped : 0 ,
77
98
totalSuites : 1 ,
78
- totalTests : 4 ,
99
+ totalTests : 6 ,
79
100
} ) ;
80
101
} ) ;
81
102
beforeAll ( ( ) => {
@@ -108,6 +129,11 @@ describe('do sync command', () => {
108
129
} ,
109
130
] ,
110
131
} ,
132
+ {
133
+ name : 'assert: expected **My link** to equal **My link**' ,
134
+ status : 'passed' ,
135
+ steps : [ ] ,
136
+ } ,
111
137
{
112
138
name : 'click' ,
113
139
status : 'passed' ,
You can’t perform that action at this time.
0 commit comments