100Days of Code Challenge

Day 17 of 100daysofcode : Cypress Test Code for Wikipedia Search Functionality

describe('Wikipedia Search Functionality Test', () => {
  before(() => {
    // Visit Wikipedia homepage before the tests
    cy.visit('https://www.wikipedia.org');
  });

  it('should display the search input and language selector', () => {
    // Check if the search input is visible
    cy.get('input#searchInput').should('be.visible');

    // Check if the language selector dropdown is visible
    cy.get('select#searchLanguage').should('be.visible');
  });

  it('should successfully perform a search and display

Day 18 of 100daysofcode : Advanced Automation using Cypress

Here’s an advanced automation testing script using Cypress. The script tests key functionalities of a sample e-commerce website, such as logging in, searching for a product, adding it to the cart, and completing a checkout process.

Prerequisites

Ensure you have Cypress installed and set up. Run the following in your project folder:

npm install cypress --save-dev

Cypress Script

// cypress/e2e/ecommerce_advanced_test.cy.js

describe('E-Commerce Website - Advanced Automation Test', () => {
  const baseUrl = 'https://example-ecommerce.com'; // Replace with the actual website URL

  before(() => {
    cy.visit(baseUrl); // Navigate to the website
  });

  it('Logs in successfully', () => {
    cy.get('a[href="/login"]').click(); // Click on the Login link
    cy.get('input[name="email"]').type('testuser@example.com'); // Enter email
    cy.get('input[name="password"]').type('TestPassword123!'); // Enter password
    cy.get('button[type="submit"]').click(); // Submit login form

    // Assert successful login
    cy.get('.user-profile').should('contain', 'Welcome, Test User'); 
  });

  it('Searches for a product and adds it to the cart', () => {
    cy.get('input[placeholder="Search"]').type('Wireless Mouse{enter}'); // Search for a product
    cy.get('.product-list').should('contain', 'Wireless Mouse'); // Assert product appears

    // Select the product
    cy.get('.product-card').contains('Wireless Mouse').click();

    // Add the product to the cart
    cy.get('button').contains('Add to Cart').click();

    // Assert product is added to the cart
    cy.get('.cart-notification').should('contain', 'Wireless Mouse added to your cart');
  });

  it('Completes the checkout process', () => {
    cy.get('.cart-icon').click(); // Open the cart
    cy.get('button').contains('Proceed to Checkout').click(); // Proceed to checkout

    // Fill in shipping information
    cy.get('input[name="address"]').type('123 Cypress St, Test City, TX');
    cy.get('input[name="zip"]').type('12345');
    cy.get('input[name="phone"]').type('1234567890');

    // Choose payment method and complete the payment
    cy.get('input[name="payment"][value="credit-card"]').check();
    cy.get('input[name="card-number"]').type('4111111111111111');
    cy.get('input[name="expiry"]').type('12/25');
    cy.get('input[name="cvv"]').type('123');
    cy.get('button').contains('Place Order').click();

    // Assert order completion
    cy.get('.order-confirmation').should('contain', 'Thank you for your order');
  });

  after(() => {
    // Log out after tests
    cy.get('.user-menu').click();
    cy.get('a').contains('Logout').click();

    // Assert successful logout
    cy.get('a[href="/login"]').should('be.visible');
  });
});

Key Features of the Script:

  1. Modularized Tests: Each test case handles a specific functionality, making the script easier to debug and maintain.
  2. Dynamic Selectors: Uses robust and descriptive selectors to ensure test stability.
  3. Assertions: Verifies that each step is successful with should() assertions.
  4. Reusable Code: Leverages Cypress commands like cy.get() and chaining for efficiency.
  5. Complete Flow: Covers login, search, cart, checkout, and logout processes.

Run the Test

To execute the test, run the following command:

npx cypress open

From the Cypress Test Runner, select the ecommerce_advanced_test.cy.js file to run the test.
lebanon-mug

Day 19 of 100daysofcode : Playwright - For cross-browser end-to-end testing

Playwright is an excellent tool for cross-browser end-to-end testing. Developed by Microsoft, it provides fast and reliable automation for testing web applications across modern browsers. Here’s an overview of what makes Playwright stand out:

Key Features of Playwright

  1. Cross-Browser Support:
  • Works seamlessly with Chromium (Google Chrome, Microsoft Edge), WebKit (Safari), and Firefox.
  • Ensures your application is functional across all major browsers.
  1. Headless and Headed Modes:
  • Tests can be run in headless mode for faster execution or in headed mode for debugging.
  1. Automated Browser Contexts:
  • Playwright allows you to create isolated browser contexts for parallel execution of tests, saving time.
  1. Powerful API:
  • Provides robust APIs to simulate user interactions like clicking, typing, hovering, file uploads, and more.
  1. Built-In Test Runner:
  • Playwright Test includes a test runner with built-in features like parallel test execution, retries, fixtures, and more.
  1. Cross-Platform:
  • Playwright supports Linux, macOS, and Windows, ensuring compatibility across development environments.
  1. Network Interception:
  • Allows mocking API responses, testing different scenarios without requiring backend changes.
  1. Visual Comparisons:
  • Supports screenshot comparisons for visual regression testing.
  1. Language Support:
  • Playwright works with multiple languages like JavaScript/TypeScript, Python, Java, and C#.
  1. Debugging Tools:
  • Comes with Playwright Inspector to debug tests visually and trace logs for easier troubleshooting.

Why Choose Playwright?

  • Reliable and Fast: Auto-waits for elements and browser states, reducing flaky tests.
  • Open Source: Free to use with an active community.
  • Full Stack Testing: Covers both frontend and backend testing (e.g., API testing).
  • Mobile Emulation: Emulate devices for responsive testing.

playwright

Day 20 of 100daysofcode : How to Install Playwright

Installing Playwright

Get started by installing Playwright using npm, yarn or pnpm. Alternatively you can also get started and run your tests using the VS Code Extension.

Day 21 of 100daysofcode : Logging in using Playwright

const { chromium } = require('playwright');

(async () => {
    const browser = await chromium.launch({ headless: false });
    const context = await browser.newContext();
    const page = await context.newPage();

    await page.goto('https://example.com/login');

    // Replace with actual selectors and credentials
    await page.fill('#username', 'your_username');
    await page.fill('#password', 'your_password');
    await page.click('#login-button');

    await page.waitForNavigation();
    console.log('Login successful!');

    await browser.close();
})();

lebanon-mug

Day 22 of 100daysofcode : advantages of using Playwright :

Here are the key advantages of using Playwright for automation testing:

1. Cross-Browser Support

  • Supports Chromium, Firefox, and WebKit.
  • Ensures consistent testing across different browsers.

2. Multi-Platform & Multi-Language

  • Works on Windows, macOS, and Linux.
  • Supports multiple languages: JavaScript, TypeScript, Python, Java, and C#.

3. Headless & Headed Execution

  • Can run tests in headless mode for faster execution.
  • Supports headed mode for debugging.

4. Auto-Wait & Smart Assertions

  • Automatically waits for elements to be ready before performing actions.
  • Provides robust assertions for reliable tests.

5. Parallel & Distributed Testing

  • Supports parallel execution to speed up test runs.
  • Integrates with CI/CD pipelines for efficient testing.

6. Mobile Emulation & Device Testing

  • Simulates different mobile devices and screen sizes.
  • Supports geolocation, permissions, and network throttling.

7. Network Interception & Request Mocking

  • Allows modifying network requests and responses.
  • Helps in testing API calls and simulating different network conditions.

8. Visual & Screenshot Testing

  • Supports screenshot and video recording.
  • Enables visual regression testing.

9. Built-in Test Generator

  • Comes with a CodeGen tool to generate test scripts automatically.

10. Strong Community & Active Development

  • Backed by Microsoft, ensuring regular updates and improvements.
  • Growing community support and documentation.

Day 23 of 100daysofcode : Cross-Browser Testing Script using Playwright


const { chromium, firefox, webkit } = require('playwright');

(async () => {
  // Browsers to test
  const browsers = [
    { name: 'Chromium', instance: chromium },
    { name: 'Firefox', instance: firefox },
    { name: 'Webkit', instance: webkit },
  ];

  // URL to test
  const url = 'https://example.com';

  for (const browserType of browsers) {
    const browser = await browserType.instance.launch({ headless: false });
    const context = await browser.newContext();
    const page = await context.newPage();

    console.log(`\nTesting on ${browserType.name}`);

    try {
      // Navigate to the page
      await page.goto(url);
      console.log(`Navigated to ${url}`);

      // Example assertions
      const pageTitle = await page.title();
      console.log(`Page Title: ${pageTitle}`);

      // Check for an element (adjust selector as needed)
      const element = await page.$('h1');
      if (element) {
        console.log('Header found successfully.');
      } else {
        console.log('Header NOT found!');
      }

      // Simulate interaction
      await page.click('a'); // Click on the first link
      await page.waitForTimeout(2000); // Wait for navigation

      console.log('Interaction successful.');
    } catch (error) {
      console.error(`Error testing on ${browserType.name}:`, error);
    } finally {
      await browser.close();
    }
  }
})();

Day 24 of 100daysofcode : testing using Playwright

lebanon-mug

Day 25 of #100daysofcode: Advance testing