NodeJS: using chrome-remote-interface instead puppeteer - node.js

I have a project which uses puppeteer to print PDFs, the problem is the download of chromium is too large to work with servers, so I want to migrate it to chrome-remote-interface instead. There is a better way to do that? I will change too much my code?

You don't even need to switch to such libraries for this problem. Puppeteer already has solution for that.
puppeteer-core
Puppeteer has puppeteer-core library which is without the chrome download and will work with remote interface.
The only difference between puppeteer-core and puppeteer atm is that puppeteer-core doesn't install chromium. So you can just swipe it.
The original difference is described here. The document for .connect is here.
Using the environment variable
You can use puppeteer as usual, except provide PUPPETEER_SKIP_CHROMIUM_DOWNLOAD environment variable to skip the download when doing npm install.

Related

using puppeteer or puppeteer-core in cli script

I need to write a node cli script that will run some tests on the forms of a website I'm working on. I want to use puppeteer but I'm a bit confused about the difference between the full version and puppeteer-core. What is the best choice if I want to run the tests from a cli script without opening the browser and only simulating it?
To put it simply, puppeteer-core is for when you already have a browser and don't want to download a whole Chromium which the main puppeteer package does, automatically.
It is better to go with the full puppeteer since this way you will be getting the "batteries included, tested and are guaranteed to work" experience.
Official documentation offers a detailed comparison.

How to bundle headless chromium module with AWS Lambda?

I'm attempting to use Puppeteer with Lambda, however, on serverless deploy, the lambda errors out due to exceeding the 250mb unbundled package size limit.
So, to get under the limit, I've switched to Puppeteer core which doesn't come packaged with chromium. This requires referencing a path to an executable to launch chrome. (e.g. puppeteer.launch({executablePath: headlessChromiumPath}));
However, I'm not sure how to load a headless Chromium into my container so that I can later reference it.
To solve this I'm trying a couple of things:
First, I've downloaded a binary headless chromium and I've included it into my API.
File structure:
-run-puppeteer.js
-headless_shell.tar.gz
Referenced like:
const browser = await puppeteer.launch({
executablePath: "../headless_shell.tar.gz"
});
However, I can't import or require it so my lambda doesn't recognize that it exists and doesn't include it in my deployment package.
My question here is how do I correctly include the headless file into my API so that I can reference it from within this lambda?
If that isn't an option - I see that I can upload the binary to S3 and then download it on container startup. Any references on where to begin tackling this would be much appreciated.
You can use chrome-aws-lambda to either package chrome with your Lambda or create a Lambda Layer to avoid the package size.
I did something similar here based on chrome-aws-lambda
chrome-aws-lambda indeed is big ~40MB adding to the deployment package, using Layer could potentially reduce the package size but also could increase the size, because the 250MB unzipped limit includes the layer and the lambda code. If you use chrome-aws-lambda then definitely do NOT use puppeteer, instead use puppeteer-core for a smaller size. I did a very similar setup this hopefully it helps1

Configuring Cypress's Electron browser to handle downloads

Is there a way to configure the Electron browser that is within Cypress, by directly modifying the Electron browser's configuration?
Namely, there's an issue with Cypress where it cannot detect download prompts. However, there's a solution that could hypothetically be applied to Electron, which would fix this issue with Cypress.
My thought was to go directly to wherever Electron is being run from within the Cypress library after installing it via npm. However, I cannot find anything pertaining to Electron in my node_modules/, even though Cypress's GitHub repository has mentions of Electron in its code.
I had the same issue and I know your questions is quiet a bit old but anyway - a solution is in development directly at Cypress:
https://github.com/cypress-io/cypress/issues/949#issuecomment-755975882
Update: It is available in Cypress >=6.3.0: https://github.com/cypress-io/cypress/issues/949#issuecomment-763323357

Run jest with electron instead of node

To make a long story short, I'd like to run my jest tests (using CLI) with electron instead of node.
It's relevant when using native module, because you need to build them using electron header while jest run them using plain node.
So I must either build my native modules for my app (at least in dev mode) or my tests, I can't have both to work.
In this thread they propose to use mocha, but I want to use jest, which is far more advanced and interact well with React.
Note that I don't want to mock the native module, since I write integration tests.
I opened an issue about the zmq github repo. One proposed solution is "to target your tests using ELECTRON_RUN_AS_NODE=true electron as your node runtime".
This is a very good solution, since using electron will both make the test environment closer to the execution environment and solve my specific issue with native modules.
I'd like to apply that, but I do no seem to be able to setup the jest CLI to use electron instead of node, and I have no idea where to start. Maybe I should run jest programmatically without the CLI ? But I might lose the nice test filtering features of the CLI.
Has anyone solved this already?
"ELECTRON_RUN_AS_NODE=true ./node_modules/.bin/electron ./node_modules/.bin/jest works fine
If you're on Windows, then Eric Burel's excellent discovery might need a bit of a tweak to use the environment variable, and call the right version of Jest:
cross-env ELECTRON_RUN_AS_NODE=true ./node_modules/.bin/electron ./node_modules/jest-cli/bin/jest.js
Sadly, the colouring of the text in the results is lost.

Headless browser automation app via electron js app

I would like to create an electron app that can do some web automation based on user input into a GUI. In my research it seems my two best bets are Phantom and Selenium+Chromedriver.
The thing I'd like to do is have an app that someone else could download and run without any additional setup. It seems with Chromedriver and Phantom that I'd need to have others download and add these things to their PATH. In order to get things functioning.
Is there a way around this? Or is there another approach I should be taking? Any advice is appreciated. Thanks!
First off, you should have a look at Nightmare.js which is like PhantomJS in many ways, but uses Electron under the hood (and that's good, because Chromium in Electron is very fresh compared to PhantomJS engine).
If you still want to use PhantomJS in Electron that's quite fine too. You may bundle it with your application or install npm module as a dependency and require that in your script. The main thing is - PhantomJS will be installed together with your app and you know the path to that folder.

Resources