github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/web/wats/helpers/web.js (about) 1 'use strict'; 2 3 const puppeteer = require('puppeteer'); 4 const { exec } = require('child-process-promise'); 5 const uuidv4 = require('uuid/v4'); 6 7 class Web { 8 constructor(url, username, password) { 9 this.url = url; 10 this.username = username; 11 this.password = password; 12 } 13 14 static async build(url, username, password) { 15 let web = new Web(url, username, password); 16 await web.init(); 17 return web; 18 } 19 20 async init() { 21 this.browser = await puppeteer.launch({ 22 //headless: false, 23 args: ['--no-sandbox', '--disable-setuid-sandbox'] 24 }); 25 26 this.page = await this.browser.newPage(); 27 //Default page navigation timeout to 90 Seconds. 28 this.page.setDefaultNavigationTimeout(90000); 29 this.page.on("console", msg => { 30 console.log(`BROWSER (${msg.type()}):`, msg.text()); 31 }); 32 } 33 34 route(path) { 35 return `${this.url}${path}`; 36 } 37 38 text(ele) { 39 return this.page.evaluate(x => (x || document.body).innerText, ele); 40 } 41 42 waitForText(text) { 43 return this.page.waitForFunction((text) => { 44 return document.body.innerText.indexOf(text) !== -1 45 }, { 46 polling: 100, 47 timeout: 90000 48 }, text) 49 } 50 51 async waitForBackgroundColor(selector, backgroundColor, {timeout = 30000} = {}) { 52 await this.page.waitFor(({expectedBackground, selector}) => { 53 const elem = document.querySelector(selector); 54 if (elem === null) return false; 55 const background = elem.style.backgroundColor; 56 return background === expectedBackground; 57 }, {timeout}, { 58 selector, 59 expectedBackground: backgroundColor.rgb().string(), 60 }); 61 } 62 63 async scrollIntoView(selector) { 64 await this.page.waitFor(selector); 65 await this.page.evaluate(selector => { 66 const elem = document.querySelector(selector); 67 elem.scrollIntoView(true); 68 }, selector); 69 } 70 71 async login(t) { 72 await this.visitLoginPage(); 73 await this.performLogin(); 74 await this.clickAndWait('#submit-login', '#user-id'); 75 t.notRegex(await this.text(), /login/); 76 } 77 78 async visitLoginPage() { 79 await this.page.goto(`${this.url}/sky/login`); 80 } 81 82 async performLogin() { 83 await this.page.waitForSelector('#login', {timeout: 90000}); 84 await this.page.type('#login', this.username); 85 await this.page.type('#password', this.password); 86 } 87 88 async clickAndWait(clickSelector, waitSelector) { 89 await this.page.waitFor(clickSelector); 90 await this.page.click(clickSelector); 91 await this.page.waitForSelector(waitSelector, {timeout: 90000}); 92 } 93 94 computedStyle(element, style) { 95 return element.executionContext().evaluate((element, style) => { 96 return window.getComputedStyle(element)[style] 97 }, element, style) 98 } 99 } 100 101 module.exports = Web;