Skip to content

Instantly share code, notes, and snippets.

@remydavid
Last active June 27, 2019 01:13
Show Gist options
  • Save remydavid/d2c3b9b4d40a0c1757fec16beaf76974 to your computer and use it in GitHub Desktop.
Save remydavid/d2c3b9b4d40a0c1757fec16beaf76974 to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
(() => {
const urls = [
'https://datastudio.google.com/u/0/reporting/XXX' // url for report 1
'https://datastudio.google.com/u/0/reporting/YYY' // url for report 2
// ...
];
// helper function to check if a CSS element is present on the page
const check = async(element, page) => (await page.$(element) !== null);
// the CSS selector for the Data Studio refresh button
const refreshButtonSelector = '#reporting-app-header div.header-button-icon.refreshIcon';
async function init(){
return puppeteer.launch({
ignoreHTTPSErrors: true,
//headless: false, //uncomment for debugging with real Chrome on local machine
slowMo: 250, // adds some throttling so events are not executed too fast
args: ['--no-sandbox', '--disable-setuid-sandbox'], // flags needed for Google SSO to work
userDataDir: './userDataDir' // directory containing user data (cookies, etc.)
});
};
async function refreshReports(logger,browser) {
var refreshPromise = new Promise( async (resolve, reject) => {
var hasError = false;
for(let url of urls){
try {
var page = await browser.newPage();
await page.goto(url,{waitUntil:['load','networkidle2']});
if(check(refreshButtonSelector,page)){
await page.click(refreshButtonSelector);
logger.info('refresh button clicked for page ' + url);
await page.close();
}else{
hasError = true;
logger.error('refresh button not found for page ' + url);
}
}catch(err){
hasError = true;
logger.error('could not refresh page ' + url + '\n' + err.message);
}
}
if(hasError){ reject(); }
else{ resolve(); }
});
return refreshPromise;
}
module.exports = {
init: init,
refreshReports : refreshReports
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment