11//Imports
22require ( 'cypress-plugin-tab' ) ;
3+ var fs = require ( 'fs' ) ;
34
45const url = Cypress . env ( 'baseUrl' ) || "https://uniandes.edu.co/" ;
56const appName = Cypress . env ( 'appName' ) || "your app" ;
@@ -20,6 +21,24 @@ function getRandomInt(min, max) {
2021 return Math . floor ( Math . random ( ) * ( max - min ) ) + min ;
2122} ;
2223
24+ function fullPath ( el ) {
25+ var names = [ ] ;
26+ while ( el . parentNode ) {
27+ if ( el . id ) {
28+ names . unshift ( '#' + el . id ) ;
29+ break ;
30+ } else {
31+ if ( el == el . ownerDocument . documentElement ) names . unshift ( el . tagName ) ;
32+ else {
33+ for ( var c = 1 , e = el ; e . previousElementSibling ; e = e . previousElementSibling , c ++ ) ;
34+ names . unshift ( el . tagName + ":nth-child(" + c + ")" ) ;
35+ }
36+ el = el . parentNode ;
37+ }
38+ }
39+ return names . join ( " > " ) ;
40+ }
41+
2342
2443//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2544//Start of random monkey
@@ -42,36 +61,37 @@ function randClick(){
4261 if ( ! ! element . id ) { //boolean that indicates if the element has a non-empty id
4362 cy . get ( `#${ element . id } ` ) . click ( ) ;
4463 }
64+ /*
4565 else if(!!element.className){ //boolean that indicates if the element has a non-empty className
46- let className = element . className . split ( " " ) [ 0 ] ;
66+ let className = element.tagName.splice(0,1)+'.'+element. className.replace(/\s/g, ".") ;
4767 cy.get(`.${className}`).then($candidates => {
4868 //click the first visible candidate
4969 for(let i = 0; i < $candidates.length; i++){
5070 let candidate = $candidates.get(i);
5171 if(!Cypress.dom.isHidden(candidate)){
5272 cy.wrap(candidate).click({force:true});
73+ break;
5374 }
5475 }
5576 });
56- }
77+ }*/
5778 else {
58- cy . get ( element . tagName ) . then ( $candidates => {
79+ cy . get ( fullPath ( element ) ) . then ( $candidates => {
5980 //click the first visible candidate
6081 for ( let i = 0 ; i < $candidates . length ; i ++ ) {
6182 let candidate = $candidates . get ( i ) ;
6283 if ( ! Cypress . dom . isHidden ( candidate ) ) {
63- cy . wrap ( candidate ) . click ( { force :true } ) ;
84+ cy . wrap ( candidate ) . rightclick ( { force :true } ) ;
85+ break ;
6486 }
6587 }
6688 } ) ;
67-
6889 }
6990 }
7091 else {
7192 cy . get ( 'body' ) . click ( randX , randY , { force :true } ) ;
7293 }
7394 focused = ! ! win . document . activeElement ;
74-
7595 } )
7696}
7797
@@ -90,25 +110,28 @@ function randDClick(){
90110 if ( ! ! element . id ) { //boolean that indicates if the element has a non-empty id
91111 cy . get ( `#${ element . id } ` ) . dblclick ( ) ;
92112 }
113+ /*
93114 else if(!!element.className){ //boolean that indicates if the element has a non-empty className
94- let className = element . className . split ( " " ) [ 0 ] ;
115+ let className = element.tagName.splice(0,1)+'.'+element. className.replace(/\s/g, ".") ;
95116 cy.get(`.${className}`).then($candidates => {
96117 //dblclick the first visible candidate
97118 for(let i = 0; i < $candidates.length; i++){
98119 let candidate = $candidates.get(i);
99120 if(!Cypress.dom.isHidden(candidate)){
100121 cy.wrap(candidate).dblclick({force:true});
122+ break;
101123 }
102124 }
103125 });
104- }
126+ }*/
105127 else {
106- cy . get ( element . tagName ) . then ( $candidates => {
128+ cy . get ( fullPath ( element ) ) . then ( $candidates => {
107129 //dblclick the first visible candidate
108130 for ( let i = 0 ; i < $candidates . length ; i ++ ) {
109131 let candidate = $candidates . get ( i ) ;
110132 if ( ! Cypress . dom . isHidden ( candidate ) ) {
111- cy . wrap ( candidate ) . dblclick ( { force :true } ) ;
133+ cy . wrap ( candidate ) . rightclick ( { force :true } ) ;
134+ break ;
112135 }
113136 }
114137 } ) ;
@@ -136,25 +159,27 @@ function randRClick(){
136159 if ( ! ! element . id ) { //boolean that indicates if the element has a non-empty id
137160 cy . get ( `#${ element . id } ` ) . rightclick ( ) ;
138161 }
139- else if ( ! ! element . className ) { //boolean that indicates if the element has a non-empty className
140- let className = element . className . split ( " " ) [ 0 ] ;
162+ /* else if(!!element.className){ //boolean that indicates if the element has a non-empty className
163+ let className = element.tagName.splice(0,1)+'.'+element. className.replace(/\s/g, ".") ;
141164 cy.get(`.${className}`).then($candidates => {
142165 //rightclick the first visible candidate
143166 for(let i = 0; i < $candidates.length; i++){
144167 let candidate = $candidates.get(i);
145168 if(!Cypress.dom.isHidden(candidate)){
146169 cy.wrap(candidate).rightclick({force:true});
170+ break;
147171 }
148172 }
149173 });
150- }
174+ }*/
151175 else {
152- cy . get ( element . tagName ) . then ( $candidates => {
176+ cy . get ( fullPath ( element ) ) . then ( $candidates => {
153177 //rightclick the first visible candidate
154178 for ( let i = 0 ; i < $candidates . length ; i ++ ) {
155179 let candidate = $candidates . get ( i ) ;
156180 if ( ! Cypress . dom . isHidden ( candidate ) ) {
157181 cy . wrap ( candidate ) . rightclick ( { force :true } ) ;
182+ break ;
158183 }
159184 }
160185 } ) ;
@@ -181,16 +206,29 @@ function randHover(){
181206 if ( ! ! element . id ) { //boolean that indicates if the element has a non-empty id
182207 cy . get ( `#${ element . id } ` ) . trigger ( 'mouseover' ) ;
183208 }
184- else if ( ! ! element . className ) { //boolean that indicates if the element has a non-empty className
209+ /* else if(!!element.className){ //boolean that indicates if the element has a non-empty className
185210 cy.get(`.${element.className}`).then($candidates => {
186211 //rightclick the first visible candidate
187212 for(let i = 0; i < $candidates.length; i++){
188213 let candidate = $candidates.get(i);
189214 if(!Cypress.dom.isHidden(candidate)){
190215 cy.wrap(candidate).trigger('mouseover');
216+ break;
191217 }
192218 }
193219 })
220+ }*/
221+ else {
222+ cy . get ( fullPath ( element ) ) . then ( $candidates => {
223+ //hover the first visible candidate
224+ for ( let i = 0 ; i < $candidates . length ; i ++ ) {
225+ let candidate = $candidates . get ( i ) ;
226+ if ( ! Cypress . dom . isHidden ( candidate ) ) {
227+ cy . wrap ( candidate ) . trigger ( 'mouseover' ) ;
228+ break ;
229+ }
230+ }
231+ } ) ;
194232 }
195233 }
196234 }
@@ -248,7 +286,7 @@ function typeCharKey(){
248286}
249287
250288function spkeypress ( ) {
251- const specialKeys = [ "{{}" , "{backspace}" , "{del}" , "{downarrow}" , "{end}" , "{enter}" , "{ esc}", "{home}" , "{leftarrow}" , "{pagedown}" , "{pageup}" , "{rightarrow}" , "{selectall}" , "{uparrow}" ] ;
289+ const specialKeys = [ "{{}" , "{backspace}" , "{del}" , "{downarrow}" , "{end}" , "{esc}" , "{home}" , "{leftarrow}" , "{pagedown}" , "{pageup}" , "{rightarrow}" , "{selectall}" , "{uparrow}" ] ;
252290 const modifiers = [ "{alt}" , "{ctrl}" , "{meta}" , "{shift}" , "" ] ;
253291 let modIndex = getRandomInt ( 0 , modifiers . length - 1 ) ;
254292 let spkIndex = getRandomInt ( 0 , specialKeys . length - 1 ) ;
@@ -289,6 +327,15 @@ function tab(){
289327 focused = true
290328}
291329
330+ function getEvtType ( i ) {
331+ if ( i === 0 ) return "Random click"
332+ else if ( i === 1 ) return "Scroll event"
333+ else if ( i === 2 ) return "Selector focus"
334+ else if ( i === 3 ) return "Keypress"
335+ else if ( i === 4 ) return "Special Keypress"
336+ else if ( i === 5 ) return "Page Navigation"
337+ }
338+
292339//Aggregate in a matrix-like constant
293340const functions = [
294341 [ randClick , randDClick , randRClick ] ,
@@ -299,13 +346,18 @@ const functions = [
299346 [ reload , navBack , navForward , changeViewport ]
300347] ;
301348
349+ var screenshotIndex = 0 ;
350+
302351function randomEvent ( ) {
303352 let typeIndex = getRandomInt ( 0 , pending_events . length ) ;
304353 if ( pending_events [ typeIndex ] > 0 ) {
354+ screenshotIndex += 1 ;
355+ //cy.screenshot('smart/'+screenshotIndex+"-"+ getEvtType(typeIndex)+"-before")
305356 let fIndex = getRandomInt ( 0 , functions [ typeIndex ] . length - 1 ) ;
306357 functions [ typeIndex ] [ fIndex ] ( ) ;
307358 pending_events [ typeIndex ] -- ;
308359 cy . wait ( delay ) ;
360+ //cy.screenshot('smart/'+screenshotIndex+"-"+ getEvtType(typeIndex)+"-after")
309361 }
310362 else {
311363 functions . splice ( typeIndex , 1 ) ;
@@ -338,7 +390,6 @@ describe( `${appName} under monkeys`, function() {
338390} )
339391
340392
341-
342393//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
343394//End of random monkey
344395//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 commit comments