diff --git a/README.md b/README.md index 9899b8d..5ee94e8 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/cypress.json b/cypress.json deleted file mode 100644 index 0967ef4..0000000 --- a/cypress.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/cypress/integration/monkey/monkey.js b/cypress/integration/monkey/monkey.js index 0a125b3..d4954e4 100644 --- a/cypress/integration/monkey/monkey.js +++ b/cypress/integration/monkey/monkey.js @@ -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(){ @@ -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': `

Uncaught exception: ${err}

`}); + }); + cy.on('window:alert', (text)=>{ + cy.task('genericLog', {'message':`An alert was fired with the message: "${text}"`}); + cy.task('genericReport', {'html': `

An alert was fired with the message: ${text}

`}); + }); + cy.on('fail', (err)=>{ + cy.task('genericLog', {'message':`The test failed with the following error: ${err}`}); + cy.task('genericReport', {'html': `

Test failed with the error: ${err}

`}); + return false; + }); it(`visits ${appName} and survives monkeys`, function() { if(!seed) seed = getRandomInt(0, Number.MAX_SAFE_INTEGER); diff --git a/cypress/integration/monkey/smart-monkey.js b/cypress/integration/monkey/smart-monkey.js index 3930522..c651b75 100644 --- a/cypress/integration/monkey/smart-monkey.js +++ b/cypress/integration/monkey/smart-monkey.js @@ -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(){ @@ -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': `

Uncaught exception: ${err}

`}); + }); + cy.on('window:alert', (text)=>{ + cy.task('genericLog', {'message':`An alert was fired with the message: "${text}"`}) + cy.task('genericReport', {'html': `

An alert was fired with the message: ${text}

`}); + }); + cy.on('fail', (err)=>{ + cy.task('genericLog', {'message':`The test failed with the following error: ${err}`}); + cy.task('genericReport', {'html': `

Test failed with the error: ${err}

`}); + return false; + }); it(`visits ${appName} and survives smarter monkeys`, function() { if(!seed) seed = getRandomInt(0, Number.MAX_SAFE_INTEGER); diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js index 21b7b8e..08b6f6e 100644 --- a/cypress/plugins/index.js +++ b/cypress/plugins/index.js @@ -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, `

Browser event (source: ${event.source}): ${event.text}

`, (err) => { + if (err) throw err + console.log(`Finished logging`) + }) + } + else if (type === 'console'){ + fs.appendFile(LOG_FILENAME, `

Console ${event.type} event. Trace: ${(!!event.stackTrace)?event.stackTrace.description:"none"}

`, (err) => { + if (err) throw err + console.log(`Finished logging`) + }) + } + return true; + }); } diff --git a/monkey-config.json b/monkey-config.json index 6f86d72..60427a6 100644 --- a/monkey-config.json +++ b/monkey-config.json @@ -1,5 +1,3 @@ - - { "projectId":"TSDL-Monkey-with-cypress", "baseUrl":"https://caev03.github.io", @@ -17,6 +15,7 @@ }, "integrationFolder": "./cypress/integration/monkey", "pluginsFile": "./cypress/plugins/index.js", + "pageLoadTimeout":120000, "testFiles": "monkey.js", "videosFolder":"./results" } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 060c098..c34c466 100644 --- a/package-lock.json +++ b/package-lock.json @@ -130,6 +130,12 @@ "resolved": "https://registry.npmjs.org/async/-/async-3.2.0.tgz", "integrity": "sha512-TR2mEZFVOj2pLStYxLht7TyfuRzaydfpxr3k9RpHIzMgw7A64dzsdqCxH1WJyQdoe8T10nDXd9wnEigmiuHIZw==" }, + "async-limiter": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz", + "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==", + "dev": true + }, "asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -225,6 +231,24 @@ "resolved": "https://registry.npmjs.org/check-more-types/-/check-more-types-2.24.0.tgz", "integrity": "sha1-FCD/sQ/URNz8ebQ4kbv//TKoRgA=" }, + "chrome-remote-interface": { + "version": "0.27.2", + "resolved": "https://registry.npmjs.org/chrome-remote-interface/-/chrome-remote-interface-0.27.2.tgz", + "integrity": "sha512-pVLljQ29SAx8KIv5tSa9sIf8GrEsAZdPJoeWOmY3/nrIzFmE+EryNNHvDkddGod0cmAFTv+GmPG0uvzxi2NWsA==", + "dev": true, + "requires": { + "commander": "2.11.x", + "ws": "^6.1.0" + }, + "dependencies": { + "commander": { + "version": "2.11.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz", + "integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==", + "dev": true + } + } + }, "ci-info": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-2.0.0.tgz", @@ -401,6 +425,16 @@ "yauzl": "2.10.0" } }, + "cypress-log-to-output": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/cypress-log-to-output/-/cypress-log-to-output-1.0.8.tgz", + "integrity": "sha512-o0PwNSXSZho2QLTOa4I/KgyPfZwgqNBqNcz+jBMcKJHPsRBZDUElaosigqSXI28uuSlprUlvcYjpcb/791u/lg==", + "dev": true, + "requires": { + "chalk": "^2.4.2", + "chrome-remote-interface": "^0.27.1" + } + }, "cypress-plugin-tab": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/cypress-plugin-tab/-/cypress-plugin-tab-1.0.5.tgz", @@ -1508,6 +1542,15 @@ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, + "ws": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz", + "integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==", + "dev": true, + "requires": { + "async-limiter": "~1.0.0" + } + }, "yauzl": { "version": "2.10.0", "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", diff --git a/package.json b/package.json index 9d923ac..af5f113 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/smart-monkey-config.json b/smart-monkey-config.json index 02d74ed..9f62478 100644 --- a/smart-monkey-config.json +++ b/smart-monkey-config.json @@ -17,6 +17,7 @@ }, "integrationFolder": "./cypress/integration/monkey", "pluginsFile": "./cypress/plugins/index.js", + "pageLoadTimeout":120000, "testFiles": "smart-monkey.js", "videosFolder":"./results" } \ No newline at end of file