Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Running puppetteer in tests in headless mode

Marek Trunkatb8d90c0372ce
diff --git a/src/puppeteer.js b/src/puppeteer.js
index 6cd52b6e3..4190d6479 100644
--- a/src/puppeteer.js
+++ b/src/puppeteer.js
@@ -3,18 +3,18 @@ import { checkParamOrThrow } from 'apify-client/build/utils';
import { parseUrl } from './utils';
import { ENV_VARS } from './constants';
-export const PUPPETEER_DEFAULT_OPTS = {
- args: ['--no-sandbox', '--disable-setuid-sandbox'],
+export const getDefaultPuppeteerOptions = () => ({
+ args: ['--no-sandbox'],
headless: !!process.env[ENV_VARS.HEADLESS],
proxyUrl: null,
-};
+});
/**
* @memberof module:Apify
* @function
* @description Launches Puppeteer preconfigured to work with Apify platform.
* The result of the function is a result of puppeteer.launch().
- * @param options Optional settings, their defaults are provided in the PUPPETEER_DEFAULT_OPTS constant.
+ * @param options Optional settings, their defaults are provided in the getDefaultPuppeteerOptions function.
* @return Returns a promise.
*/
export const launchPuppeteer = (opts = {}) => {
@@ -34,5 +34,5 @@ export const launchPuppeteer = (opts = {}) => {
opts.env[proxyVarName] = opts.proxyUrl;
}
- return puppeteer.launch(Object.assign({}, PUPPETEER_DEFAULT_OPTS, opts));
+ return puppeteer.launch(Object.assign({}, getDefaultPuppeteerOptions(), opts));
};
diff --git a/test/puppeteer.js b/test/puppeteer.js
index d1f82ec0b..a0178046f 100644
--- a/test/puppeteer.js
+++ b/test/puppeteer.js
@@ -1,7 +1,10 @@
import { expect } from 'chai';
import Apify from '../build/index';
+import { ENV_VARS } from '../build/constants';
-describe('Apify.launchPuppeteer()', function () {
+process.env[ENV_VARS.HEADLESS] = true;
+
+describe('Apify.launchPuppeteer()', () => {
it('throws on invalid args', () => {
expect(() => Apify.launchPuppeteer({ proxyUrl: 'invalidurl' })).to.throw(Error);
expect(() => Apify.launchPuppeteer({ proxyUrl: 'http://host-without-port' })).to.throw(Error);