Skip to content

Commit

Permalink
extension(popup): integration test for popup (#5412)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet authored and patrickhulce committed Jun 6, 2018
1 parent 8f23052 commit ac264c0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lighthouse-extension/app/src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,30 +247,30 @@ async function initPopup() {
});

// bind throttling control button
const lanternCheckbox = /** @type {HTMLInputElement} */ (find('lantern-checkbox'));
const lanternCheckbox = /** @type {HTMLInputElement} */ (find('#lantern-checkbox'));
lanternCheckbox.addEventListener('change', async () => {
const settings = await background.loadSettings();
settings.useDevTools = !lanternCheckbox.checked;
background.saveSettings(settings);
});

// bind Generate Report button
const generateReportButton = find('generate-report');
const generateReportButton = find('#generate-report');
generateReportButton.addEventListener('click', () => {
background.loadSettings().then(settings => {
onGenerateReportButtonClick(background, settings);
});
});

// bind View Options button
const generateOptionsEl = find('configure-options');
const generateOptionsEl = find('#configure-options');
const optionsEl = find('.options');
generateOptionsEl.addEventListener('click', () => {
optionsEl.classList.add(subpageVisibleClass);
});

// bind Save Options button
const okButton = find('ok');
const okButton = find('#ok');
okButton.addEventListener('click', () => {
// Save settings when options page is closed.
const checkboxes = /** @type {NodeListOf<HTMLInputElement>} */
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"watch": "gulp watch",
"build": "gulp build:production",
"test": "mocha test/extension-test.js"
"test": "mocha test/**/*-test.js"
},
"devDependencies": {
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
Expand Down
95 changes: 95 additions & 0 deletions lighthouse-extension/test/popup-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* @license Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

/* eslint-env mocha */

const path = require('path');
const assert = require('assert');
const puppeteer = require('../../node_modules/puppeteer/index.js');

const lighthouseExtensionPath = path.resolve(__dirname, '../dist');

describe('Lighthouse chrome popup', function() {
// eslint-disable-next-line no-console
console.log('\n✨ Be sure to have recently run this: yarn build-extension');

let browser;
let page;
const pageErrors = [];

before(async function() {
// eslint-disable-next-line
this.timeout(90 * 1000);

// start puppeteer
browser = await puppeteer.launch({
headless: false,
executablePath: process.env.CHROME_PATH,
});

page = await browser.newPage();
await page.evaluateOnNewDocument(() => {
const backgroundMock = {
isRunning: () => false,
listenForStatus: () => {},
loadSettings: () => Promise.resolve({
selectedCategories: [],
useDevTools: false,
}),
getDefaultCategories: () => [],
};

Object.defineProperty(chrome, 'tabs', {
get: () => ({
query: (args, cb) => {
cb([{
url: 'http://example.com',
}]);
},
}),
});
Object.defineProperty(chrome, 'runtime', {
get: () => ({
getBackgroundPage: cb => {
cb(backgroundMock);
},
}),
});
});

page.on('pageerror', err => {
pageErrors.push(err);
});

await page.goto('file://' + path.join(lighthouseExtensionPath, 'popup.html'), {waitUntil: 'networkidle2'});
});

after(async () => {
if (browser) {
await browser.close();
}
});

it('should load without errors', async function() {
assert.equal(pageErrors.length, 0);
});

it('should load the popup with an url', async () => {
const titleText = await page.evaluate(() =>
document.querySelector('.header-titles__main').textContent);
const urlText = await page.evaluate(() =>
document.querySelector('.header-titles__url').textContent);

// check if the popup is showing the lighthouse page
const subPageIsVisible = await page.evaluate(() =>
document.querySelector('.status').classList.contains('subpage--visible'));

assert.ok(!subPageIsVisible, 'Popup is stuck on the splash screen');
assert.equal(titleText, 'Lighthouse');
assert.equal(urlText, 'http://example.com');
});
});

0 comments on commit ac264c0

Please sign in to comment.