add custom command for reusable login via API

This commit is contained in:
mahula 2022-09-15 09:14:29 +02:00
parent 1418d2e8fb
commit 9890898191
4 changed files with 43 additions and 3 deletions

View File

@ -31,13 +31,16 @@ export default defineConfig({
excludeSpecPattern: "*.js",
baseUrl: 'http://localhost:3000',
chromeWebSecurity: false,
supportFile: false,
supportFile: 'cypress/support/index.ts',
viewportHeight: 720,
viewportWidth: 1280,
retries: {
runMode: 2,
openMode: 0
},
env: {
backendURL: 'http://localhost:4000',
},
setupNodeEvents
}
})

View File

@ -0,0 +1,26 @@
Cypress.Commands.add('login', (email, password) => {
Cypress.LocalStorage.clear
cy.request({
method: 'POST',
url: Cypress.env('backendURL'),
body: {
"operationName": null,
"variables": {
"email": email,
"password": password
},
"query": "query ($email: String!, $password: String!, $publisherId: Int) {\n login(email: $email, password: $password, publisherId: $publisherId) {\n email\n firstName\n lastName\n language\n klickTipp {\n newsletterState\n __typename\n }\n hasElopage\n publisherId\n isAdmin\n creation\n __typename\n }\n}\n"
}
})
.then((response) => {
let vuexToken = {
token : JSON.parse(JSON.stringify(response.headers))['token'],
// TODO: how to compute the token time from the token im response?
tokenTime : 1663224487
};
cy.visit('/');
window.localStorage.setItem('vuex', JSON.stringify(vuexToken));
})
})

View File

@ -0,0 +1,11 @@
/// <reference types="cypress" />
import './e2e';
declare global {
namespace Cypress {
interface Chainable<Subject> {
login(email: string, password: string): Chainable<any>
}
}
}

View File

@ -3,8 +3,8 @@
"target": "es2016",
"lib": ["es6", "dom"],
"baseUrl": "../node_modules",
"types": ["cypress", "node"],
"strict": true
"types": ["cypress", "node"],
"strict": true
},
"include": ["**/*.ts"]
}