From 46f9feda5693966d5318f7c018bd5d7f9d082bd4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Frans=20Pondaco=20Winandy?=
 <jvfpw18@inf.ufpr.br>
Date: Fri, 28 Jun 2019 11:13:38 -0300
Subject: [PATCH] Add tests for simcaq front using cypress

---
 .gitignore                    |   4 +
 cypress.json                  |   4 +
 cypress/fixtures/example.json |   5 +
 cypress/plugins/index.js      |  17 ++++
 cypress/support/commands.js   |  25 +++++
 cypress/support/index.js      |  20 ++++
 package.json                  |   9 ++
 tests/simcaq-front.js         | 173 ++++++++++++++++++++++++++++++++++
 8 files changed, 257 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 cypress.json
 create mode 100644 cypress/fixtures/example.json
 create mode 100644 cypress/plugins/index.js
 create mode 100644 cypress/support/commands.js
 create mode 100644 cypress/support/index.js
 create mode 100644 package.json
 create mode 100644 tests/simcaq-front.js

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b15abc1
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+node_modules/
+cypress/videos/
+cypress/screenshots/
+package-lock.json
\ No newline at end of file
diff --git a/cypress.json b/cypress.json
new file mode 100644
index 0000000..46d8424
--- /dev/null
+++ b/cypress.json
@@ -0,0 +1,4 @@
+{
+    "baseUrl": "http://simcaq.c3sl.ufpr.br/",
+    "integrationFolder": "./tests"
+}
diff --git a/cypress/fixtures/example.json b/cypress/fixtures/example.json
new file mode 100644
index 0000000..da18d93
--- /dev/null
+++ b/cypress/fixtures/example.json
@@ -0,0 +1,5 @@
+{
+  "name": "Using fixtures to represent data",
+  "email": "hello@cypress.io",
+  "body": "Fixtures are a great way to mock data for responses to routes"
+}
\ No newline at end of file
diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js
new file mode 100644
index 0000000..fd170fb
--- /dev/null
+++ b/cypress/plugins/index.js
@@ -0,0 +1,17 @@
+// ***********************************************************
+// This example plugins/index.js can be used to load plugins
+//
+// You can change the location of this file or turn off loading
+// the plugins file with the 'pluginsFile' configuration option.
+//
+// You can read more here:
+// https://on.cypress.io/plugins-guide
+// ***********************************************************
+
+// This function is called when a project is opened or re-opened (e.g. due to
+// the project's config changing)
+
+module.exports = (on, config) => {
+  // `on` is used to hook into various events Cypress emits
+  // `config` is the resolved Cypress config
+}
diff --git a/cypress/support/commands.js b/cypress/support/commands.js
new file mode 100644
index 0000000..c1f5a77
--- /dev/null
+++ b/cypress/support/commands.js
@@ -0,0 +1,25 @@
+// ***********************************************
+// This example commands.js shows you how to
+// create various custom commands and overwrite
+// existing commands.
+//
+// For more comprehensive examples of custom
+// commands please read more here:
+// https://on.cypress.io/custom-commands
+// ***********************************************
+//
+//
+// -- This is a parent command --
+// Cypress.Commands.add("login", (email, password) => { ... })
+//
+//
+// -- This is a child command --
+// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
+//
+//
+// -- This is a dual command --
+// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
+//
+//
+// -- This is will overwrite an existing command --
+// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
diff --git a/cypress/support/index.js b/cypress/support/index.js
new file mode 100644
index 0000000..d68db96
--- /dev/null
+++ b/cypress/support/index.js
@@ -0,0 +1,20 @@
+// ***********************************************************
+// This example support/index.js is processed and
+// loaded automatically before your test files.
+//
+// This is a great place to put global configuration and
+// behavior that modifies Cypress.
+//
+// You can change the location of this file or turn off
+// automatically serving support files with the
+// 'supportFile' configuration option.
+//
+// You can read more here:
+// https://on.cypress.io/configuration
+// ***********************************************************
+
+// Import commands.js using ES2015 syntax:
+import './commands'
+
+// Alternatively you can use CommonJS syntax:
+// require('./commands')
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..69c2395
--- /dev/null
+++ b/package.json
@@ -0,0 +1,9 @@
+{
+	"scripts": {
+		"cypress": "cypress open",
+		"cy:run": "cypress run"
+	},
+	"devDependencies": {
+		"cypress": "^3.3.2"
+	}
+}
diff --git a/tests/simcaq-front.js b/tests/simcaq-front.js
new file mode 100644
index 0000000..fc92e25
--- /dev/null
+++ b/tests/simcaq-front.js
@@ -0,0 +1,173 @@
+const TIMEOUT = {timeout: 130000};
+
+describe('1 - CAQ', function() {
+    it('successfully loads', function() {
+        cy.clearLocalStorage()
+        cy.clearCookies()
+
+        cy.server()
+        cy.route({url: 'api/v1/enrollment**', method: 'GET'}).as('getEnrollment')
+        cy.route({url: 'api/v1/classroom_count**', method: 'POST'}).as('postApi')
+        cy.visit('http://simcaq.c3sl.ufpr.br/', {
+            auth: {
+                username: 'simcaq',
+                password: 'simcaqeducacao'
+            }
+        })
+        cy.wait(1000)
+
+        cy.get('.caq').contains('Acessar').click()
+
+        cy.get('.block-ui-main').should('visible')
+        cy.wait('@postApi', TIMEOUT)
+        cy.wait(Array(5).fill('@getEnrollment'), TIMEOUT)
+        cy.wait(300)
+
+        cy.route({url: 'api/v1/**', method: 'GET'}).as('getApi')
+        cy.get('.edit-mode').click()
+        
+        let apiGetCalls = [1, 1, 0, 1, 0, 3, 1, 1, 0, 4, 1, 0, 4, 0, 1, 0, 0, 7, 3, 0, 0, 0, 0, 0]      // Quantidade de get's da api por tela
+        for (let i = 0; i < 24; i++) {
+            cy.wait(300)
+            if (i == 9 || i == 18)      // Telas que contêm um post.
+                cy.wait('@postApi', TIMEOUT)
+            if (apiGetCalls[i] > 0)
+                cy.wait(Array(apiGetCalls[i]).fill('@getApi'), TIMEOUT)
+
+            cy.contains('Próximo').click()
+        }
+        cy.wait(Array(3).fill('@getApi'), TIMEOUT)
+        cy.get('.block-ui-main', TIMEOUT).should('not.visible').then(() => {return cy.contains('Finalizar').click()} )
+    })
+})
+
+describe('2 - Orçamento educacional', function() {
+    it('successfully loads without selecting a state', function() {
+        cy.clearLocalStorage()
+        cy.clearCookies()
+
+        cy.server()
+        cy.route({url: 'api/v1/enrollment**', method: 'GET'}).as('getEnrollment')
+        cy.route({url: 'api/v1/classroom_count**', method: 'POST'}).as('postApi')
+        cy.visit('http://simcaq.c3sl.ufpr.br/', {
+            auth: {
+                username: 'simcaq',
+                password: 'simcaqeducacao'
+            }
+        })
+        cy.wait(1000)
+
+        cy.get('.planning').contains('Simular').click()
+        cy.wait('@getEnrollment', TIMEOUT)
+        cy.wait(300)
+
+        cy.contains('Simular').click()
+        cy.wait(['@postApi', '@postApi'], TIMEOUT)     // Classroom count chamada duas vezes seguidas
+        cy.wait(Array(5).fill('@getEnrollment'), TIMEOUT)
+        cy.wait(300)
+
+        cy.get('.edit-mode').click()
+        cy.route({url: 'api/v1/**', method: 'GET'}).as('getApi')
+        cy.wait(300)
+
+        cy.contains('4').click()
+        let apiGetCalls = [0, 1, 2, 0, 1, 0, 3, 1, 1, 0, 4, 1, 0, 4, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0]      // Quantidade de get's da api por tela
+        for (let i = 0; i < 25; i++) {
+            cy.wait(300)
+            if (i == 10 || i == 19)      // Telas que contêm um post.
+                cy.wait('@postApi', TIMEOUT)
+            if (apiGetCalls[i] > 0)
+                cy.wait(Array(apiGetCalls[i]).fill('@getApi'), TIMEOUT)
+
+            cy.contains('Próximo').click()
+        }
+        cy.wait(Array(3).fill('@getApi'), TIMEOUT)
+        cy.get('.block-ui-main', TIMEOUT).should('not.visible').then(() => {return cy.contains('Finalizar').click()} )
+    })
+
+    it('successfully loads with RS selected', function() {
+        cy.clearLocalStorage()
+        cy.clearCookies()
+
+        cy.server()
+        cy.route({url: 'api/v1/enrollment**', method: 'GET'}).as('getEnrollment')
+        cy.route({url: 'api/v1/classroom_count**', method: 'POST'}).as('postApi')
+        cy.visit('http://simcaq.c3sl.ufpr.br/', {
+            auth: {
+                username: 'simcaq',
+                password: 'simcaqeducacao'
+            }
+        })
+        cy.wait(1000)
+
+        cy.get('.planning').contains('Simular').click()
+        cy.wait('@getEnrollment', TIMEOUT)
+        cy.wait(300)
+
+        cy.get('.state-rs').click({force: true})
+        cy.wait('@getEnrollment', TIMEOUT)
+        cy.wait(300)
+
+        cy.contains('Simular').click()
+        cy.wait(['@postApi', '@postApi'], TIMEOUT)     // Classroom count chamada duas vezes seguidas
+        cy.wait(Array(5).fill('@getEnrollment'), TIMEOUT) 
+        cy.wait(300)
+
+        cy.get('.edit-mode').click()
+        cy.route({url: 'api/v1/**', method: 'GET'}).as('getApi')
+        cy.wait(300)
+
+        cy.contains('4').click()
+        let apiGetCalls = [0, 1, 2, 0, 1, 0, 3, 1, 1, 0, 4, 1, 0, 4, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0]      // Quantidade de get's da api por tela
+        for (let i = 0; i < 25; i++) {
+            cy.wait(300)
+            if (i == 10 || i == 19)      // Telas que contêm um post.
+                cy.wait('@postApi', TIMEOUT)
+            if (apiGetCalls[i] > 0)
+                cy.wait(Array(apiGetCalls[i]).fill('@getApi'), TIMEOUT)
+
+            cy.contains('Próximo').click()
+        }
+        cy.wait(Array(3).fill('@getApi'), TIMEOUT)
+        cy.get('.block-ui-main', TIMEOUT).should('not.visible').then(() => {return cy.contains('Finalizar').click()} )
+    })
+})
+
+describe('3 - Fundo de manutenção', function() {
+    it('successfully loads', function() {
+        cy.clearLocalStorage()
+        cy.clearCookies()
+
+        cy.server()
+        cy.route({url: 'api/v1/enrollment**', method: 'GET'}).as('getEnrollment')
+        cy.route({url: 'api/v1/classroom_count**', method: 'POST'}).as('postApi')
+        cy.visit('http://simcaq.c3sl.ufpr.br/', {
+            auth: {
+                username: 'simcaq',
+                password: 'simcaqeducacao'
+            }
+        })
+        cy.wait(1000)
+
+        cy.get('.financing').contains('Consultar').click()
+        cy.wait('@postApi', TIMEOUT)
+        cy.wait(Array(5).fill('@getEnrollment'), TIMEOUT)
+        cy.wait(10000)
+
+        cy.route({url: 'api/v1/**', method: 'GET'}).as('getApi')
+        cy.get('.edit-mode').click()
+
+        let apiGetCalls = [1, 1, 0, 1, 0, 3, 1, 1, 0, 4, 1, 0, 4, 0, 1, 0, 0, 7, 3, 0, 0, 0, 0, 0]      // Quantidade de get's da api por tela
+        for (let i = 0; i < 24; i++) {
+            cy.wait(300)
+            if (i == 9 || i == 18)      // Telas que contêm um post.
+                cy.wait('@postApi', TIMEOUT)
+            if (apiGetCalls[i] > 0)
+                cy.wait(Array(apiGetCalls[i]).fill('@getApi'), TIMEOUT)
+
+            cy.contains('Próximo').click()
+        }
+        cy.wait(Array(3).fill('@getApi'), TIMEOUT)
+        cy.get('.block-ui-main', TIMEOUT).should('not.visible').then(() => {return cy.contains('Finalizar').click()} )
+    })
+})
\ No newline at end of file
-- 
GitLab