Skip to content

Commit

Permalink
Implement method to retrieve logs for Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Feb 29, 2024
1 parent ee3d6c3 commit 2c817fd
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/selenium/chrome/capabilities.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ class Selenium::Chrome::Capabilities < Selenium::Capabilities
@[JSON::Field(key: "goog:chromeOptions")]
property chrome_options = ChromeOptions.new

# Enable logging
#
# ```
# capabilities = Selenium::Chrome::Capabilities.new
# capabilities.logging_prefs = {
# "browser" => "ALL",
# "driver" => "ALL",
# "performance" => "ALL",
# }
# ```
@[JSON::Field(key: "goog:loggingPrefs")]
property logging_prefs : Hash(String, String)?

class ChromeOptions
include JSON::Serializable

Expand Down
10 changes: 10 additions & 0 deletions src/selenium/chrome/command_handler.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Selenium::Chrome::CommandHandler < Selenium::CommandHandler
CHROME_COMMANDS = {
get_available_log_types: {:get, "/session/:session_id/se/log/types"},
get_log: {:post, "/session/:session_id/se/log"},
}

def commands
DEFAULT_COMMANDS.merge(CHROME_COMMANDS)
end
end
4 changes: 4 additions & 0 deletions src/selenium/chrome/driver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ class Selenium::Chrome::Driver < Selenium::Driver
super(capabilities)
end

private def command_handler
Chrome::CommandHandler.new(@http_client)
end

def create_session(args : Array(String)) : Session
capabilities = Chrome::Capabilities.new
capabilities.chrome_options.args = args
Expand Down
8 changes: 8 additions & 0 deletions src/selenium/chrome/log_entry.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Selenium::Chrome::LogEntry
include JSON::Serializable

property level : String
property message : String
property source : String?
property timestamp : Int64
end
7 changes: 6 additions & 1 deletion src/selenium/command_handler.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
class Selenium::CommandHandler
include DefaultCommands

getter http_client : HttpClient

def initialize(@http_client)
end

def commands
DEFAULT_COMMANDS
end

def execute(command, path_variables : Hash(String, String) = {} of String => String, parameters = {} of String => String) : JSON::Any
method, path = DEFAULT_COMMANDS[command]
method, path = commands[command]
full_path = path_variables.reduce(path) { |acc, entry| acc.sub(entry.first, entry.last) }

execute(method, full_path, parameters.to_json)
Expand Down
6 changes: 5 additions & 1 deletion src/selenium/driver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ abstract class Selenium::Driver
@service.try &.start
base_url ||= @service.not_nil!.base_url
@http_client = HttpClient.new(base_url: base_url)
@command_handler = CommandHandler.new(@http_client)
@command_handler = command_handler
end

private def command_handler : CommandHandler
CommandHandler.new(@http_client)
end

abstract def create_session(args : Array(String)) : Session
Expand Down
18 changes: 18 additions & 0 deletions src/selenium/session.cr
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ class Selenium::Session
perform_actions([sequence])
end

# Get available log types
def available_log_types : Array(String)
data = command_handler.execute(:get_available_log_types, path_variables)

data["value"].as_a.map(&.as_s)
end

# Get the log for a given log type. Log buffer is reset after each request
#
# List of common log types: "client", "driver", "browser", "server"
def log(type : String) : Array(Selenium::Chrome::LogEntry)
data = command_handler.execute(:get_log, path_variables, {"type" => type})

data.as_h["value"].as_a.map do |log|
Selenium::Chrome::LogEntry.from_json(log.to_json)
end
end

private def path_variables
{":session_id" => id}
end
Expand Down

0 comments on commit 2c817fd

Please sign in to comment.