DEV Community

Cover image for Lighthouse Score for Pages behind Authentication with Selenium
Vishnuvardhan S
Vishnuvardhan S

Posted on

Lighthouse Score for Pages behind Authentication with Selenium

Default runs of Lighthouse load a page as a "new user". It needs someone to prepare the pages that are behind authentication to analyze the performance.

The Lighthouse team provides a way to achieve this via puppeteer. However, this article explains how it can be achieved with Selenium.

The main idea is to create a WebDriver instance with a remote-debugging-port and then make Lighthouse target the same port to measure the performance

How to?

  • Initialize a driver with remote-debugging-port and keep it alive.
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.edge.service import Service as EdgeService

edge_options = Options()
edge_options.add_experimental_option('detach', True)
edge_options.add_argument('--remote-debugging-port=9000')

driver = webdriver.Edge(service=EdgeService(
    EdgeChromiumDriverManager().install()), options=edge_options)
Enter fullscreen mode Exit fullscreen mode
  • Now use selenium to login to your website. For this article, facebook is taken as an example.
driver.get('https://www.facebook.com')

emailInput = driver.find_element(By.XPATH, '//input[@id="email"]')
passInput = driver.find_element(By.XPATH, '//input[@id="pass"]')

email = os.getenv('email')
password = os.getenv('password')

emailInput.send_keys(email)
passInput.send_keys(password)

submitButton = driver.find_element(
    By.XPATH, '//button[@type="submit" and @name="login"]')

submitButton.click()
Enter fullscreen mode Exit fullscreen mode

Login

  • Now all pages behind authentication should work with Lighthouse. The catch here is the port. It targets the same port in which the WebDriver was exposed with Selenium.
lighthouse "https://www.facebook.com" 
--port=9000 
--preset=desktop 
--output-path='./report.html' 
--view
Enter fullscreen mode Exit fullscreen mode

Lighthouse

Code:

  1. The script used

References:

  1. Lighthouse Documentation

Top comments (0)