Skip to content

Commit

Permalink
fix JS script execution for cuprite driver
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jan 22, 2025
1 parent 3499c33 commit 6ccb242
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 23 deletions.
12 changes: 8 additions & 4 deletions lib/datadog/ci/contrib/cuprite/driver.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "script_executor"
require_relative "../patcher"

require_relative "../../ext/rum"
Expand Down Expand Up @@ -51,12 +52,16 @@ def visit(url)
end

def reset!
Datadog.logger.debug("[Cuprite] Driver reset! event")

datadog_end_rum_session

super
end

def quit
Datadog.logger.debug("[Cuprite] Driver quit event")

datadog_end_rum_session

super
Expand All @@ -75,11 +80,10 @@ def datadog_configuration
def datadog_end_rum_session
return unless datadog_configuration[:enabled]

Datadog.logger.debug("[Cuprite] Driver quit event")

Utils::RUM.stop_rum_session(self)
executor = ScriptExecutor.new(browser)
Utils::RUM.stop_rum_session(executor, rum_flush_wait_millis: datadog_configuration[:rum_flush_wait_millis])

Datadog.logger.debug("[Cuprite] RUM session stopped, deleting cookie")
Datadog.logger.debug("[Cuprite] Deleting Datadog cookie")
remove_cookie(CI::Ext::RUM::COOKIE_TEST_EXECUTION_ID)
end
end
Expand Down
32 changes: 32 additions & 0 deletions lib/datadog/ci/contrib/cuprite/script_executor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require_relative "../../ext/rum"

module Datadog
module CI
module Contrib
module Cuprite
class ScriptExecutor
# Ferrum::Browser requires a JS script to be wrapped in a function() { ... } block
WRAPPED_SCRIPTS = {
CI::Ext::RUM::SCRIPT_IS_RUM_ACTIVE => "function() { #{CI::Ext::RUM::SCRIPT_IS_RUM_ACTIVE}; }",
CI::Ext::RUM::SCRIPT_STOP_RUM_SESSION => <<~JS
function() {
#{CI::Ext::RUM::SCRIPT_STOP_RUM_SESSION};
}
JS
}.freeze

def initialize(ferrum_browser)
@ferrum_browser = ferrum_browser
end

def execute_script(script)
script = WRAPPED_SCRIPTS.fetch(script, script)
@ferrum_browser.evaluate_func(script)
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/selenium/capybara_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def reset!

Datadog.logger.debug("[Selenium] Capybara session reset event")

Utils::RUM.stop_rum_session(@browser)
Utils::RUM.stop_rum_session(@browser, rum_flush_wait_millis: datadog_configuration[:rum_flush_wait_millis])

Datadog.logger.debug("[Selenium] RUM session stopped, deleting cookie")
@browser.manage.delete_cookie(CI::Ext::RUM::COOKIE_TEST_EXECUTION_ID)
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/selenium/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def quit

Datadog.logger.debug("[Selenium] Driver quit event")

Utils::RUM.stop_rum_session(@bridge)
Utils::RUM.stop_rum_session(@bridge, rum_flush_wait_millis: datadog_configuration[:rum_flush_wait_millis])

Datadog.logger.debug("[Selenium] RUM session stopped, deleting cookie")
@bridge.manage.delete_cookie(CI::Ext::RUM::COOKIE_TEST_EXECUTION_ID)
Expand Down
31 changes: 15 additions & 16 deletions lib/datadog/ci/utils/rum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,26 @@ module RUM
def self.is_rum_active?(script_executor)
is_rum_active_script_result = script_executor.execute_script(Ext::RUM::SCRIPT_IS_RUM_ACTIVE)

Datadog.logger.debug { "[Selenium] SCRIPT_IS_RUM_ACTIVE result is #{is_rum_active_script_result.inspect}" }
Datadog.logger.debug { "[RUM] SCRIPT_IS_RUM_ACTIVE result is #{is_rum_active_script_result.inspect}" }

Utils::Parsing.convert_to_bool(is_rum_active_script_result)
end

def self.stop_rum_session(script_executor, rum_flush_wait_millis: 500)
config = Datadog.configuration.ci[:selenium]
if is_rum_active?(script_executor)
Datadog::CI.active_test&.set_tag(
CI::Ext::Test::TAG_IS_RUM_ACTIVE,
"true"
)

result = script_executor.execute_script(Ext::RUM::SCRIPT_STOP_RUM_SESSION)
Datadog.logger.debug { "[RUM] SCRIPT_STOP_RUM_SESSION result is #{result.inspect}" }

# introduce a delay to allow the RUM session to be stopped
delay = config[:rum_flush_wait_millis] / 1000.0
Datadog.logger.debug { "[RUM] Waiting for #{delay} seconds" }
sleep(delay)
end
return unless is_rum_active?(script_executor)

Datadog::CI.active_test&.set_tag(
CI::Ext::Test::TAG_IS_RUM_ACTIVE,
"true"
)

result = script_executor.execute_script(Ext::RUM::SCRIPT_STOP_RUM_SESSION)
Datadog.logger.debug { "[RUM] SCRIPT_STOP_RUM_SESSION result is #{result.inspect}" }

# introduce a delay to allow the RUM session to be stopped
delay = rum_flush_wait_millis / 1000.0
Datadog.logger.debug { "[RUM] Waiting for #{delay} seconds" }
sleep(delay)
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions sig/datadog/ci/contrib/cuprite/script_executor.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Datadog
module CI
module Contrib
module Cuprite
class ScriptExecutor
@ferrum_browser: Ferrum::Browser
WRAPPED_SCRIPTS: ::Hash[::String, ::String]

def initialize: (Ferrum::Browser ferrum_browser) -> void

def execute_script: (String script) -> String
end
end
end
end
end
2 changes: 1 addition & 1 deletion sig/datadog/ci/utils/rum.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Datadog
module RUM
def self.is_rum_active?: (untyped script_executor) -> bool

def self.stop_rum_session: (untyped script_executor) -> void
def self.stop_rum_session: (untyped script_executor, ?rum_flush_wait_millis: Integer) -> void
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

Then "visit page" do
visit "http://www.example.com"
p "visited"

Capybara.reset_session!
p "reset"

Capybara.current_session.quit
p "quit"
end
2 changes: 2 additions & 0 deletions vendor/rbs/cuprite/0/driver.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Ferrum::Browser
def version: () -> Ferrum::Browser::VersionInfo

def options: () -> Ferrum::Browser::Options

def evaluate_func: (String script) -> String
end

class Ferrum::Browser::Options
Expand Down

0 comments on commit 6ccb242

Please sign in to comment.