I'm writing Playwright tests integrated with the Browserstack Automate dashboard. I have encountered a situation where if run e.g. 5 tests in a file, they will all run locally in Playwright and all results will be displayed in terminal, but the Browserstack Dashboard will always only be updated with results of 2 tests (the default number of workers for each test run is 2).
However, if I manually open (const browser = await chromium.launch(); const page = await browser.newPage();
) and close the browser in each test, the Browserstack dashboard correctly logs all test runs.
This works:
test('test1', async ({ }) => {
test.setTimeout(240000)
const browser = await chromium.launch();
const page = await browser.newPage();
try {
//test code here
await page.evaluate(_ => {}, `browserstack_executor: ${JSON.stringify({action: 'setSessionStatus',arguments: {name: '', status: 'passed',reason: ''}})}`);
} catch (e) {
console.log(e);
await page.evaluate(_ => {}, `browserstack_executor: ${JSON.stringify({action: 'setSessionStatus',arguments: {name: '', status: 'failed',reason: ''}})}`);
}
browser.close()
});
....
Given that opening and closing the browser for each individual test seems to make Browserstack log them correctly, I'm trying to write a beforeEach and afterEach hook to do this. However, this doesn't work:
test.beforeEach(async ({ }) => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto(`${url}`);
});
test('test1', async ({ page }) => {
test.setTimeout(240000)
try {
//test code here
await page.evaluate(_ => {}, `browserstack_executor: ${JSON.stringify({action: 'setSessionStatus',arguments: {name: '', status: 'passed',reason: ''}})}`);
} catch (e) {
console.log(e);
await page.evaluate(_ => {}, `browserstack_executor: ${JSON.stringify({action: 'setSessionStatus',arguments: {name: '', status: 'failed',reason: ''}})}`);
}
});
....
test.afterEach(async ({ browser }) => {
browser.close()
});
The before/afterEach hooks keep resulting in error messages about the browser being abruptly closed. Is there a better way to open/close browsers for each test? I know that other testing frameworks have a config setting for restarting the browser. Or maybe there is another way to make the test results appear in the Browserstack Automate dashboard?