Skip to content

Commit 672b93a

Browse files
committed
First version to fix #9
1 parent 6192d8a commit 672b93a

File tree

9 files changed

+108
-7
lines changed

9 files changed

+108
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This repository contains the code for a random tester developed using [Cypress](
44
## How to run
55
In order to use the tester, you will have to follow these steps:
66
- Get the source code from this repository: Click on Download as Zip and unzip the folder in your machine or clone the repo
7-
- Install the required modules: Using [Node Package Manager](https://www.npmjs.com/), run `npm install` on the root folder; this will install the cypress CLI module and other dependencies, which are the [faker](https://www.npmjs.com/package/faker) module and a cypress [plugin](https://github.com/Bkucera/cypress-plugin-tab) for pressing the tab key. In case you already have cypress installed, it is better to avoid installing it again in this folder; for this, run the commands `npm install faker` and `npm install -D cypress-plugin-tab` individually.
7+
- Install the required modules: Using [Node Package Manager](https://www.npmjs.com/), run `npm install` on the root folder; this will install the cypress CLI module and other dependencies, which are the [faker](https://www.npmjs.com/package/faker) module and a cypress [plugin](https://github.com/Bkucera/cypress-plugin-tab) for pressing the tab key, along with [another plugin](https://github.com/flotwig/cypress-log-to-output) for capturing the browser console output. In case you already have cypress installed, it is better to avoid installing it again in this folder; for this, run the commands `npm install faker`, `npm install -D cypress-log-to-output` and `npm install -D cypress-plugin-tab` individually.
88
- Configure the desired parameters: The repository's root folder contains two JSON files which have the configuration parameters for each test. Open them and edit the parameters as needed. You can change the baseURL, the seed for the test, the percentage of events, the delay between events, and the number of events.
99
- Run the desired tester: The commands for running the tests must be executed from the root folder, so do not forget to change de directory again with the `cd` command. For the random tester, run `cypress run --config-file ./monkey-config.json`. For the slightly smarter random tester, run `cypress run --config-file ./smart-monkey-config.json`.
1010

cypress.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

cypress/integration/monkey/monkey.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ function changeViewport(){
434434
curPageMaxY = Math.max( d.body.scrollHeight, d.body.offsetHeight, d.documentElement.clientHeight, d.documentElement.scrollHeight, d.documentElement.offsetHeight) - win.innerHeight
435435
curPageMaxX = Math.max( d.body.scrollWidth, d.body.offsetWidth, d.documentElement.clientWidth, d.documentElement.scrollWidth, d.documentElement.offsetWidth) - win.innerWidth
436436
})
437-
cy.task("logCommand", { funtype: "Viewport change", info: `Changed the viewport to ${viewports[index]} with ${orientations[index]} orientation`})
437+
cy.task("logCommand", { funtype: "Viewport change", info: `Changed the viewport to ${viewports[index]} with ${orientations[oindex]} orientation`})
438438
}
439439

440440
function navBack(){
@@ -510,6 +510,20 @@ function randomEvent(){
510510
var pending_events = [,,,,,]
511511

512512
describe( `${appName} under monkeys`, function() {
513+
//Listener
514+
cy.on('uncaught:exception', (err)=>{
515+
cy.task('genericLog', {'message':`An exception occurred: ${err}`});
516+
cy.task('genericReport', {'html': `<p><strong>Uncaught exception: </strong>${err}</p>`});
517+
});
518+
cy.on('window:alert', (text)=>{
519+
cy.task('genericLog', {'message':`An alert was fired with the message: "${text}"`});
520+
cy.task('genericReport', {'html': `<p><strong>An alert was fired with the message: </strong>${text}</p>`});
521+
});
522+
cy.on('fail', (err)=>{
523+
cy.task('genericLog', {'message':`The test failed with the following error: ${err}`});
524+
cy.task('genericReport', {'html': `<p><strong>Test failed with the error: </strong>${err}</p>`});
525+
return false;
526+
});
513527
it(`visits ${appName} and survives monkeys`, function() {
514528
if(!seed) seed = getRandomInt(0, Number.MAX_SAFE_INTEGER);
515529

cypress/integration/monkey/smart-monkey.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ function changeViewport(){
446446
curPageMaxY = Math.max( d.body.scrollHeight, d.body.offsetHeight, d.documentElement.clientHeight, d.documentElement.scrollHeight, d.documentElement.offsetHeight) - win.innerHeight
447447
curPageMaxX = Math.max( d.body.scrollWidth, d.body.offsetWidth, d.documentElement.clientWidth, d.documentElement.scrollWidth, d.documentElement.offsetWidth) - win.innerWidth
448448
})
449-
cy.task("logCommand", { funtype: "Viewport change", info: `Changed the viewport to ${viewports[index]} with ${orientations[index]} orientation`})
449+
cy.task("logCommand", { funtype: "Viewport change", info: `Changed the viewport to ${viewports[index]} with ${orientations[oindex]} orientation`})
450450
}
451451

452452
function navBack(){
@@ -638,6 +638,20 @@ const functions = [
638638
];
639639

640640
describe( `${appName} under smarter monkeys`, function() {
641+
//Listeners
642+
cy.on('uncaught:exception', (err)=>{
643+
cy.task('genericLog', {'message':`An exception occurred: ${err}`})
644+
cy.task('genericReport', {'html': `<p><strong>Uncaught exception: </strong>${err}</p>`});
645+
});
646+
cy.on('window:alert', (text)=>{
647+
cy.task('genericLog', {'message':`An alert was fired with the message: "${text}"`})
648+
cy.task('genericReport', {'html': `<p><strong>An alert was fired with the message: </strong>${text}</p>`});
649+
});
650+
cy.on('fail', (err)=>{
651+
cy.task('genericLog', {'message':`The test failed with the following error: ${err}`});
652+
cy.task('genericReport', {'html': `<p><strong>Test failed with the error: </strong>${err}</p>`});
653+
return false;
654+
});
641655
it(`visits ${appName} and survives smarter monkeys`, function() {
642656
if(!seed) seed = getRandomInt(0, Number.MAX_SAFE_INTEGER);
643657

cypress/plugins/index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,41 @@ module.exports = (on, config) => {
5656
if (err) throw err
5757
console.log(`Logged error`)
5858
})
59+
return null
5960
},
6061
genericLog({message}){
6162
console.log(message)
63+
return null
64+
},
65+
genericReport({html}){
66+
fs.appendFile(LOG_FILENAME, html, (err) => {
67+
if (err) throw err
68+
console.log(`Logged error`)
69+
})
70+
return null
6271
}
63-
})
72+
});
6473

74+
require('cypress-log-to-output').install(on, (type, event) => {
75+
// return true or false from this plugin to control if the event is logged
76+
// `type` is either `console` or `browser`
77+
// if `type` is `browser`, `event` is an object of the type `LogEntry`:
78+
// https://chromedevtools.github.io/devtools-protocol/tot/Log#type-LogEntry
79+
// if `type` is `console`, `event` is an object of the type passed to `Runtime.consoleAPICalled`:
80+
// https://chromedevtools.github.io/devtools-protocol/tot/Runtime#event-consoleAPICalled
81+
if(type === 'browser'){
82+
fs.appendFile(LOG_FILENAME, `<p><strong>Browser event (source: ${event.source}): </strong>${event.text}</p>`, (err) => {
83+
if (err) throw err
84+
console.log(`Finished logging`)
85+
})
86+
}
87+
else if (type === 'console'){
88+
fs.appendFile(LOG_FILENAME, `<p><strong>Console ${event.type} event. Trace: </strong>${(!!event.stackTrace)?event.stackTrace.description:"none"}</p>`, (err) => {
89+
if (err) throw err
90+
console.log(`Finished logging`)
91+
})
92+
}
93+
return true;
94+
});
6595
}
6696

monkey-config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
{
42
"projectId":"TSDL-Monkey-with-cypress",
53
"baseUrl":"https://caev03.github.io",
@@ -17,6 +15,7 @@
1715
},
1816
"integrationFolder": "./cypress/integration/monkey",
1917
"pluginsFile": "./cypress/plugins/index.js",
18+
"pageLoadTimeout":120000,
2019
"testFiles": "monkey.js",
2120
"videosFolder":"./results"
2221
}

package-lock.json

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"homepage": "https://github.com/TheSoftwareDesignLab/monkey-cypress#readme",
1919
"devDependencies": {
20+
"cypress-log-to-output": "^1.0.8",
2021
"cypress-plugin-tab": "^1.0.5"
2122
},
2223
"dependencies": {

smart-monkey-config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"integrationFolder": "./cypress/integration/monkey",
1919
"pluginsFile": "./cypress/plugins/index.js",
20+
"pageLoadTimeout":120000,
2021
"testFiles": "smart-monkey.js",
2122
"videosFolder":"./results"
2223
}

0 commit comments

Comments
 (0)