Posts

Browser vs Node.js Runtime

Browser Runtime: // Available in browser window.alert('Hello'); document.getElementById('button'); localStorage.setItem('key', 'value'); fetch('https://api.example.com'); // NOT available in browser const fs = require('fs'); // ERROR!   Node.js Runtime:  // Available in Node.js const fs = require('fs'); const http = require('http'); process.exit(0); // NOT available in Node.js window.alert('Hello'); // ERROR! document.getElementById('button'); // ERROR!  What Does "Providing a Runtime" Mean? When we say "Node.js provides a runtime for JavaScript," we mean: It gives JavaScript a place to run outside the browser It provides access to system resources (files, network, processes) It manages memory and execution It handles errors and crashes It provides utility functions and modules Real-World Example When you run Playwright tests: // Your test file: test.js const { chromium } = requi...

what is Node.js runtime environment?

  What is a Runtime Environment? A runtime environment is the infrastructure that allows your code to execute. Think of it as the "world" where your program lives and runs. Simple analogy : Your JavaScript code = a recipe Runtime environment = the kitchen with all the tools, ingredients, and appliances needed to make that recipe The Node.js runtime environment is everything that's needed to execute JavaScript code outside of a browser. It includes: 1. JavaScript Engine (V8) Reads and executes your JavaScript code Converts JavaScript to machine code the computer understands Same engine Chrome uses 2. Core Libraries/APIs Built-in modules that give JavaScript superpowers it doesn't have in browsers: // File system access (not possible in browsers!) const fs = require('fs'); fs.readFile('data.txt', 'utf8', (err, data) => {   console.log(data); }); // HTTP server (create your own web server!) const http = require('http'); http.cr...

Why do we need Node.js as a pre-requisite before installing Playwright?

Before we get into the details, let us first know the origin of Node.js Ryan Dahl created Node.js in 2009 . He was a software engineer frustrated with the limitations of existing web servers at the time. The Problem He Wanted to Solve In 2009, Ryan was working on web applications and noticed: Apache HTTP Server (the most popular web server) handled each connection with a separate thread This meant if you had 10,000 simultaneous users, you needed 10,000 threads Creating/managing so many threads consumed massive memory and CPU File upload progress bars were difficult - servers couldn't easily tell the browser "50% uploaded... 75% uploaded..." By combining JavaScript, V8, and non-blocking I/O, Ryan Dahl created a platform that revolutionized backend development and enabled JavaScript to dominate both frontend and backend.   Playwright is Built on Node.js Playwright is a Node.js Library Playwright is written in TypeScript/JavaScript and distributed as an npm package It r...

Await and Async

Typescript and Javascript is asynchronous . This means in our test which has multiple steps there is no guarantee that they will be executed one after the other. Let us consider an example where we typically try to login to a website.  Say, I want to book the book travel tickets. Below are the steps we follow manually.  Step1: Go to the Travel website URL  Step2: Provide Username and Password  Step3: Click on Login button to get into the application If we can notice, all these steps are sequential in nature. if we try to miss step2 and with out credentials clicking on Login button would not take us to the application.  Similarly, as we are trying to do automation ( web automation ), we should ensure all these steps are followed in sequence in above scenario. And we are relying on Typescript/Javascript to enable this automation. But we should understand that these are asynhronous. Meaning the steps can be executed in any order. So t...

What is spec.ts?

The spec file is a collection of test cases. Once we have done npm init playwright, we see a function @playwright/test imported. Using this test annotation, we can define the test cases. import {test} from '@playwright/test'; Test function is what we are importing. test('Name of the test case', 'callback function' { // playwright test steps // step 1 // step 2 ........ ........ ........ }); There are some built-in test sub-functions available that we can use with the test annotation. These help in controlling which tests to run or not. Example: test.only() -- Playwright will only run this test from the specs. test.skip() -- Playwright will not run this test from the specs. test.fail() -- Playwright will validate and check if the test is failing. if not, then it will complain.