From 0b698d1173c0cea899c44eb5991a68f5e8e16a6c Mon Sep 17 00:00:00 2001 From: Priyank Patel Date: Sat, 16 May 2020 17:14:31 +0000 Subject: [PATCH] puppeteer: Take screenshot on failure. To take a screenshot on failure where we have our common error handling code in common.run_test method we need to have access to the Page instance. Currently, we create a new Page in the test method we pass into run_test, so we pass in a new Page instance to that method so the test and the run_test method have access to the same Page. And, then we take a screenshot on failure. It will be saved as `failure-${num}` in var/puppeteer directory. --- frontend_tests/puppeteer_lib/common.js | 10 +++++++++- frontend_tests/puppeteer_tests/00-realm-creation.js | 4 ++-- frontend_tests/puppeteer_tests/01-login.js | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/frontend_tests/puppeteer_lib/common.js b/frontend_tests/puppeteer_lib/common.js index 15d3326819..438bb66a00 100644 --- a/frontend_tests/puppeteer_lib/common.js +++ b/frontend_tests/puppeteer_lib/common.js @@ -45,10 +45,18 @@ class CommonUtils { } async run_test(test_function) { + // Pass a page instance to test so we can take + // a screenshot of it when the test fails. + const page = await this.get_page(); try { - await test_function(); + await test_function(page); } catch (e) { console.log(e); + + // Take a screenshot, and increment the screenshot_id. + await this.screenshot(page, `failure-${this.screenshot_id}`); + this.screenshot_id += 1; + await this.browser.close(); process.exit(1); } finally { diff --git a/frontend_tests/puppeteer_tests/00-realm-creation.js b/frontend_tests/puppeteer_tests/00-realm-creation.js index 3f210db778..eaba0716ff 100644 --- a/frontend_tests/puppeteer_tests/00-realm-creation.js +++ b/frontend_tests/puppeteer_tests/00-realm-creation.js @@ -6,8 +6,8 @@ const subdomain = 'testsubdomain'; const organization_name = 'Awesome Organization'; const host = "zulipdev.com:9981"; -async function realm_creation_tests() { - const page = await common.get_page('http://' + host + '/new/'); +async function realm_creation_tests(page) { + await page.goto('http://' + host + '/new/'); // submit the email for realm creation. await page.waitForSelector('#email'); diff --git a/frontend_tests/puppeteer_tests/01-login.js b/frontend_tests/puppeteer_tests/01-login.js index d209d451ac..410d1506d7 100644 --- a/frontend_tests/puppeteer_tests/01-login.js +++ b/frontend_tests/puppeteer_tests/01-login.js @@ -22,8 +22,8 @@ async function log_out(page) { assert(page.url().includes('accounts/login/')); } -async function login_tests() { - const page = await common.get_page(realm_url + 'login/'); +async function login_tests(page) { + await page.goto(realm_url + 'login/'); await log_in(page, test_credentials.default_user); await log_out(page); }