Skip to content

Commit

Permalink
First version to fix #9
Browse files Browse the repository at this point in the history
  • Loading branch information
jespitiaa committed Apr 1, 2020
1 parent 6192d8a commit 672b93a
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This repository contains the code for a random tester developed using [Cypress](
## How to run
In order to use the tester, you will have to follow these steps:
- Get the source code from this repository: Click on Download as Zip and unzip the folder in your machine or clone the repo
- 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.
- 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.
- 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.
- 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`.

Expand Down
1 change: 0 additions & 1 deletion cypress.json

This file was deleted.

16 changes: 15 additions & 1 deletion cypress/integration/monkey/monkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ function changeViewport(){
curPageMaxY = Math.max( d.body.scrollHeight, d.body.offsetHeight, d.documentElement.clientHeight, d.documentElement.scrollHeight, d.documentElement.offsetHeight) - win.innerHeight
curPageMaxX = Math.max( d.body.scrollWidth, d.body.offsetWidth, d.documentElement.clientWidth, d.documentElement.scrollWidth, d.documentElement.offsetWidth) - win.innerWidth
})
cy.task("logCommand", { funtype: "Viewport change", info: `Changed the viewport to ${viewports[index]} with ${orientations[index]} orientation`})
cy.task("logCommand", { funtype: "Viewport change", info: `Changed the viewport to ${viewports[index]} with ${orientations[oindex]} orientation`})
}

function navBack(){
Expand Down Expand Up @@ -510,6 +510,20 @@ function randomEvent(){
var pending_events = [,,,,,]

describe( `${appName} under monkeys`, function() {
//Listener
cy.on('uncaught:exception', (err)=>{
cy.task('genericLog', {'message':`An exception occurred: ${err}`});
cy.task('genericReport', {'html': `<p><strong>Uncaught exception: </strong>${err}</p>`});
});
cy.on('window:alert', (text)=>{
cy.task('genericLog', {'message':`An alert was fired with the message: "${text}"`});
cy.task('genericReport', {'html': `<p><strong>An alert was fired with the message: </strong>${text}</p>`});
});
cy.on('fail', (err)=>{
cy.task('genericLog', {'message':`The test failed with the following error: ${err}`});
cy.task('genericReport', {'html': `<p><strong>Test failed with the error: </strong>${err}</p>`});
return false;
});
it(`visits ${appName} and survives monkeys`, function() {
if(!seed) seed = getRandomInt(0, Number.MAX_SAFE_INTEGER);

Expand Down
16 changes: 15 additions & 1 deletion cypress/integration/monkey/smart-monkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ function changeViewport(){
curPageMaxY = Math.max( d.body.scrollHeight, d.body.offsetHeight, d.documentElement.clientHeight, d.documentElement.scrollHeight, d.documentElement.offsetHeight) - win.innerHeight
curPageMaxX = Math.max( d.body.scrollWidth, d.body.offsetWidth, d.documentElement.clientWidth, d.documentElement.scrollWidth, d.documentElement.offsetWidth) - win.innerWidth
})
cy.task("logCommand", { funtype: "Viewport change", info: `Changed the viewport to ${viewports[index]} with ${orientations[index]} orientation`})
cy.task("logCommand", { funtype: "Viewport change", info: `Changed the viewport to ${viewports[index]} with ${orientations[oindex]} orientation`})
}

function navBack(){
Expand Down Expand Up @@ -638,6 +638,20 @@ const functions = [
];

describe( `${appName} under smarter monkeys`, function() {
//Listeners
cy.on('uncaught:exception', (err)=>{
cy.task('genericLog', {'message':`An exception occurred: ${err}`})
cy.task('genericReport', {'html': `<p><strong>Uncaught exception: </strong>${err}</p>`});
});
cy.on('window:alert', (text)=>{
cy.task('genericLog', {'message':`An alert was fired with the message: "${text}"`})
cy.task('genericReport', {'html': `<p><strong>An alert was fired with the message: </strong>${text}</p>`});
});
cy.on('fail', (err)=>{
cy.task('genericLog', {'message':`The test failed with the following error: ${err}`});
cy.task('genericReport', {'html': `<p><strong>Test failed with the error: </strong>${err}</p>`});
return false;
});
it(`visits ${appName} and survives smarter monkeys`, function() {
if(!seed) seed = getRandomInt(0, Number.MAX_SAFE_INTEGER);

Expand Down
32 changes: 31 additions & 1 deletion cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,41 @@ module.exports = (on, config) => {
if (err) throw err
console.log(`Logged error`)
})
return null
},
genericLog({message}){
console.log(message)
return null
},
genericReport({html}){
fs.appendFile(LOG_FILENAME, html, (err) => {
if (err) throw err
console.log(`Logged error`)
})
return null
}
})
});

require('cypress-log-to-output').install(on, (type, event) => {
// return true or false from this plugin to control if the event is logged
// `type` is either `console` or `browser`
// if `type` is `browser`, `event` is an object of the type `LogEntry`:
// https://chromedevtools.github.io/devtools-protocol/tot/Log#type-LogEntry
// if `type` is `console`, `event` is an object of the type passed to `Runtime.consoleAPICalled`:
// https://chromedevtools.github.io/devtools-protocol/tot/Runtime#event-consoleAPICalled
if(type === 'browser'){
fs.appendFile(LOG_FILENAME, `<p><strong>Browser event (source: ${event.source}): </strong>${event.text}</p>`, (err) => {
if (err) throw err
console.log(`Finished logging`)
})
}
else if (type === 'console'){
fs.appendFile(LOG_FILENAME, `<p><strong>Console ${event.type} event. Trace: </strong>${(!!event.stackTrace)?event.stackTrace.description:"none"}</p>`, (err) => {
if (err) throw err
console.log(`Finished logging`)
})
}
return true;
});
}

3 changes: 1 addition & 2 deletions monkey-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


{
"projectId":"TSDL-Monkey-with-cypress",
"baseUrl":"https://caev03.github.io",
Expand All @@ -17,6 +15,7 @@
},
"integrationFolder": "./cypress/integration/monkey",
"pluginsFile": "./cypress/plugins/index.js",
"pageLoadTimeout":120000,
"testFiles": "monkey.js",
"videosFolder":"./results"
}
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"homepage": "https://github.com/TheSoftwareDesignLab/monkey-cypress#readme",
"devDependencies": {
"cypress-log-to-output": "^1.0.8",
"cypress-plugin-tab": "^1.0.5"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions smart-monkey-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"integrationFolder": "./cypress/integration/monkey",
"pluginsFile": "./cypress/plugins/index.js",
"pageLoadTimeout":120000,
"testFiles": "smart-monkey.js",
"videosFolder":"./results"
}

0 comments on commit 672b93a

Please sign in to comment.