question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Add ability of taking screenshot after test.step is finished

See original GitHub issue

Would great to have a way of tacking screenshot when the test.step function is finished, so that it is visible on Allure report. Maybe some optional parameter, or maybe some step object passed to the callback that have screenshot function, like

test.step('do stuff',() => {
  step.screenshot('some name here')
}, {screenshot: true});

or

test.step('do stuff', step => {
  step.screenshot('some name here')
});

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
tymfearcommented, Oct 18, 2021

Also came with the following fixture for taking screenshots (maybe someone will find it useful)

export interface ScreenshotFixture {
  takeScreenshot: (name: string) => Promise<void>;
}

const test: TestType<PlaywrightTestArgs & ScreenshotFixture, PlaywrightWorkerArgs & PlaywrightWorkerOptions> =
  base.extend<ScreenshotFixture>({
    takeScreenshot: ({ page }, use, testInfo) => {
      const takeScreenshotFn = async (name: string) => {
        const path = testInfo.outputPath(`${name}.png`);

        await page.screenshot({ path });
        
        testInfo.attachments.push({
          name,
          path,
          contentType: 'image/png',
        });
      };

      return use(takeScreenshotFn);
    },
  });

export default test;

and usage

test('take screenshot', async ({
   takeScreenshot,
  }: ScreenshotFixture) => {
    await takeScreenshot('some name goes here');
    await test.step('Take screenshot in step', async () => {
      await takeScreenshot('some name goes here too');
    });
  });
2reactions
tymfearcommented, Oct 14, 2021

Actually I figured it out. You can use testInfo object to do that. Kind of

test.step('do stuff' => {
  const path = testInfo.outputPath('screenshot.png');
  await page.screenshot({ path });
  testInfo.attachments.push({ name: 'step name', path, contentType: 'image/png' });
});

But it attaches screenshot to the overall result (in Allure at least) so could be a bit more convenient to be able to attach to the step. Also would be a bit less of code if the testInfo object was available in the step callback, otherwise in custom fixtures I have to provide test info as a fixture. My fixture example

const test = base.extend<{
  securityEventsPage: SecurityEventsPage;
  userData: Credentials & AccountUuid;
  testInfo: TestInfo;
}>({
  userData: {} as Credentials & AccountUuid,
  
  testInfo: ({}, use, testInfo) => use(testInfo),
  
  page: ({ page }, use) => use(page),

  securityEventsPage: async ({ page, userData }, use) => {
    const loginPage = await LoginPage.visit(page);
    await loginPage.login(userData);

    const securityEventsPage = await SecurityEventsPage.visit(page, userData.accountUuid);

    await use(securityEventsPage);
  },
});

export default test;

and test

test(
    'some title',
    async ({
      securityEventsPage,
      testInfo,
      page,
    }: {
      securityEventsPage: SecurityEventsPage;
      testInfo: TestInfo;
      page: Page;
    }) => {

      await test.step('Verify if security events page is displayed', async () => {
        await securityEventsPage.isDisplayed();
       
       const path = testInfo.outputPath('screenshot.png');
        await page.screenshot({ path });
        testInfo.attachments.push({
          name: 'Verify if security events page is displayed',
          path,
          contentType: 'image/png',
        });
      });

    },
  );
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to take Screenshot in Selenium WebDriver - BrowserStack
An easy way to do this would be to use TestNG annotations. Here are the steps to capture a screenshot in Selenium in...
Read more >
How to capture a screenshot after each step in tests with JAVA ...
Solved this using Aspects. Was pretty tricky, note the annotation: @After("call(public * cucumber.runtime.StepDefinitionMatch.runStep(.
Read more >
How to add screenshot at each step in the same test case?
I am using TestNG framework and I have a test case where I am taking screenshot at each step and pushing to report....
Read more >
Managing automatic test screenshot settings
Capturing many screenshots can impair test performance. You can control which types of screenshots the system captures to minimize this ...
Read more >
How to Take a Screenshot in Selenium - Testim.io
To take a screenshot, you should use the TakesScreenshot method. This method notifies the Selenium WebDriver to capture the screenshot.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found