How can I retrieve the user agent string in a headless browser?

I am conducting tests using a headless version of Chrome and I need to obtain the user agent for the headless instance. In a regular Chrome browser, I utilize the following code to retrieve the user agent:

page.execute_script("navigator.userAgent"); # This works as expected

However, this method does not function properly for the headless version. Is there an alternative approach to acquire the user agent string?

Note: My framework utilizes Ruby and Capybara.

Another potential avenue is to handle this task via network interception. If working with Selenium, you can navigate through the DevTools Protocol that allows you to intercept and access network requests. By doing so, you can fetch information about the user agent from the headers of these requests. This might be a bit more involved compared to the straightforward script execution but gives you a broader control over network operations in a headless browser environment, especially beneficial for testing purposes.

You can try setting the user agent directly when you’re launching the headless Chrome instance. With Capybara and Selenium, you can specify desired capabilities to set a custom user agent. For example, Capybara::Selenium::Driver.new(app, browser: :chrome, options: Selenium::WebDriver::Chrome::Options.new(args: ['--headless', '--user-agent=my-custom-user-agent'])). If you need to read the set user agent later, make sure you’ve defined it during the browser setup as this approach might work differently than in non-headless environments.

In my experience, one effective way to handle user agent retrieval in a headless browser like Chrome with Capybara and Selenium is utilizing browser logs. Once you have configured your Selenium driver, you can enable logging for performance or browser categories. After making a request or loading a page, inspect the logs stored by the Selenium driver to find the user agent information recorded there. This method not only confirms the current agent but also provides visibility into other aspects of your interaction with the headless browser.