Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running Lighthouse and Puppeteer on Headless Chrome on RHLS (breaks on PC, works on VM) #5518

Closed
29rayb opened this issue Jun 18, 2018 · 12 comments

Comments

@29rayb
Copy link

29rayb commented Jun 18, 2018

I am using this example.

This works locally on my MacOSX.
This works on my virtual machine (RHLS 7.4)
This breaks on my PC (RHLS 7.4)

All have the same versions.

lighthouse: 3.0.0-alpha.2
"puppeteer": "^1.5.0",

Running via CLI works fine in all three environments:
lighthouse --chrome-flags='--headless --no-sandbox' http://www.google.com but breaks when running it like this:

    "lp:ci": "rm -rf lighthouse-reports/*.html pally-reports/*.html && start-server-and-test l:start http://localhost:8888 lighthouse-jenkins-test",
    "lighthouse-jenkins-test": "node puplighthouse.js",

The part that breaks (in the PC) is here:

  const {lhr} = await lighthouse(HOME_PAGE, {
    port: (new URL(browser.wsEndpoint())).port,
    output: 'json',
    logLevel: 'info'
  });

Note: this works flawlessly on both MacOS and in the VM (RHLS 7.4) but only breaks in the PC (RHLS 7.4).

The output error:

Mon, 18 Jun 2018 22:53:14 GMT status Initializing…
Post stage
15:53:15 [Lighthouse & Pa11y Tests] Mon, 18 Jun 2018 22:53:15 GMT status Disconnecting from browser...
15:53:15 [Lighthouse & Pa11y Tests] Unhandled Rejection at: Promise Promise {
15:53:15 [Lighthouse & Pa11y Tests]   <rejected> Error: You probably have multiple tabs open to the same origin.
15:53:15 [Lighthouse & Pa11y Tests]     at versions.forEach.ver (/home/jenkins/workspace/or_Pipeline_ray9656_jenkins-K4TELRXZ2GGXTCMJMLMVULSDKD4IGNTMJLVG7MI2QWX3S5ZZL4BQ/builder/node_modules/lighthouse/lighthouse-core/gather/driver.js:354:21)
15:53:15 [Lighthouse & Pa11y Tests]     at Array.forEach (<anonymous>)
15:53:15 [Lighthouse & Pa11y Tests]     at registrations.filter.forEach.reg (/home/jenkins/workspace/or_Pipeline_ray9656_jenkins-K4TELRXZ2GGXTCMJMLMVULSDKD4IGNTMJLVG7MI2QWX3S5ZZL4BQ/builder/node_modules/lighthouse/lighthouse-core/gather/driver.js:346:20)
15:53:15 [Lighthouse & Pa11y Tests]     at Array.forEach (<anonymous>)
15:53:15 [Lighthouse & Pa11y Tests]     at getServiceWorkerRegistrations.then.then.then.then._ (/home/jenkins/workspace/or_Pipeline_ray9656_jenkins-K4TELRXZ2GGXTCMJMLMVULSDKD4IGNTMJLVG7MI2QWX3S5ZZL4BQ/builder/node_modules/lighthouse/lighthouse-core/gather/driver.js:345:10)
15:53:15 [Lighthouse & Pa11y Tests]     at <anonymous>
15:53:15 [Lighthouse & Pa11y Tests]     at process._tickCallback (internal/process/next_tick.js:188:7) }
15:53:15 [Lighthouse & Pa11y Tests] || reason: Error: You probably have multiple tabs open to the same origin.
15:53:15 [Lighthouse & Pa11y Tests]     at versions.forEach.ver (/home/jenkins/workspace/or_Pipeline_ray9656_jenkins-K4TELRXZ2GGXTCMJMLMVULSDKD4IGNTMJLVG7MI2QWX3S5ZZL4BQ/builder/node_modules/lighthouse/lighthouse-core/gather/driver.js:354:21)
15:53:15 [Lighthouse & Pa11y Tests]     at Array.forEach (<anonymous>)
15:53:15 [Lighthouse & Pa11y Tests]     at registrations.filter.forEach.reg (/home/jenkins/workspace/or_Pipeline_ray9656_jenkins-K4TELRXZ2GGXTCMJMLMVULSDKD4IGNTMJLVG7MI2QWX3S5ZZL4BQ/builder/node_modules/lighthouse/lighthouse-core/gather/driver.js:346:20)
15:53:15 [Lighthouse & Pa11y Tests]     at Array.forEach (<anonymous>)
15:53:15 [Lighthouse & Pa11y Tests]     at getServiceWorkerRegistrations.then.then.then.then._ (/home/jenkins/workspace/or_Pipeline_ray9656_jenkins-K4TELRXZ2GGXTCMJMLMVULSDKD4IGNTMJLVG7MI2QWX3S5ZZL4BQ/builder/node_modules/lighthouse/lighthouse-core/gather/driver.js:345:10)
15:53:15 [Lighthouse & Pa11y Tests]     at <anonymous>
15:53:15 [Lighthouse & Pa11y Tests]     at process._tickCallback (internal/process/next_tick.js:188:7)
15:53:15 [Lighthouse & Pa11y Tests] events.js:183
15:53:15 [Lighthouse & Pa11y Tests]       throw er; // Unhandled 'error' event
15:53:15 [Lighthouse & Pa11y Tests]       ^
15:53:15 [Lighthouse & Pa11y Tests] 
15:53:15 [Lighthouse & Pa11y Tests] Error: connect ECONNREFUSED 127.0.0.1:37122
15:53:15 [Lighthouse & Pa11y Tests]     at Object._errnoException (util.js:992:11)
15:53:15 [Lighthouse & Pa11y Tests]     at _exceptionWithHostPort (util.js:1014:20)
15:53:15 [Lighthouse & Pa11y Tests]     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
15:53:15 [Lighthouse & Pa11y Tests] npm ERR! code ELIFECYCLE
15:53:15 [Lighthouse & Pa11y Tests] npm ERR! errno 1
15:53:15 [Lighthouse & Pa11y Tests] npm ERR! builder@0.1.0 lighthouse-jenkins-test: `node puplighthouse.js`
15:53:15 [Lighthouse & Pa11y Tests] npm ERR! Exit status 1
15:53:15 [Lighthouse & Pa11y Tests] npm ERR! 
15:53:15 [Lighthouse & Pa11y Tests] npm ERR! Failed at the builder@0.1.0 lighthouse-jenkins-test script.
15:53:15 [Lighthouse & Pa11y Tests] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
15:53:15 [Lighthouse & Pa11y Tests] 
15:53:15 [Lighthouse & Pa11y Tests] npm ERR! A complete log of this run can be found in:
15:53:15 [Lighthouse & Pa11y Tests] npm ERR!     /var/lib/jenkins/.npm/_logs/2018-06-18T22_53_15_388Z-debug.log
15:53:15 [Lighthouse & Pa11y Tests] 
15:53:15 [Lighthouse & Pa11y Tests] INFO: Gracefully shutting down. Please wait...
15:53:15 [Lighthouse & Pa11y Tests] { Error: Command failed: npm run lighthouse-jenkins-test

What is the expected behavior?

Environment Information

  • Affected Channels:
  • Lighthouse version: 3.0.0-alpha.2
  • Node.js version: v8.11.3 (on PC and VM running on RHLS 7.4) and v10.1.0 (MacOS)

Related issues
#3024

  • you have more than one tab open to the same origin: this happens when i run with headless: false locally on my MacOS but produces all the outputs without any error. this is also the case with the jenkins running on the VM (which works) and also the case for jenkins running on the PC (which does NOT work)
  • You have another tab open that has an iframe to the same origin.: unrelated
  • The target page has an iframe that is also controlled by the service worker: unrelated
  • The target page has a link[rel=prerender] element to a page controlled by the same service worker: unrelated

How do I go about making sure I have all the necessary dependencies for running lighthouse/pup on RHLS 7.4. I don't have any experience with Linux nor VMs and think the error is due to some linux configs...

@devtools-bot
Copy link

Howdy chief! Appreciate you filing this bug. 👏

This is a known issue that is best described in #3024. So, we'll automatically close this as a duplicate.

However, if you believe your bug is different than the cases described there, please comment here with "necessarily-wide-alpaca" and I'll reopen this bug. 🤖 Beep beep boop.

@29rayb
Copy link
Author

29rayb commented Jun 18, 2018

necessarily-wide-alpaca

@patrickhulce patrickhulce reopened this Jun 19, 2018
@patrickhulce
Copy link
Collaborator

This seems like you're opening a URL that is already open in the same instance of Chrome. Are you auditing the Google? The new tab page in Chrome is actually a google-origin page controlled by a service worker. What happens when you try to audit example.com instead?

@29rayb
Copy link
Author

29rayb commented Jun 19, 2018

@patrickhulce I am trying to audit a page running on localhost:3000 and succeeding pages like localhost:3000/user/profile etc.

So in the jenkins I run the lp:ci command:

    "lp:ci": "start-server-and-test l:start http://localhost:3000 lighthouse-jenkins-test",
    "l:start": "serve -s -l 3000 build",
    "lighthouse-jenkins-test": "node puplighthouse.js",

Then I launch Puppeteer and when it tries to generate the first Lighthouse Report on localhost:3000 (with the code above), it breaks with the above error.

I've tried this with you suggestion and when I run the same code to generate the lighthouse reports on public URLs like example.com twitter.com it works flawlessly.

But what I don't get is why it won't work on the PC for localhost (I've tried changing the port to like 7000, and it still doesn't work while it works on the VM with the same specs and locally on my Mac).
I've also made sure that port 3000 isn't in use prior to me starting the server.

@patrickhulce
Copy link
Collaborator

Well that LH error only appears when you have multiple tabs open to the same origin. We can't see your code, so we can't really say much. My best guess is that you're accidentally opening more than one tab to localhost:3000 with the puppeteer script at some point. As for why it's only happening on RHLS, I'm afraid your guess is as good as mine, you know best what might be different about your environment.

@29rayb
Copy link
Author

29rayb commented Jun 19, 2018

it fails with this absolute minimal code with the same error above:

const puppeteer = require('puppeteer');
const lighthouse = require('lighthouse');
const {URL} = require('url');
const ReportGenerator = require('lighthouse/lighthouse-core/report/report-generator');
const fs = require('fs');

const HOME_PAGE = 'http://localhost:9000/';

const opts = {
  chromeFlags: ['--headless', '--no-sandbox', '--disable-gpu', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
}

async function createNewBrowser() {
  const browser = await puppeteer.launch({
    args: opts.chromeFlags
  })
  return browser;
}

async function createNewPage(browser) {
  const page = await browser.newPage();
  return page;
}


async function run() {
  const browser = await createNewBrowser();
  const page = await createNewPage(browser);

  process.on("unhandledRejection", async (reason, p) => {
    console.error("Unhandled Rejection at: Promise", p, "reason: ", reason);
    browser.close();
  });

  await page.goto(HOME_PAGE);

  await home_lighthouse(browser) 
  browser.close();
}

function generateReport(lhr){
  const htmlReportAssets = require('lighthouse/lighthouse-core/report/html/html-report-assets');

  const sanitizedJson = JSON.stringify(lhr)
    .replace(/</g, '\\u003c') // replaces opening script tags
    .replace(/\u2028/g, '\\u2028') // replaces line separators ()
    .replace(/\u2029/g, '\\u2029'); // replaces paragraph separators
  const sanitizedJavascript = htmlReportAssets.REPORT_JAVASCRIPT.replace(/<\//g, '\\u003c/');

  return ReportGenerator.replaceStrings(htmlReportAssets.REPORT_TEMPLATE, [
    {search: '%%LIGHTHOUSE_JSON%%', replacement: sanitizedJson},
    {search: '%%LIGHTHOUSE_JAVASCRIPT%%', replacement: sanitizedJavascript},
    {search: '/*%%LIGHTHOUSE_CSS%%*/', replacement: htmlReportAssets.REPORT_CSS},
    {search: '%%LIGHTHOUSE_TEMPLATES%%', replacement: htmlReportAssets.REPORT_TEMPLATES},
  ]);
}

async function home_lighthouse(browser) {
  // BREAKS HERE
  const {lhr} = await lighthouse(HOME_PAGE, {
    port: (new URL(browser.wsEndpoint())).port,
    output: 'json',
    logLevel: 'info'
  });

  const lighthouse_results = generateReport(lhr);

  fs.writeFile('lighthouse-reports/lighthouse-home-report.html', lighthouse_results, 'utf8', function(err){
    if (err) return console.log(err);
    console.log('home-lighthouse file was saved');
  });
}

run();


It does work on the VM running on RHLS while it does NOT work on the PC running on the same distro and version (7.4)

switch the HOME_PAGE=http://example.com/ || https://www.google.com/ works as expected

@patrickhulce
Copy link
Collaborator

try not to navigate to the page with puppeteer before running LH, also FYI you should be able to greatly simplify some of this.

async function run() {
  const browser = await puppeteer.launch({args: opts.chromeFlags});

  process.on("unhandledRejection", async (reason, p) => {
    console.error("Unhandled Rejection at: Promise", p, "reason: ", reason);
    browser.close();
  });

  const {report} = await lighthouse(HOME_PAGE, {
    port: (new URL(browser.wsEndpoint())).port,
    output: 'html',
    logLevel: 'info'
  });

  fs.writeFileSync('./report.html', report)
  browser.close();
}

@brendankenny
Copy link
Member

Closing for now. If you're still seeing this issue please comment and we can reopen!

@Rameshbodh
Copy link

Hi,
Can any one please guid me through how to run puppeteer script using jenkins
So far I have created sample project in jenkins. I want my sample project to run puppest.js script
I'm running it in my local machine using CLI and its working fine. I need to run it using jenkins.
Please help.

@Nick-Zag
Copy link

I have the same issue, I haven't control over Jenkins jobs, just can to add scripts to pipeline. And now I want to run Lighthouse performance check on pipeline, but have such error
"Runtime error encountered: The environment variable CHROME_PATH must be set to executable of a build of Chromium version 54.0 or later."
How I can run lighthouse on pipeline?

@Rameshbodh
Copy link

Rameshbodh commented Nov 1, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants