spot_img

How to Open a Local File in Puppeteer?

There are a lot of ways to test and automate browsers with Puppeteer. Most of the time, you will use the headless Chrome tool to navigate to live websites.

However, there may be times when it makes sense to load a local file instead. Here’s how to use Puppeteer to open a local file.

How to Open a Local File in Puppeteer?


To open a local file in Puppeteer, change the navigation URL to the file directory location with the file:// prefix.

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    await page.goto(`file://${process.cwd()}/src/techozu.html`);

    //Do stuff here

   await browser.close();
})();

The file:/ prefix replaces the usual http(s):// prefix and tells the browser to look for a local file. Since Puppeteer is just Chrome without a user interface, it will look for the file in the place you tell it to.

The same thing will happen with your regular desktop browser. If you don’t know where your file is located, you can drag it into your browser window and then copy and paste the URL into Puppeteer.

On a Windows computer, your URL string will look something like this:

file:///C:/Users/Techozu/Projects/testfile.js

On Linux, it may look something like this:

file:///root/Projects/Techozu/scripts/testfile.js

In the code example above, we use the process.cwd() function to find out where our script is running from.

Now that we have the path, we need to add any subdirectories and the file itself. The “process” module is automatically imported by Node, so you don’t need extra code to run that function.

 

Why Open Local Files


When you open local files in Puppeteer, one of the main reasons is to make automated browser tests for your projects. You can test simple websites without running a development server if you use a robust testing framework like Jest.

You can do things on your page as a user would, and then use the “expect” function to make sure the page did what it was supposed to. Another reason to load a local file is to automatically make certain file types, like PDFs or images.

spot_img

Subscribe

Related articles

OnePlus 5T Wallpapers Download

Introduction: The OnePlus 5T is a popular smartphone known for...

Airtel’s First Quarterly Loss in 2002: A Closer Look at Jio’s Impact

The telecom industry has witnessed several significant shifts over...

Xiaomi Confirms Investment in Blackshark Gaming Phone Launch set for April 13

An engaging introduction to Xiaomi Confirms Investment in Blackshark...

LG G7 ThinQ M LCD Panel

Introduction:The LG G7 ThinQ M LCD panel is a...

Intel Core i9 Laptops with Optane Memory

Intel Core i9 laptops with Optane Memory combine the...

Apple iOS 11.4 Beta 1

Apple iOS 11.4 Beta 1 is the latest update...

Google Search AI Reorganization: Improving Search Quality and User Experience

Introduction:In the ever-evolving digital landscape, search engines play a...
Peter Graham
Peter Grahamhttp://fix-iphones.com
Hi there! I'm Peter, a software engineer and tech enthusiast with over 10 years of experience in the field. I have a passion for sharing my knowledge and helping others understand the latest developments in the tech world. When I'm not coding, you can find me hiking or trying out the latest gadgets.

LEAVE A REPLY

Please enter your comment!
Please enter your name here