github.com/hernad/nomad@v1.6.112/scripts/screenshots/src/utils.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 async function error(browser, message = "Something went wrong.") { 7 console.error(message); 8 await browser.close(); 9 process.exit(); 10 } 11 12 async function capture(page, name, options = {}) { 13 console.log(`Capturing ${name}`); 14 const dir = process.env.SCREENSHOTS_DIR || "screenshots"; 15 await page.screenshot( 16 Object.assign({ path: `${dir}/${name}.png`, fullPage: true }, options) 17 ); 18 } 19 20 async function wait(time) { 21 return new Promise(resolve => { 22 setTimeout(resolve, time); 23 }); 24 } 25 26 async function click(page, selector, options) { 27 const [response] = await Promise.all([ 28 page.waitForNavigation(), 29 page.click(selector, options) 30 ]); 31 32 // Allow for render 33 await wait(500); 34 35 return response; 36 } 37 38 async function clickX(page, path) { 39 const [element] = await page.$x(path); 40 const [response] = await Promise.all([ 41 page.waitForNavigation(), 42 element.click() 43 ]); 44 45 // Allow for render 46 await wait(500); 47 48 return response; 49 } 50 51 async function clickJob(page, type) { 52 let jobIndex = await page.$$eval( 53 "tr.job-row", 54 (rows, type) => 55 rows.findIndex( 56 row => row.querySelector("td:nth-child(3)").textContent.trim() === type 57 ), 58 type 59 ); 60 jobIndex++; 61 62 await clickX(page, `//tr[contains(@class, "job-row")][${jobIndex}]`); 63 } 64 65 async function clickTab(page, label) { 66 let tabIndex = await page.$$eval( 67 ".tabs.is-subnav a", 68 (tabs, label) => tabs.findIndex(tab => tab.textContent.trim() === label), 69 label 70 ); 71 tabIndex++; 72 73 await clickX( 74 page, 75 `//div[contains(@class, "is-subnav")]//ul//li[${tabIndex}]//a` 76 ); 77 } 78 79 async function clickMainMenu(page, label) { 80 await clickX( 81 page, 82 `//div[contains(@class, "page-column is-left")]//a[contains(text(), "${label}")]` 83 ); 84 } 85 86 module.exports = { 87 error, 88 capture, 89 wait, 90 click, 91 clickX, 92 clickJob, 93 clickTab, 94 clickMainMenu 95 };