2. Functions & Modern JavaScript Syntax

Functions & Arrow Functions in Test Automation

Arrow functions provide concise syntax and lexical this binding:
// Arrow function with default parameter:
const formatUrl = (path, host = "https://targetqa.com") => ${host}${path};

// Callback function pattern:
function executeWithRetry(action, retries = 3) {
for (let i = 0; i < retries; i++) {
try {
return action();
} catch (e) {
if (i === retries - 1) throw e;
}
}
}

Destructuring & Rest/Spread

// Object destructuring:
const testConfig = { browser: "chromium", timeout: 30000, headless: true };
const { browser, timeout } = testConfig;

// Spread operator to clone and override options:
const mobileConfig = { ...testConfig, viewport: { width: 375, height: 667 } };

Task

  • Implement buildTestUrl:
  • 1. Accept path string and optional baseUrl defaulting to "https://targetqa.com".
  • 2. Return `${baseUrl}${path}`.
πŸ’‘Common doubts
Did you like the problem?
Lesson 2 of 35
JavaScript
Loading code editor…
Next