Skip to content

ManthanRPatel/Cypress-mysql-query-and-localstorage-jwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Cypress-mysql-query-and-localstorage-jwt

cypress mysql connection , mysql perform query ,, select query with examples,,,

available at issuse in link = cypress-io/cypress#3689 (comment)

  1. add env { } in cypress.json

2)... add function in plugin > index.js ===> const mysql = require('mysql');

on('task', { // deconstruct the individual properties queryTestDb({query,params}) { const connection = mysql.createConnection(config.env.db) connection.connect() return new Promise((resolve, reject) => { connection.query(query,[params], (error, results) => { if (error) reject(error) else { connection.end() return resolve(results) } }) }) } })

3) make file for testing like mydemoMysql.js in integration folder

make function in describe

==>
it('mysql connection',()=>{
    cy.log("data")
    cy.task('queryTestDb',{query:"SELECT * FROM `tbl_demo` WHERE username = ? ",params:"macmax"}).then(results => {
        expect(results).to.have.lengthOf(2)
      })
    cy.wait(3000);
})
  1. database info:: database name: manthan_demo table name : tbl_demo columns : demo_id,title,username,password

note::: performing query can take some times... so always put cy.wait(2000) in query performing lines.....

Cypress -localstorage-jwt

  1. first put code in cypress/support/commands.js ===>

    let LOCAL_STORAGE_MEMORY = {};

    Cypress.Commands.add("saveLocalStorageCache", () => { Object.keys(localStorage).forEach(key => { LOCAL_STORAGE_MEMORY[key] = localStorage[key]; }); });

    Cypress.Commands.add("restoreLocalStorageCache", () => { Object.keys(LOCAL_STORAGE_MEMORY).forEach(key => { localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]); }); });

    Cypress.Commands.add("clearLocalStorageCache", () => { localStorage.clear(); LOCAL_STORAGE_MEMORY = {}; });

  2. then call it on your file like ===>

    beforeEach(() => { cy.restoreLocalStorageCache(); });

    afterEach(() => { cy.saveLocalStorageCache(); });

    after(function() { cy.clearLocalStorageCache(); });

    conclusion : doing this will prvent cypress clearing localstorage and jwt token after your all tests...

for file upload - cypress-io/cypress#170

About

cypress mysql connection , mysql perform query ,, select query with examples,,,

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published