Skip to content

Fix: Firefox browser not launching in BeforeAll hook#20

Open
TestRoverAutomation wants to merge 1 commit into
vitalets:mainfrom
TestRoverAutomation:hooks
Open

Fix: Firefox browser not launching in BeforeAll hook#20
TestRoverAutomation wants to merge 1 commit into
vitalets:mainfrom
TestRoverAutomation:hooks

Conversation

@TestRoverAutomation

Copy link
Copy Markdown

Pull Request Description:
Issue Description: Currently, when using the Playwright-BDD framework with custom hooks, Firefox is not launching as expected within the BeforeAll hook. The issue arises because the browser is being initialized manually, but Chromium is still being launched by default, overriding the explicit call to Firefox.

Root Cause: The Playwright-BDD framework seems to default to Chromium if the browser is not explicitly overridden in the configuration file or in the test hook. The current behavior ignores the manual call to firefox.launch() within the BeforeAll hook.

Proposed Solution: This PR modifies the test setup to explicitly launch Firefox , webkit within the BeforeAll hook without configuring the browser in the playwright.config.ts file. The changes include:

Manually launching Firefox and Webkit with firefox.launch({ headless: false }) inside the BeforeAll hook.
Opening a new page in the Firefox browser and ensuring that the tests proceed within this context.
Ensuring that Firefox, rather than Chromium, is the browser used during the test lifecycle.

Code Changes:

`import { expect } from "@playwright/test";
import { firefox } from '@playwright/test';
import { Given, When, Then } from "./fixtures";
import { createBdd } from "playwright-bdd";

const { BeforeAll } = createBdd();

let browser, page;

BeforeAll(async function () {
try {
// Explicitly launch Firefox
browser = await firefox.launch({
headless: false, // Set to false to see the browser
});

// Open a new page in the Firefox browser
page = await browser.newPage();
console.log("Firefox browser opened:", Boolean(page));

} catch (error) {
console.log("Error launching Firefox:", error);
}
});

Given("I am on Playwright home page", async ({ page }) => {
await page.goto("https://playwright.dev");
});

When("I click link {string}", async ({ page }, name: string) => {
await page.getByRole("link", { name }).click();
});

Then("I see in title {string}", async ({ page }, text: string) => {
await expect(page).toHaveTitle(new RegExp(text));
});
`

@vitalets

Copy link
Copy Markdown
Owner

You can run Firefox as a default browser via projects configuration. In that case, you don't need to perform any setup in BeforeAll hook.

@QAPranav

Copy link
Copy Markdown

Hi Vitalets you are right but can I perform like this as we have browser instance here, so in some company we define browser setup in hooks which with switch case, so this might be for enhancement to setup the hooks

@vitalets

Copy link
Copy Markdown
Owner

Okey, understood!
Then I'll keep this PR as a reference for such approach.
Don't think we should merge it into the main example code, as it's assumed to provide a simplest template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants