Javascript
Course Index
Index

Javascript Basics

1. What is JavaScript, and how is it used in automation testing?

JavaScript is a lightweight, interpreted programming language used to create interactive web applications. In automation testing, JavaScript is commonly used in tools like Selenium WebDriver (with WebDriverJS), Cypress, and Playwright to write test scripts for UI automation.

2. What are var, let, and const in JavaScript?

  • var: Function-scoped, can be redeclared and reassigned.
  • let: Block-scoped, can be reassigned but not redeclared.
  • const: Block-scoped, cannot be reassigned or redeclared.
Example:
Javascript
Copy
var a = 10; // Function-scoped
let b = 20; // Block-scoped
const c = 30; // Constant

3. How do you handle timeouts in JavaScript automation?

Timeouts prevent scripts from failing due to slow page loads. You can handle timeouts using:
1. Implicit Wait (Selenium WebDriverJS)
driver.manage().setTimeouts({ implicit: 10000 }); 2. Explicit Wait (Using await in Playwright/Cypress)
await page.waitForSelector("#element", { timeout: 5000 });

4. How does JavaScript handle exceptions in automation testing?

JavaScript handles errors using try...catch blocks.
Example

Javascript
Copy
try {
  await page.click("#nonExistingElement");
} catch (error) {
  console.error("Element not found", error);
}

This prevents test scripts from failing abruptly.

5. What are JavaScript Data Types, and how are they useful in automation testing?

JavaScript has primitive and reference data types:

  • Primitive: string, number, boolean, null, undefined, bigint, symbol
  • Reference: object, array, function
Example (Using Data Types in Tests)
Javascript
Copy
let testName = "Login Test"; // String
let isPassed = true; // Boolean
let responseTime = 2.5; // Number
let user = { username: "tester", password: "12345" }; // Object

6. What is the difference between == and === in JavaScript?

  • == (Loose Equality) checks value but allows type conversion.
  • === (Strict Equality) checks both value and type.
Example:
console.log(5 == "5"); // true (type conversion happens) console.log(5 === "5"); // false (different data types)

Use Case in Automation: Prevent unintended type coercion while comparing expected vs. actual results.

7. What is the difference between null and undefined in JavaScript?

  • null: A deliberate assignment representing "no value".
  • undefined: A variable declared but not assigned any value.
Example:
Javascript
Copy
let a; // undefined
let b = null; // null
console.log(typeof a, typeof b); // "undefined" "object"


Use Case in Automation: Checking API responses where a missing key returns undefined, while null indicates a deliberate empty value.