Documentation Biome
website·Indexed 30 days ago
https://www.documentation-biome.xyz/A comprehensive learning guide covering JavaScript fundamentals, Playwright automation, and CI/CD. Topics include JavaScript arrays and functions, Playwright Page Object Model (POM), API testing with JSON schema validation and snapshots, Git version control, and GitHub Actions workflow triggers and reusable workflows.
What's inside Documentation Biome
- An Application Programming Interface (API) acts as an intermediary messenger that allows two applications to communicate by delivering requests to a supplier and returning the response. Testing APIs allows developers to quickly and efficiently verify that the system performs as expected when handling both expected and unexpected requests. While RESTful APIs are the most common, other types include SOAP and GraphQL.
Implement a CharacterCreation class for a game
Create aCharacterCreationclass to manage game character attributes and behaviors. The class uses a constructor to initialize the character's name, class, and level. It includes agreeting()method for a formatted introduction and anattack()method that returns a specific action string based on the character'scharClass(e.g., Wizard, Barbarian, Rogue, Ranger, Fighter).class CharacterCreation { constructor(name, charClass, level) { this.name = name; this.charClass = charClass; this.level = level; } greeting() { return `Hello my name is ${this.name} and im a level ${this.level} ${this.charClass}` } attack() { if (this.charClass === "Wizard") { return `${this.name} casts FIREBALL!` } else if (this.charClass === "Barbarian") { return `${this.name} swings an axe!` } else if (this.charClass === "Rogue") { return `${this.name} Attacks with a dagger` } else if (this.charClass === "Ranger") { return `${this.name} Fires an arrow` } else if (this.charClass === "Fighter") { return `${this.name} Swings a sword` } else { return `Hey, check for spelling on the ${this.charClass} charClass` } } } module.exports = { CharacterCreation }Understand Git forking
Forking creates an exact replica of another user's repository under your own account. This allows you to experiment with changes, evaluate code, or propose updates to the original project via pull requests without affecting the main repository.Generate assertions using Playwright Codegen
Starting with Playwright v1.40.0, the Codegen tool includes a horizontal bar at the top of the browser window that allows users to record assertions. This closes the loop on test creation by generatingexpectstatements automatically based on UI interactions.Install Playwright using npm
To install Playwright, you must have NodeJS installed on your machine. Start by creating a project folder, then run the initialization command. During the setup process, the following options are recommended for a standard installation:
- Language: JavaScript
- e2e test location: tests
- GitHub Actions Workflow: true
- Install Playwright Browsers: true
# Verify NodeJS installation npm -v node -v # Initialize Playwright project npm init playwright@latestControl program flow using if, else if, and else statements
JavaScript uses conditional statements to determine whether specific blocks of code should execute based on a boolean condition.
ifstatement: Executes the block of code if the specified condition is true.else ifstatement: Provides a new condition to test if the precedingiforelse ifconditions were false.elsestatement: Executes the block of code if all preceding conditions in the chain were false.
Key behavior: Only the first truthy condition in a chain is executed. If multiple
else ifblocks are true, only the first one encountered will run. If no conditions are true and noelseblock is provided, the program simply continues to the next line of code after the conditional block.let conditionIsFalse = false; let conditionIsTrue = true; if (conditionIsFalse) { // This will not run console.log("This block of code runs"); } else if (conditionIsTrue) { // This is the first truthy condition, so it runs console.log("This is the else if block"); } else if (conditionIsTrue) { // This will NOT run even though it is true, because a previous block already executed console.log("This is the else if block 2"); } else { // This runs only if all above are false console.log("This is the else block"); }Return values from a JavaScript function
Use thereturnstatement to output a value from a function to the caller. When thereturnstatement is reached, the function stops executing immediately.function addTwoNumbers(a, b) { return a + b; } const sum = addTwoNumbers(5, 5); console.log(sum); // 10Define and instantiate a JavaScript class
JavaScript classes act as templates for creating objects. A class is defined using the
classkeyword followed by the class name and a block of code. To create an instance of a class, use thenewkeyword followed by the class constructor.Key constraints:
- Class names must be unique within their scope; declaring two classes with the same name will result in a syntax error.
- The
constructor()method is a special method used to initialize object properties. It is executed automatically when a new object is created vianew. If no constructor is explicitly defined, JavaScript provides an empty one by default.
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } greeting() { return `Hello my name is ${this.firstName} ${this.lastName}` } } // Creating instances of the Person class let person1 = new Person("Frodo", "Baggins"); console.log(person1.greeting()); // Output: Hello my name is Frodo BagginsConfigure Git and GitHub for source code management
Source code management is handled via GitHub. This requires two steps: creating a GitHub account and installing the Git CLI on your local operating system (Windows or Mac).Use const for immutable variable declarations
Theconstkeyword is used to declare variables that cannot be updated (reassigned) or redeclared. Attempting to change the value of aconstvariable or declare it again will result in an error.const firstName = "Steve"; // Attempting to update will fail // firstName = "will"; // Attempting to redeclare will fail // const firstName = "will";Record tests at the current cursor position
The Playwright extension provides aRecord at cursorfeature that starts recording user interactions in the browser and inserts the generated test code directly at the current cursor position in the active spec file.Externalize API test data using JSON files
To improve maintainability and readability, move large request bodies into a separate JSON file. This allows you to reuse the data across multiple tests and dynamically reference fields (like IDs) for assertions instead of hard-coding them.// @ts-check const { test, expect } = require('@playwright/test'); const petData = require('../data/pet.json'); test('adding a pet', async ({ request }) => { const response = await request.put('pet', { data: petData }); expect(response.status()).toBe(200); }); test('check that pet was added', async ({ request }) => { // Use the ID from the JSON file to avoid hard-coding const response = await request.get(`pet/${petData.id}`); const json = await response.json(); // Assert that the returned name matches the data file expect(json.name).toBe(petData.name); });