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