Skip to content

Instantly share code, notes, and snippets.

@ajs139
Last active October 24, 2019 02:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajs139/3ddc10e807ee9b94b581c80a762de587 to your computer and use it in GitHub Desktop.
Save ajs139/3ddc10e807ee9b94b581c80a762de587 to your computer and use it in GitHub Desktop.
require('expect-puppeteer');
const isCI = require('is-ci');
const path = require('path');
const { mkdirp, writeFile } = require('fs-extra');
const screenshotsPath = path.resolve(__dirname, '../reports/screenshots');
const toFilename = s => s.replace(/[^a-z0-9.-]+/gi, '_');
const saveScreenshot = async (screenshot, testName) => {
await mkdirp(screenshotsPath);
const fileName = toFilename(`${new Date().toISOString()}_${testName}_screenshot.png`);
const filePath = path.join(screenshotsPath, fileName);
await writeFile(filePath, screenshot);
return filePath;
};
let currentTest = '';
let currentScreenshot = null;
if (isCI) {
jasmine.getEnv().addReporter({
specStarted: ({ fullName }) => {
currentTest = fullName;
currentScreenshot = null;
},
specDone: async ({ status, fullName }) => {
if (status === 'failed') {
if(currentScreenshot) {
try {
const filePath = await saveScreenshot(currentScreenshot, currentTest);
console.error(`FAILED ${fullName}: screenshot @ ${filePath}`);
} catch (e) {
console.error(`FAILED ${fullName}: could not save screenshot.`, e);
}
} else {
console.error(`FAILED ${fullName}: sadly, no screenshot could be taken.`);
}
}
},
});
afterEach(async () => {
currentScreenshot = await page.screenshot();
});
} else {
jest.setTimeout(120e3);
}
@cbodavula
Copy link

Hello ajs139,

While I am running multiple test suites, The script throws an error at line#33
console.error(FAILED ${fullName}: screenshot @ ${filePath});

Cannot log after tests are done. Did you forget to wait for something async in your test?

If you have any idea, please help me.

@ajs139
Copy link
Author

ajs139 commented Jun 4, 2019

It's strange that it's behaving differently for you. You could try replacing console.error with process.stderr.write:

      if (status === 'failed') {
        if(currentScreenshot) {
          try {
            const filePath = await saveScreenshot(currentScreenshot, currentTest);
            process.stderr.write(`FAILED ${fullName}: screenshot @ ${filePath}\n`);
          } catch (e) {
            process.stderr.write(`FAILED ${fullName}: could not save screenshot.\n${e.message}\n${e.stack}\n`);
          }
        } else {
          process.stderr.write(`FAILED ${fullName}: sadly, no screenshot could be taken.\n`);
        }
      }

@cbodavula
Copy link

Thank you, the above logic worked for me
I am getting the following error by using "done" in the code and also it is not taking a screenshot.

test('google page', async (done) => {
      await page.goto('https://www.google.co.in/imghp?hl=en&tab=ri&authuser=0&ogbl');
      await page.setViewport(AUT.deskTopViewPoint);
      expect(1).to.equal(0);
      done();
    }, timeout);
(node:2436) UnhandledPromiseRejectionWarning: Error: Caught error after test environment was torn down

Protocol error (Target.activateTarget): Target closed.
(node:2436) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:2436) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

In after each, I have added the console, only I am getting the first console(i.e in after each)

afterEach(async () => {
  console.log('-----------------in after each -------------------------------');
  currentScreenshot = await webpage.screenshot();
  console.log('-----------------end after each -------------------------------');
});

Please let me know if you have any solution. Thank you.

@ajs139
Copy link
Author

ajs139 commented Jun 6, 2019

@chandana-96, can you try this:

  test('google page', async done => {
    try {
      await page.goto('https://www.google.co.in/imghp?hl=en&tab=ri&authuser=0&ogbl');
      expect(1).to.equal(0);
      done();
    } catch (e) {
      done.fail(e.message);
    }
  }, timeout);

@cbodavula
Copy link

@ajs139, Thank you, it is working

@cbodavula
Copy link

@ajs139 is it possible to use jest- circus test runner instead of jasmine? If you have any idea please help me.

@ajs139
Copy link
Author

ajs139 commented Oct 24, 2019

@chandana-b - haven't had the chance to experiment with Jest Circus yet, hope you managed to find a solution.

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