Skip to main content

Posts

Get OTP from email

/** * Retrieves an OTP from Gmail in a headless tab. * @param {Object} options - The options for the function. * @param {string} options.email - The Gmail email address. * @param {string} options.password - The Gmail password. * @returns {Promise<string | null>} The OTP retrieved from the email. */ export async function getOtpFromGmail ({ email , password , } : { email : string ; password : string ; }) : Promise < string | null > { const { chromium } = require ( "playwright" ); // Launch a headless browser context for Gmail login const browser = await chromium . launch ({ headless : true }); const context = await browser . newContext (); const page = await context . newPage (); await page . goto ( "https://mail.google.com" ); const emailInput = await page . waitForSelector ( "#identifierId" ); await emailInput . fill ( email ); const emailNextButton = await page . wa...
Recent posts

Playwright Solution

 There is a web site need to run with browser. and which has zero downtime and alert and notifications pop up too. Third party applications are involved in and need to fix bugs too.What is the solution and how to create a framework for this scenario : need to use playwright as well 1. Understand the Requirements and Challenges: Zero Downtime: Ensure tests do not interrupt the service. Alerts and Notifications: Handle unexpected pop-ups gracefully. Third-Party Integrations: Test interactions with third-party services. Bug Fixing: Implement a process to identify and log bugs efficiently. 2. Set Up the Playwright Framework: Installation: Ensure Playwright is installed and configured. bash Copy code npm install playwright 3. Structure the Test Framework: Test Suites: Organize tests into suites (e.g., smoke tests, regression tests, integration tests). Page Object Model (POM): Use POM to manage page elements and actions. Configuration: Set up configurations for different envir...

Mock server

 Using a mock server in end-to-end (E2E) testing is indeed common, and it can be used for more than just reading values. Mock servers simulate the behavior of real servers and can handle various HTTP methods, including POST , PUT , DELETE , and PATCH , in addition to GET . This means you can use a mock server to simulate interactions that involve writing data, such as filling out and submitting forms. Automating Form Interactions with Mock Servers Simulating Form Submissions : Mock servers can be set up to handle form submissions. When you submit a form in your E2E test, the mock server can capture the request and respond with a predefined response. Validating Data : You can use the mock server to validate the data sent by your application. The mock server can check if the request body matches the expected format and values. Response Handling : After submitting a form, your application typically processes the server's response. The mock server can provide specific responses to test...

Control test order​

  Playwright Test runs tests from a single file in the order of declaration, unless you  parallelize tests in a single file . There is no guarantee about the order of test execution across the files, because Playwright Test runs test files in parallel by default. However, if you  disable parallelism , you can control test order by either naming your files in alphabetical order or using a "test list" file. Sort test files alphabetically ​ When you  disable parallel test execution , Playwright Test runs test files in alphabetical order. You can use some naming convention to control the test order, for example  001-user-signin-flow.spec.ts ,  002-create-new-document.spec.ts  and so on.

Fixtures

 In Playwright, a fixture refers to a predefined setup or configuration that provides the context for your tests. Fixtures are used to define the environment in which tests run, ensuring that necessary prerequisites are in place before the execution of each test or suite of tests. Fixtures can include actions like launching a browser, setting up test data, initializing objects, or any other setup needed to ensure consistent and reliable test execution. Key Concepts of Fixtures in Playwright Setup and Teardown : Fixtures handle the setup and teardown processes, making sure that the environment is correctly prepared before a test runs and cleaned up afterward. Isolation : Fixtures help isolate tests from each other, ensuring that tests do not interfere with one another by sharing the same state or data. Reusable Configurations : Fixtures allow for the reuse of common setups across multiple tests, reducing redundancy and making the test code cleaner and more maintainable. Normally, i...

Timeout

  How Timeout Values Are Applied Global Timeout in Config : The timeout value in playwright.config.ts sets a default timeout for all expect assertions if no specific timeout is provided in the test script. Local Timeout in Tests : If a specific timeout is provided in a test script, it overrides the global timeout for that particular step or assertion. Determining the Actual Timeout Global Timeout Only : If you only set the global timeout in the playwright.config.ts file, the timeout for each expect assertion will be the global timeout value (e.g., 5000 milliseconds in the example). Local Timeout Only : If you set a timeout for a specific step in your test script, that value will be used for that particular step (e.g., 10000 milliseconds in the example). Both Global and Local Timeout : When both global and local timeouts are set, the local timeout takes precedence for the specific step or assertion. The global timeout will apply to other steps or assertions without a specific t...

Selenium vs Playwright

 Selenium and Playwright are both popular tools for automating web browser interactions, but they have distinct features and capabilities. Here’s a comparison between Selenium and Playwright across several key aspects: 1. Browser Support Selenium : Supports a wide range of browsers including Chrome, Firefox, Safari, Edge, and others. Each browser requires its own driver (e.g., ChromeDriver for Chrome). Playwright : Initially developed by Microsoft, Playwright supports Chromium, Firefox, and WebKit (Safari). It provides consistent API across all browsers without needing separate drivers. 2. Language Support Selenium : Supports multiple programming languages such as Java, Python, JavaScript (Node.js), C#, Ruby, and others through Selenium WebDriver bindings. Playwright : Provides native support for JavaScript (Node.js), TypeScript, Python, and C#. It offers a clean and modern API for each language. 3. API and Features Selenium : Provides a mature and widely adopted API (WebDriver) fo...