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

Run a lighthouse test against the current browser session #395

Open
M-Z opened this issue Jan 19, 2021 · 6 comments
Open

Run a lighthouse test against the current browser session #395

M-Z opened this issue Jan 19, 2021 · 6 comments

Comments

@M-Z
Copy link

M-Z commented Jan 19, 2021

I can see the comment "Run a lighthouse test against the current browser session"
But, after launching the agent with the browser GUI, I can see the browser closes and a new one spawns just for LH. Which loses the cookies/storage/etc. for my tests.
I applied a quick fix (don't know if this is the right approach here) that might help

wptagent.py
L#129

self.task['running_lighthouse'] = False
     if self.task['run'] == 1 and not self.task['cached'] and \
         self.job['warmup'] <= 0 and \
         self.task['error'] is None and \
         'lighthouse' in self.job and self.job['lighthouse']:
                 if 'page_result' not in self.task or \
                     self.task['page_result'] is None or \
                     self.task['page_result'] == 0 or \
                     self.task['page_result'] == 99999:
                         self.task['running_lighthouse'] = True
              self.run_single_test()
              self.wpt.get_bodies(self.task)
              elapsed = monotonic() - start
              logging.debug('Test run time: %0.3f sec', elapsed)

L#191

if browser is not None:
            browser.prepare(self.job, self.task)
            browser.launch(self.job, self.task)
            try:
                browser.run_task(self.task)

                if self.task['running_lighthouse']:
                    self.task['lighthouse_log'] = \
                        'Lighthouse testing is not supported with this browser.'
                    try:
                        browser.run_lighthouse_test(self.task)
                    except Exception:
                        logging.exception('Error running lighthouse test')
                    if self.task['lighthouse_log']:
                        try:
                            log_file = os.path.join(self.task['dir'], 'lighthouse.log.gz')
                            with gzip.open(log_file, GZIP_TEXT, 7) as f_out:
                                f_out.write(self.task['lighthouse_log'])
                        except Exception:
                            logging.exception('Error compressing lighthouse log')

devtools_browser.py
L#54
Removed the ret = self.devtools.wait_for_available(self.CONNECT_TIME_LIMIT)
and made it just connect once to the dev tools interface and not wait/reconnect to the devtools
L#267

if not self.task['running_lighthouse']:
        self.task = None
@pmeenan
Copy link
Contributor

pmeenan commented Jan 19, 2021

I don't understand exactly what you are trying to do. You can't re-use a browser session across a WPT test and a Lighthouse test.

Lighthouse makes 3-4 passes across a page measuring different things and is expecting to be in full control and starting from a clean slate. If you need to specify additional headers or cookies it needs to be done explicitly.

@M-Z
Copy link
Author

M-Z commented Jan 19, 2021

I'm trying to access a page behind auth. For WPT, setCookie is enough. But for LH, the page gets redirected to the login page probably due to some JS code.
Currently, I tried two approaches that worked to some extent:

  1. (Explained above) Is to keep the browser open (with all cookies/data set already) and run LH on a new tab.
  2. Tried sending command Network.setCookie in run_lighthouse_test but the devtools weren't connecting (due to the new browser window for LH?). So I had to remove self.devtools.wait_for_available(self.CONNECT_TIME_LIMIT) and connect to a new Devtools instance to inject cookies.

Is there another "right" way to inject cookies into the browser for LH to work with WPT other than --extra-headers flag?

Thanks for your time!

@pmeenan
Copy link
Contributor

pmeenan commented Jan 19, 2021

The easiest way with lighthouse is to set the cookies as explicit HTTP headers (custom headers in the advanced tab "Cookie: xxxxx"). Lighthouse supports the additional headers.

@M-Z
Copy link
Author

M-Z commented Jan 22, 2021

This doesn't work for me in both WPT and LH. So I was trying to do something like setCookie for LH as well.

@imkalvin
Copy link

imkalvin commented Jul 6, 2021

Hi @pmeenan & @M-Z ,

I managed to get this work with #420

Could you help to take a look whether it meets the overall design of WPT+LH?

@M-Z
Copy link
Author

M-Z commented Jul 7, 2021

Hi @imkalvin ,

I'm currently just using a local patch for my test agents.
Injecting this snippet in devtools_browser.py after this line

if 'cookies' in self.task:
    for cookie in self.task['cookies']:
        self.devtools.send_command('Network.setCookie', cookie)

I also changed this method and removed the running_lighthouse if statement.

I have this patch running thousands of tests daily for the last 6 months and I didn't see anything going wrong for me.

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

No branches or pull requests

3 participants