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:
Modularized Tests: Each test case handles a specific functionality, making the script easier to debug and maintain.
Dynamic Selectors: Uses robust and descriptive selectors to ensure test stability.
Assertions: Verifies that each step is successful with should() assertions.
Reusable Code: Leverages Cypress commands like cy.get() and chaining for efficiency.
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
Cross-Browser Support:
Works seamlessly with Chromium (Google Chrome, Microsoft Edge), WebKit (Safari), and Firefox.
Ensures your application is functional across all major browsers.
Headless and Headed Modes:
Tests can be run in headless mode for faster execution or in headed mode for debugging.
Automated Browser Contexts:
Playwright allows you to create isolated browser contexts for parallel execution of tests, saving time.
Powerful API:
Provides robust APIs to simulate user interactions like clicking, typing, hovering, file uploads, and more.
Built-In Test Runner:
Playwright Test includes a test runner with built-in features like parallel test execution, retries, fixtures, and more.
Cross-Platform:
Playwright supports Linux, macOS, and Windows, ensuring compatibility across development environments.
Network Interception:
Allows mocking API responses, testing different scenarios without requiring backend changes.
Visual Comparisons:
Supports screenshot comparisons for visual regression testing.
Language Support:
Playwright works with multiple languages like JavaScript/TypeScript, Python, Java, and C#.
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.