Search code examples
unit-testingautomated-testsplaywright

Is there a way to stop running of Before Hooks in Playwright


I am writing testcases for an Organization which only uses Google for Authentication. But we can't automate Google Authentication with Playwright. So I am trying to Build a Test which runs a Browser which User Opened and Authenticated in it. So after that he can run the Tests.

Thus I thought of designing a Testcase in Typescript where I intend to Open a Browser Log In and Then I want to run the Test Case on the Same Browser. But, due to the Before Hooks the Tab in which I logged in is reloaded.

Consider the Below Example where I open a Playwright Browser and Navigate to Google Manually, then I write a Test Case which Searches for Apple by Typing Apple in the Search Bar which was previously loaded manually. I have written a Test Case for Searching but it always fails as the Tab is reloaded before the execution of actual test body.

import { test, expect } from '@playwright/test';

test('LinkedinLoginTestCase', async ({ page }) => {
  await page.getByLabel('Search', { exact: true }).click();
  await page.getByLabel('Search', { exact: true }).fill('Apple');
  await page.getByRole('link', { name: 'Apple - Official Site apple.' }).click();
});

Solution

  • Your problem can be solved if you open a browser which has profile and login is not required.Like it happen when you open a website in your own browser.

    Check the link in the last explaining the same in detail

    Same can be achieved if you open the browser loading a particular profile

    browser = playwright.chromium.connect_over_cdp("http://localhost:9222")
    

    Before above you can lunach the browser like

    C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
    

    Source :- -Though this answer is for python approach remains the same even if you do the same thing in selenium