Playwright + JS · Lesson 1 of 35

0/35 done

Module 1: JavaScript Fundamentals

1. JavaScript Fundamentals for Test Automation

12 minlesson

Try it yourself

Complete the verification function:

  1. Declare const expectedTitle = "TargetQA - Learn Automation".
  2. Use if/else to check if actualTitle === expectedTitle.
  3. Return true if matching, otherwise false.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

JavaScript Variables: const vs let vs var

In modern test automation, we avoid var due to variable hoisting and scope leak issues:

  • const: For values that remain fixed during test runs (Base URLs, selectors, timeouts).
  • let: For values that change dynamically (retry counts, loop indices, status flags).
javascript
const baseUrl = "https://targetqa.com";
let retryAttempts = 0;
retryAttempts += 1;

Primitive Data Types

  1. string: Test locators, text assertions ("Sign In").
  2. number: Element counts, status codes (200, 5000).
  3. boolean: Element states (true, false).
  4. null: Deliberate empty value.
  5. undefined: Unassigned variable.

Loops for Automation

javascript
// Loop through test items with for...of:
const menuItems = ["Dashboard", "Orders", "Settings"];
for (const item of menuItems) {
  console.log("Validating navigation item: " + item);
}

// Inspect configuration keys with for...in:
const config = { browser: "chromium", headless: true };
for (const key in config) {
  console.log(key + ": " + config[key]);
}

Key Takeaways & Walkthrough Summary

Hello and welcome to Playwright with JavaScript! In this first module, we explore JavaScript fundamentals essential for test automation. You will learn variable declarations, data types, comparison operators, and loops to control your automated test flows. Let's get started!

Part of the Playwright + JS course (Playwright with JavaScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why should we use === instead of == in JavaScript?

=== checks both value and type without silent type coercion. == converts types unexpectedly (e.g., '0' == 0 is true), which causes test flakiness.

When should I use for...of vs for...in?

Use for...of to iterate through elements of arrays or lists of elements. Use for...in only when iterating through property keys of an object.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

JavaScript
Skip