Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Cypress e2e testing with a firebase auth project?

Tags:

I am exploring Cypress for e2e testing, looks like great software. The problem is Authentication, the Cypress documentation explains why using the UI is very bad here.

So I tried looking at the network tap of my application, to see if I could create a POST request to the firebase API, and authenticate without using the GUI. But I can see that there at least 2 request fired, and token saved to application storage.

So what approach should I use?

  1. Authenticate with the UI of my application, and instruct Cypress not to touch the local storage
  2. Keep experimenting with a way of sending the correct POST requests, and save the values to local storage.
  3. Make Cypress run custom JS code, and then use the Firebase SDK to login.

I am really looking for some advice here :)

like image 807
DauleDK Avatar asked Feb 11 '18 19:02

DauleDK


1 Answers

When doing this myself I made custom commands (like cy.login for auth then cy.callRtdb and cy.callFirestore for verifying data). After getting tired of repeating the logic it took to build them, I wrapped it up into a library called cypress-firebase. It includes custom commands and a cli to generate a custom auth token.

Setup mostly just consists of adding the custom commands in cypress/support/commands.js:

import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/database';
import 'firebase/firestore';
import { attachCustomCommands } from 'cypress-firebase';

const fbConfig = {
    // Your config from Firebase Console
};

window.fbInstance = firebase.initializeApp(fbConfig);

attachCustomCommands({ Cypress, cy, firebase })

And adding the plugin to cypress/plugins/index.js:

const cypressFirebasePlugin = require('cypress-firebase').plugin

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config

  // Return extended config (with settings from .firebaserc)
  return cypressFirebasePlugin(config)
}

But there full details on setup are available in the setup docs.

Disclosure, I am the author of cypress-firebase, which is the whole answer.

like image 53
Scott Avatar answered Sep 18 '22 17:09

Scott