Skip to content
Snippets Groups Projects
Commit a682ea81 authored by hollum's avatar hollum
Browse files

create exercise - automated test using cypress

parent 1610dd9f
No related branches found
No related tags found
No related merge requests found
Pipeline #160104 passed
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
/// <reference types="cypress" />
// ***********************************************************
// 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)
/**
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
// ***********************************************
// 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 will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
// ***********************************************************
// 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'
var coachUser = {
username: null,
password: null,
email: 'coach@example.com',
phone_number: '12345678',
street_address: 'Roadstreet 1',
city: 'Citytown',
country: 'Countryland',
}
var athleteUser = {
username: null,
password: null,
email: 'athlete@example.com',
phone_number: '98765432',
street_address: 'Streetroad 2',
city: 'Towncity',
country: 'Landcountry',
}
var apiUrl = "localhost:8000"
var baseUrl = "localhost:9090"
function getRandomString(len=40) {
const dec2hex = dec => dec.toString(16).padStart(2, "0")
var arr = new Uint8Array(len / 2)
window.crypto.getRandomValues(arr)
return Array.from(arr, dec2hex).join('')
}
function generateUser() {
return {
username: `test_user_${getRandomString(10)}`,
password: getRandomString(10),
}
}
function registerUser(user) {
cy.visit(`${baseUrl}/index.html`)
cy.contains('Welcome to SecFit!')
cy.get('#btn-register').click()
cy.url().should('include', 'register.html')
cy.get('input[name="username"]').type(user.username)
cy.get('input[name="password"]').type(user.password)
cy.get('input[name="password1"]').type(user.password)
cy.get('#btn-create-account').click()
cy.url().should('include', 'workouts.html')
logout()
}
function login(user) {
cy.visit(`${baseUrl}/index.html`)
cy.contains('Welcome to SecFit!')
cy.get('#btn-login-nav').click()
cy.url().should('include', 'login.html')
cy.get('input[name="username"]').type(user.username)
cy.get('input[name="password"]').type(user.password)
cy.intercept({method: 'GET', url: '/workouts.html' }).as('loginRequest')
cy.get('#btn-login').click()
cy.wait('@loginRequest')
}
function logout() {
cy.get('#btn-logout', { timeout: 10000 }).click()
cy.url().should('include', 'index.html')
}
before(() => {
coachUser = {
...coachUser,
...generateUser()
}
athleteUser = {
...athleteUser,
...generateUser()
}
registerUser(coachUser)
registerUser(athleteUser)
cy.visit(`${baseUrl}/index.html`)
})
beforeEach(() => {
cy.wrap(coachUser).as('coachUser')
cy.wrap(athleteUser).as('athleteUser')
cy.wrap(login).as('login')
})
/*
import './commands'
import './commands'
var coachUser = {
username: "coachUser",
password: "123",
email: 'coach@example.com',
phone_number: '12345678',
street_address: 'Roadstreet 1',
city: 'Citytown',
country: 'Countryland',
main_gym: "SiT Moholt",
favourite_exercise: "Bench press"
}
var athleteUser = {
username: "athleteUser",
password: "123",
email: 'athlete@example.com',
phone_number: '98765432',
street_address: 'Streetroad 2',
city: 'Towncity',
country: 'Landcountry',
main_gym: "SiT Moholt",
favourite_exercise: "Bench press"
}
var baseUrl = "localhost:9090"
function registerUser(user) {
cy.visit(`${baseUrl}/index.html`)
cy.contains('Welcome to SecFit!')
cy.get('#btn-register').click()
cy.url().should('include', 'register.html')
cy.get('input[name="username"]').type(user.username)
cy.get('input[name="email"]').type(user.email)
cy.get('input[name="password"]').type(user.password)
cy.get('input[name="password1"]').type(user.password)
cy.get('input[name="phone_number"]').type(user.phone_number)
cy.get('input[name="country"]').type(user.country)
cy.get('input[name="city"]').type(user.city)
cy.get('input[name="street_address"]').type(user.street_address)
cy.get('input[name="main_gym"]').type(user.main_gym)
cy.get('input[name="favourite_exercise"]').type(user.favourite_exercise)
cy.get('#btn-create-account').click()
cy.url().should('include', 'workouts.html')
// cy.log("Waiting 1.5 seconds to allow request to go through. Should probably be replaced with a register-watcher, but hey watcha gonna doooo")
// cy.wait(1500)
logout()
}
function login(user) {
cy.visit(`${baseUrl}/index.html`)
cy.contains('Welcome to SecFit!')
cy.get('#btn-login-nav').click()
cy.url().should('include', 'login.html')
cy.get('input[name="username"]').type(user.username)
cy.get('input[name="password"]').type(user.password)
cy.intercept({method: 'GET', url: '/workouts.html' }).as('loginRequest')
cy.get('#btn-login').click()
cy.wait('@loginRequest')
}
function logout() {
cy.get('#btn-logout', { timeout: 10000 }).click()
cy.url().should('include', 'index.html')
}
before(() => {
registerUser(coachUser)
registerUser(athleteUser)
cy.visit(`${baseUrl}/index.html`)
})
beforeEach(() => {
cy.wrap(coachUser).as('coachUser')
cy.wrap(athleteUser).as('athleteUser')
cy.wrap(login).as('login')
})
*/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment