HashNuke / hound

Elixir library for writing integration tests and browser automation
http://hexdocs.pm/hound
MIT License
1.36k stars 145 forks source link

Headless Chrome sessions not starting on recent Chrome versions #257

Open michallepicki opened 9 months ago

michallepicki commented 9 months ago

To fix the “invalid session id” we need to call:

Hound.start_session([additional_capabilities: %{browserName: "chrome"}])
ScreenverseJames commented 9 months ago

Thank you, Michał, for posting this. We hit the same issue, and your workaround worked nicely for us! Very much appreciated!

agatheblues commented 9 months ago

Worked for us too! Thanks a bunch ❤️

HarshBalyan commented 8 months ago

@michallepicki How did you conclude that? I tried your suggestion but it didn't work. Before I see this invalid session ID error, i see the following response for a POST /session call to the driver server.

%{
   "sessionId" => "e9796a5997cef178db3120e89d0f1f28",
   "status" => 33,
   "value" => %{
     "message" => "session not created: Chrome failed to start: exited normally.\n  (session not created: DevToolsActivePort file doesn't exist)\n  (The process started from chrome location /usr/lib/chromium/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)\n  (Driver info: chromedriver=121.0.6167.184 (057a8ae7deb3374d0f1b04b36304d236f0136188-refs/branch-heads/6167@{#1818}),platform=Linux 5.15.49-linuxkit-pr aarch64)"
   }
 }

Any idea?

michallepicki commented 8 months ago

How did you conclude that?

I inspected responses from chromedriver somewhere in hound code, and there was an error about incorrect browser name. Unfortunately I am not familiar with the problem you're facing

HarshBalyan commented 8 months ago

I finally solved it by overriding the chromeOptions that are set by default_capabilities for chrome_headless browser(I don't set user_agent in additional capabilities). I had to also set the browserName as an additional capability. My config

config :hound,
  driver: "chrome_driver",
  browser: "chrome_headless",
  host: System.get_env("HEADLESS_BROWSER_URL"),
  port: 9515

I started my hound_session/1 in test like

defmodule SomeTest do
  use ExUnit.Case
  use Hound.Helpers

  hound_session(additional_capabilities())

  # some tests

  defp additional_capabilities do
      [
        additional_capabilities: %{
          :"goog:chromeOptions" => %{
            "args" => [
              "--headless",
              "--disable-gpu",
              "--no-sandbox",
              "--disable-dev-shm-usage",
              "--disable-software-rasterizer"
            ]
          },
          browserName: "chrome"
        }
      ]
  end
end

I am using a flavour of this docker image for starting a chrome driver server.

MikaAK commented 6 months ago

@HarshBalyan this shockingly worked for me as well. Thanks for the fix!