Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

page.evaluate: Browser has been closed #21

Open
bradydowling opened this issue Apr 22, 2022 · 6 comments
Open

page.evaluate: Browser has been closed #21

bradydowling opened this issue Apr 22, 2022 · 6 comments

Comments

@bradydowling
Copy link

When using the code specified in fixtures.js, this line fails with the following error:

page.evaluate: Browser has been closed

I'm running with "@playwright/test": "~1.20.2". As a workaround, I'm making a direct PUT call to BrowserStack as such:

      if (testInfo.status === 'failed') {
        await axios({
          method: 'put',
          url: `https://api.browserstack.com/automate/sessions/${sessionId}.json`,
          data: payloadArguments,
          auth: {
            username: process.env.BROWSERSTACK_USERNAME,
            password: process.env.BROWSERSTACK_ACCESS_KEY
          },
        });
      }
      else {
        await newPage.evaluate(() => {}, `browserstack_executor: ${browserStackCommandPayloadString}`);
      }

Is there something I need to do to prevent the browser from closing?

@bradydowling
Copy link
Author

This used to be only happening when the test failed but now it seems to be happening when it succeeds as well. Not really sure where to go from here.

@bradydowling
Copy link
Author

Here is my Playwright config file that I'm using:

import { PlaywrightTestConfig } from '@playwright/test';
import { WorkerOptions } from './fixtures';

const config: PlaywrightTestConfig<{}, WorkerOptions> = {
  testDir: 'tests',
  testMatch: '**/*.test.ts',
  retries: 4,
  timeout: 5 * 60 * 1000, // timeout 5 minute --- default = 30 sec
  globalSetup: './globalSetup.ts',
  use: {
    browserstack: true,
    trace: 'on-first-retry',
    viewport: { width: 1600, height: 1200 },
    baseURL: 'https://mysite.com'
  },
  projects: [
    {
      name: 'Chromium (BrowserStack)'
    }
  ]
};
export default config;

With my globalSetup file just being empty:

export default async () => {
  console.log('Starting BrowserStack connection...');
};

@bradydowling
Copy link
Author

This issue seems to be restricted to Chromium, as it works with Firefox. I'll also try with Webkit to see if that works and then I'll update here.

@bradydowling
Copy link
Author

Looks like this repo isn't maintained anymore but I'll post updates here anyway, in hopes that it'll help someone else.

Seems to be working with Chromium for some of my coworkers and it occasionally works with Chromium for me but mostly fails. This is blocking me.

@asambstack
Copy link
Contributor

Hey @bradydowling!
Seems to be a config fix as I tried the sample script and it seems to be working fine for me.
If you don't mind, could you show me how you're forming the caps that are being passed to browserstack? It would be helpful if you could provide me with the fixture.js file as well.

@bradydowling
Copy link
Author

@asambstack hopefully this helps out! Let me know if you think it could be anything else.

import { chromium, test as baseTest, Page, TestInfo } from '@playwright/test';
import * as cp from 'child_process';
import axios from 'axios';

export interface WorkerOptions {
  browserstack?: boolean;
}

const clientPlaywrightVersion = cp
  .execSync('npx playwright --version')
  .toString()
  .trim()
  .split(' ')[1];

async function launchBrowser(caps: Record<string, string>) {
  const browser = await chromium.connect({
    wsEndpoint: `wss://cdp.browserstack.com/playwright?caps=${encodeURIComponent(
      JSON.stringify(caps)
    )}`
  });

  return browser;
}

const getCaps = (name: string) => {
  let buildName;
  if (process.env.CI_MERGE_REQUEST_ID) {
    buildName = `my-project/!${process.env.CI_MERGE_REQUEST_ID}`;
  } else if (process.env.CI_COMMIT_BRANCH) {
    buildName = `my-project/${process.env.CI_COMMIT_BRANCH}`;
  } else {
    const username =
      process.env.HOSTNAME ??
      process.env.USER ??
      process.env.BROWSERSTACK_USERNAME ??
      'unknown user';
    buildName = `my project local (${username} ${process.env.TIME})`;
  }

  return {
    project: 'My Project',
    name,
    build: buildName,
    browser: 'playwright-chromium',
    browser_version: 'latest',
    os: 'os x',
    os_version: 'big sur',
    'browserstack.username': process.env.BROWSERSTACK_USERNAME!,
    'browserstack.accessKey': process.env.BROWSERSTACK_ACCESS_KEY!,
    'client.playwrightVersion': clientPlaywrightVersion
  };
};

const isHash = (entity: any): boolean =>
  Boolean(entity && typeof entity === 'object' && !Array.isArray(entity));
const getTestResultReason = (hash: any, keys: string[]): string => {
  return (
    keys.reduce((hash, key) => (isHash(hash) ? hash[key] : undefined), hash) ||
    ''
  );
};
const removeUnicodeColors = (reason: string): string =>
  reason.replace(/\u001b\[\d+m/g, '');

const sendBrowserStackInfo = async (
  page: Page,
  testInfo: TestInfo,
  sessionId: string
) => {
  const testResultReason = removeUnicodeColors(
    getTestResultReason(testInfo, ['error', 'message'])
  );
  const payloadArguments = {
    status: testInfo.status,
    reason: testResultReason
  };

  // Sometimes shows `page.evaluate: Browser has been closed` on failure so we need to talk to BrowserStack without page.evaluate
  if (testInfo.status === 'failed') {
    await axios({
      method: 'put',
      url: `https://api.browserstack.com/automate/sessions/${sessionId}.json`,
      data: payloadArguments,
      auth: {
        username: process.env.BROWSERSTACK_USERNAME,
        password: process.env.BROWSERSTACK_ACCESS_KEY
      }
    });
    return;
  }

  const browserStackCommandPayloadString = JSON.stringify({
    action: 'setSessionStatus',
    arguments: payloadArguments
  });
  await page.evaluate(() => {},
  `browserstack_executor: ${browserStackCommandPayloadString}`);
};

export const test = baseTest.extend<{}, WorkerOptions>({
  browserstack: [false, { option: true, scope: 'worker' }],
  context: async ({ context }, use) => {
    await use(context);
  },
  page: async ({ page, browserstack }, use, testInfo) => {
    if (browserstack) {
      console.log('Connecting to BrowserStack...\n');
      const browser = await launchBrowser(
        getCaps(`${testInfo.titlePath[0]}: ${testInfo.title}`)
      );
      const newPage = await browser.newPage(testInfo.project.use);
      const resp = await JSON.parse(
        await newPage.evaluate(_ => {},
        `browserstack_executor: ${JSON.stringify({ action: 'getSessionDetails' })}`)
      );
      const sessionId = resp.hashed_id;

      await use(newPage);
      await sendBrowserStackInfo(newPage, testInfo, sessionId);
      await newPage.close();
      await browser.close();
    } else {
      use(page);
    }
  }
});

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

No branches or pull requests

2 participants