puppeteeer fails on extensions
gitea/salix/puppeteer This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2019-11-25 09:13:20 +01:00
parent 5040d1d4a8
commit 14dcc66efe
6 changed files with 169 additions and 45 deletions

View File

@ -1,6 +1,7 @@
/* eslint no-invalid-this: "off" */ /* eslint no-invalid-this: "off" */
import Nightmare from 'nightmare'; // import Nightmare from 'nightmare';
import Nightmare from 'puppeteer';
import {URL} from 'url'; import {URL} from 'url';
import config from './config.js'; import config from './config.js';
@ -34,9 +35,18 @@ let actions = {
await this.autocompleteSearch(langSelector, 'English'); await this.autocompleteSearch(langSelector, 'English');
}, },
// doLogin: async function(userName, password) {
// if (password == null) password = 'nightmare';
// await this.wait(`vn-login [name=user]`)
// .clearInput(`vn-login [name=user]`)
// .write(`vn-login [name=user]`, userName)
// .write(`vn-login [name=password]`, password)
// .click(`vn-login button[type=submit]`);
// },
doLogin: async function(userName, password) { doLogin: async function(userName, password) {
if (password == null) password = 'nightmare'; if (password == null) password = 'nightmare';
await this.wait(`vn-login [name=user]`) return await this.waitFor(`vn-login [name=user]`)
.clearInput(`vn-login [name=user]`) .clearInput(`vn-login [name=user]`)
.write(`vn-login [name=user]`, userName) .write(`vn-login [name=user]`, userName)
.write(`vn-login [name=password]`, password) .write(`vn-login [name=password]`, password)
@ -441,23 +451,25 @@ let actions = {
} }
}; };
for (let name in actions) { // for (let name in actions) {
Nightmare.action(name, function(...args) { // Nightmare.action(name, function(...args) {
let fnArgs = args.slice(0, args.length - 1); // let fnArgs = args.slice(0, args.length - 1);
let done = args[args.length - 1]; // let done = args[args.length - 1];
actions[name].apply(this, fnArgs) // actions[name].apply(this, fnArgs)
.then(res => done(null, res)) // .then(res => done(null, res))
.catch(err => { // .catch(err => {
let stringArgs = fnArgs // let stringArgs = fnArgs
.map(i => typeof i == 'function' ? 'Function' : i) // .map(i => typeof i == 'function' ? 'Function' : i)
.join(', '); // .join(', ');
let orgMessage = err.message.startsWith('.wait()') // let orgMessage = err.message.startsWith('.wait()')
? '.wait() timed out' // ? '.wait() timed out'
: err.message; // : err.message;
done(new Error(`.${name}(${stringArgs}) failed: ${orgMessage}`)); // done(new Error(`.${name}(${stringArgs}) failed: ${orgMessage}`));
}); // });
}); // });
} // }
export default actions;

View File

@ -1,29 +1,47 @@
const Nightmare = require('nightmare'); // const Nightmare = require('nightmare');
const config = require('./config.js'); // const config = require('./config.js');
let nightmare; // let nightmare;
module.exports = function createNightmare(width = 1280, height = 720) { // module.exports = function createNightmare(width = 1280, height = 720) {
if (nightmare) // if (nightmare)
return nightmare; // return nightmare;
nightmare = new Nightmare({ // nightmare = new Nightmare({
show: process.env.E2E_SHOW, // show: process.env.E2E_SHOW,
typeInterval: 10, // typeInterval: 10,
x: 0, // x: 0,
y: 0, // y: 0,
waitTimeout: 2000, // waitTimeout: 2000,
// openDevTools: {mode: 'detach'} // // openDevTools: {mode: 'detach'}
}).viewport(width, height); // }).viewport(width, height);
nightmare.on('console', (type, message, ...args) => { // nightmare.on('console', (type, message, ...args) => {
if (type === 'error') { // if (type === 'error') {
console[type](message, ...args); // console[type](message, ...args);
throw new Error(message); // throw new Error(message);
} else // } else
console[type](message, ...args); // console[type](message, ...args);
// });
// nightmare.header('Accept-Language', 'en');
// return nightmare.goto(config.url);
// };
// changes for puppeteer
const Puppeteer = require('puppeteer');
let browser;
module.exports = async function createNightmare() {
if (browser)
return browser;
browser = await Puppeteer.launch({
headless: false,
slowMo: 0, // slow down by ms
}); });
nightmare.header('Accept-Language', 'en'); return browser;
return nightmare.goto(config.url);
}; };

15
e2e/helpers/puppeteer.js Normal file
View File

@ -0,0 +1,15 @@
const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch({
headless: false,
slowMo: 0, // slow down by ms
// devtools: true,
});
const page = await browser.newPage();
await page.goto('https://wowhead.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();

View File

@ -1,12 +1,40 @@
// import createNightmare from '../../helpers/nightmare';
// describe('Login path', () => {
// const nightmare = createNightmare();
// it('should receive an error when the username is incorrect', async() => {
// const result = await nightmare
// .doLogin('badUser', null)
// .waitForLastSnackbar();
// changes for puppeteer
import createNightmare from '../../helpers/nightmare'; import createNightmare from '../../helpers/nightmare';
import Puppeteer from 'puppeteer';
import actions from '../../helpers/extensions';
import {objectTypeAnnotation} from '@babel/types';
describe('Login path', () => { describe('Login path', () => {
const nightmare = createNightmare(); let browser;
let nightmare;
beforeEach(async() => {
browser = await Puppeteer.launch({
headless: false,
slowMo: 0, // slow down by ms
// devtools: true,
});
nightmare = await browser.newPage();
Object.keys(actions).forEach(key => {
nightmare.__proto__[key] = actions[key].bind(nightmare);
});
nightmare.__proto__.wait = nightmare.waitFor;
});
it('should receive an error when the username is incorrect', async() => { fit('should receive an error when the username is incorrect', async() => {
const result = await nightmare const result = await nightmare
.doLogin('badUser', null) .doLogin('badUser', null);
.waitForLastSnackbar(); // .waitForLastSnackbar();
expect(result.length).toBeGreaterThan(0); expect(result.length).toBeGreaterThan(0);
}); });

View File

@ -187,6 +187,56 @@ function e2eOnly() {
} }
e2eOnly.description = `Runs the e2e tests only`; e2eOnly.description = `Runs the e2e tests only`;
function e2eSingleRun() {
require('@babel/register')({presets: ['@babel/preset-env']});
require('@babel/polyfill');
const jasmine = require('gulp-jasmine');
const SpecReporter = require('jasmine-spec-reporter').SpecReporter;
const createNightmare = require('./e2e/helpers/nightmare');
if (argv.show || argv.s)
process.env.E2E_SHOW = true;
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = true;
const specFiles = [
`${__dirname}/e2e/paths/01*/*[sS]pec.js`,
// `${__dirname}/e2e/paths/02*/*[sS]pec.js`,
// `${__dirname}/e2e/paths/03*/*[sS]pec.js`,
// `${__dirname}/e2e/paths/04*/*[sS]pec.js`,
// `${__dirname}/e2e/paths/05*/*[sS]pec.js`,
// `${__dirname}/e2e/paths/06*/*[sS]pec.js`,
// `${__dirname}/e2e/paths/07*/*[sS]pec.js`,
// `${__dirname}/e2e/paths/08*/*[sS]pec.js`,
// `${__dirname}/e2e/paths/09*/*[sS]pec.js`,
// `${__dirname}/e2e/paths/**/*[sS]pec.js`,
`${__dirname}/e2e/helpers/extensions.js`
];
return gulp.src(specFiles).pipe(jasmine({
errorOnFail: false,
timeout: 10000,
reporter: [
new SpecReporter({
spec: {
displayStacktrace: 'summary',
displaySuccessful: true,
displayFailedSpec: true,
displaySpecDuration: true,
}
})
]
})
// .on('jasmineDone', async function() {
// const nightmare = await createNightmare();
// nightmare.end(() => {});
// await browser.close();
// })
);
}
e2eSingleRun.description = `Runs the e2e tests just once`;
async function backendStatus() { async function backendStatus() {
const milliseconds = 250; const milliseconds = 250;
return new Promise(resolve => { return new Promise(resolve => {

View File

@ -74,6 +74,7 @@
"minimist": "^1.2.0", "minimist": "^1.2.0",
"mysql2": "^1.6.5", "mysql2": "^1.6.5",
"nightmare": "^3.0.2", "nightmare": "^3.0.2",
"puppeteer": "^2.0.0",
"node-sass": "^4.9.3", "node-sass": "^4.9.3",
"nodemon": "^1.18.10", "nodemon": "^1.18.10",
"plugin-error": "^1.0.1", "plugin-error": "^1.0.1",