I am trying to find a way to set the browser configurations for Puppeteer once so that I do not have to do it for every test case. Currently, this is the structure I'm working with:
jest.config.js
process.env.BROWSER_PATH='<PATH_TO_CHROME>/chrome.exe';
module.exports = {
preset: "jest-puppeteer",
globals: {
URL: "http:localhost:3000",
HEADLESS: (process.env.NODE_ENV==='development' ||
process.env.NODE_ENV==='production'
? true : false
)
},
testMatch: [
"**/test/**/*.test.js"
],
verbose: true
}
jest-puppeteer.config.js
module.exports = {
launch: {
headless: HEADLESS,
slwoMo: process.env.SLOWMO ? process.env.SLOWMO : 0,
devtools: false,
executablePath: process.env.BROWSER_PATH,
}
}
browserTest.js
describe('Browser Test', () => {
// This test has 2 asserts
test('Browser Popup: successful', async () => {
console.log("[BrowserTest] headless:", HEADLESS);
try {
const browser = await puppeteer.launch({
// headless: process.env.HEADLESS == 'true'? true:false,
// executablePath: process.env.BROWSER_PATH,
isMobile: false,
});
const page = await browser.newPage();
await page.goto(URL, {
waitUntil: 'domcontentloaded'
});
page.once('load', () => console.log('[BrowserPopup] Page loaded!'));
await new Promise(resolve => setTimeout(resolve, 7500));
await browser.close();
} catch (e) {
console.log('Err: ', e);
}
}, timeout);
});
The idea is that if I have headless and executablePath in the launch configurations (in jest-puppeteer.config.js), then shouldn't puppeteer pick it up from there? Why do I have to re-declare it when launching the browser in my test case?
The alternative I thought of was that if I cannot do this, then I want to be able to replicate this behavior in the process.env.HEADLESS as shown in the first line jest.config.js. I'd remove it from the globals declaration of the config file. This method does not work either.
Question: Is there a singular place I could put this so that I don't have to keep declaring this? The HEADLESS state depends on its environment it is being executed in.
Note: Currently, this throws an error because jest-puppeteer.config.js cannot see jest.config.js. So its a 'HEADLESS is not defined' error. After fixing it, it threw another error that Chrome was not defined. So I had to put that in the puppeteer.launch({}) parameters as well. Which leads me to believe that jest-puppeteer.js isn't really doing anything, since it can't see headless or executablePath.
Note: I run the tests using jest --config=jest.config.js. This works because I can use the global variable in a successful test run. Thanks for all the help!
Aucun commentaire:
Enregistrer un commentaire