mozilla / blurts-server

Mozilla Monitor arms you with tools to keep your personal information safe. Find out what hackers already know about you and learn how to stay a step ahead of them.
https://monitor.mozilla.org
Mozilla Public License 2.0
731 stars 205 forks source link

Add some Puppeteer or Selenium tests? #767

Closed pdehaan closed 8 months ago

pdehaan commented 5 years ago

Not sure if there's an easy/good way to add a couple of E2E tests that run on each PR so we know that the server will start up and render at least the homepage.

The biggest problem I see is probably how to manage the server env vars and secrets.

pdehaan commented 5 years ago

I quickly hacked this together, but while the Heroku site was down, it was silently failing with an exit code of 0 (oddly while fetching the /__version__ path):

const assert = require("assert");
const path = require("path");

const axios = require("axios");
const puppeteer = require("puppeteer");

const envMap = new Map();
envMap.set("prod", "https://monitor.firefox.com");
envMap.set("stage", "https://blurts-server.stage.mozaws.net");
envMap.set("dev", "https://fx-breach-alerts.herokuapp.com");

const env = process.env.SERVER_ENV || "dev";

main(env);

async function main(env="dev") {
  const serverUrl = envMap.get(env) || envMap.get("dev");
  try {
    const version = await versionJson(serverUrl);
    const filename = `${env}-home-${version.commit}.png`;
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(serverUrl);
    await page.waitForSelector("h2[data-location='landing-page']");
    await page.screenshot({path: path.join(__dirname, "screenshots", filename)});
    console.log(`Writing ${filename}`);

    const landingHeadline = await page.$eval("h2[data-location='landing-page']", el => el.textContent.trim());
    assert.equal(landingHeadline, "Your right to be safe from hackers starts here.");

    await browser.close();
  } catch (err) {
    console.error(err.message);
    process.exit(1);
  }
}

async function versionJson(serverUrl) {
  const versionUrl = `${serverUrl}/__version__`;
  try {
    const res = await axios.get(versionUrl);
    return res.data;
  } catch (err) {
    throw err;
  }
}
EMMLynch commented 8 months ago

We have implemented E2E tests. Closing.