Skip to content
This repository was archived by the owner on Apr 4, 2026. It is now read-only.

Selenium

Atip Kajitamkul edited this page Oct 13, 2024 · 9 revisions

composed of many tools to automate the test cases for the web browser including the API.

https://www.selenium.dev/documentation/overview/

Selenium Web Driver API

The driver follows the standard W3C Recommendation Dates: 05 June 2018

This API starts the process of browser controlling by creating driver sessions (initialise Driver class object) with Options to describe the browser option needed.

  • Local driver (Python)

Chrome

def test_page_load_strategy_normal():
    options = webdriver.ChromeOptions()

    options.page_load_strategy = 'normal'
    driver = webdriver.Chrome(options=options) 

    driver.get("https://www.selenium.dev/")
    driver.quit() 

Firefox

def test_set_window_rect():
    options = webdriver.FirefoxOptions()
    options.set_window_rect = True # Full support in Firefox
    driver = webdriver.Firefox(options=options)

    driver.get("https://www.selenium.dev/")
    driver.quit()
  • Remote driver (Python, Requires Selenium Grid on the end-node)
def test_start_remote(server):
    options = webdriver.ChromeOptions()
    driver = webdriver.Remote(command_executor=server, options=options)

    assert "localhost" in driver.command_executor._url
    driver.quit()

Performance metrics aspect

Selenium seem to be limited this feature by using Chrome Devtools Protocol (CDP) to develop a API for remotely debugging.

Demo

from selenium import webdriver
from selenium.webdriver.common.by import By
import trio
# from selenium.webdriver.common.devtools.v128.network import Headers
# import base64

async def test_performance_amazon_buy_now():
    options = webdriver.ChromeOptions()
#     options.add_argument("-headless")
    driver = webdriver.Chrome(options=options)
  
    async with driver.bidi_connection() as connection:
            # await connection.session.execute(connection.devtools.network.enable())
            await connection.session.execute(connection.devtools.performance.enable())

            # credentials = base64.b64encode("admin:admin".encode()).decode()
            # auth = {'authorization': 'Basic ' + credentials}
            # await connection.session.execute(connection.devtools.network.set_extra_http_headers(Headers(auth)))

            metric_list = await connection.session.execute(connection.devtools.performance.get_metrics())
            
            driver.get("https://www.amazon.co.uk/fire-tv-stick-4k/dp/B0BTFRN4K6?ref=dlx_deals_dg_dcl_B0BTFRN4K6_dt_sl14_cf")

    driver.execute_script("return document.getElementById('buy-now-button').click()")

    metrics = {metric.name: metric.value for metric in metric_list}
    for k, v in metrics.items():
        print(f"{k}: {v}")

trio.run(test_performance_amazon_buy_now)

Result

  • Timestamp: 152282.28288
  • AudioHandlers: 0.0
  • AudioWorkletProcessors: 0.0
  • Documents: 3.0
  • Frames: 2.0
  • JSEventListeners: 0.0
  • LayoutObjects: 5.0
  • MediaKeySessions: 0.0
  • MediaKeys: 0.0
  • Nodes: 12.0
  • Resources: 0.0
  • ContextLifecycleStateObservers: 3.0
  • V8PerContextDatas: 1.0
  • WorkerGlobalScopes: 0.0
  • UACSSResources: 0.0
  • RTCPeerConnections: 0.0
  • ResourceFetchers: 3.0
  • AdSubframes: 0.0
  • DetachedScriptStates: 0.0
  • ArrayBufferContents: 0.0
  • LayoutCount: 0.0
  • RecalcStyleCount: 0.0
  • LayoutDuration: 0.0
  • RecalcStyleDuration: 0.0
  • DevToolsCommandDuration: 6.4e-05
  • ScriptDuration: 0.0
  • V8CompileDuration: 0.0
  • TaskDuration: 9.3e-05
  • TaskOtherDuration: 2.9e-05
  • ThreadTime: 0.000213
  • ProcessTime: 0.185262
  • JSHeapUsedSize: 453868.0
  • JSHeapTotalSize: 1998848.0
  • FirstMeaningfulPaint: 0.0
  • DomContentLoaded: 152281.36409
  • NavigationStart: 152280.464883

Potential limitations

  • Compatibility with Other Web Browsers Since Selenium uses the Chrome DevTools Protocol (CDP) to implement its API, the performance features are only available on Chromium-based browsers.
  • Flexibility Selenium follow to the old W3C WebDriver standard, so some current features may have been deprecated or may not be fully supported in certain browsers.

Clone this wiki locally