Skip to content

Commit

Permalink
extract RUM logic out of Selenium contrib, reenable testing selenium/…
Browse files Browse the repository at this point in the history
…capybara integration for Ruby 3.4
  • Loading branch information
anmarchenko committed Jan 21, 2025
1 parent ff0a815 commit aac60e3
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ TEST_METADATA = {
"knapsack_pro-7-rspec-3" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ❌ jruby"
},
"selenium" => {
"selenium-4-capybara-3" => "❌ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby"
"selenium-4-capybara-3" => "❌ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby"
},
"timecop" => {
"timecop-0" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ jruby"
Expand Down
8 changes: 4 additions & 4 deletions lib/datadog/ci/contrib/selenium/capybara_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

require_relative "../patcher"

require_relative "ext"
require_relative "rum"
require_relative "../../ext/rum"
require_relative "../../ext/test"
require_relative "../../utils/rum"

module Datadog
module CI
Expand All @@ -22,10 +22,10 @@ def reset!

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

RUM.stop_rum_session(@browser)
Utils::RUM.stop_rum_session(@browser)

Datadog.logger.debug("[Selenium] RUM session stopped, deleting cookie")
@browser.manage.delete_cookie(Ext::COOKIE_TEST_EXECUTION_ID)
@browser.manage.delete_cookie(CI::Ext::RUM::COOKIE_TEST_EXECUTION_ID)
rescue ::Selenium::WebDriver::Error::WebDriverError => e
Datadog.logger.debug("[Selenium] Error while resetting Capybara session: #{e.message}")
ensure
Expand Down
4 changes: 3 additions & 1 deletion lib/datadog/ci/contrib/selenium/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
require_relative "../ext"
require_relative "../../settings"

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

module Datadog
module CI
module Contrib
Expand All @@ -21,7 +23,7 @@ class Settings < Datadog::CI::Contrib::Settings

option :rum_flush_wait_millis do |o|
o.type :int
o.env Ext::ENV_RUM_FLUSH_WAIT_MILLIS
o.env CI::Ext::RUM::ENV_RUM_FLUSH_WAIT_MILLIS
o.default 500
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/datadog/ci/contrib/selenium/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

require_relative "../patcher"

require_relative "ext"
require_relative "rum"
require_relative "../../utils/rum"
require_relative "../../ext/rum"
require_relative "../../ext/test"

module Datadog
Expand All @@ -22,10 +22,10 @@ def quit

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

RUM.stop_rum_session(@bridge)
Utils::RUM.stop_rum_session(@bridge)

Datadog.logger.debug("[Selenium] RUM session stopped, deleting cookie")
@bridge.manage.delete_cookie(Ext::COOKIE_TEST_EXECUTION_ID)
@bridge.manage.delete_cookie(CI::Ext::RUM::COOKIE_TEST_EXECUTION_ID)
rescue ::Selenium::WebDriver::Error::WebDriverError => e
Datadog.logger.debug("[Selenium] Error while quitting Selenium driver: #{e.message}")
ensure
Expand Down
15 changes: 0 additions & 15 deletions lib/datadog/ci/contrib/selenium/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@ module Selenium
# @public_api
module Ext
ENV_ENABLED = "DD_CIVISIBILITY_SELENIUM_ENABLED"
ENV_RUM_FLUSH_WAIT_MILLIS = "DD_CIVISIBILITY_RUM_FLUSH_WAIT_MILLIS"

COOKIE_TEST_EXECUTION_ID = "datadog-ci-visibility-test-execution-id"

SCRIPT_IS_RUM_ACTIVE = <<~JS
return !!window.DD_RUM
JS
SCRIPT_STOP_RUM_SESSION = <<~JS
if (window.DD_RUM && window.DD_RUM.stopSession) {
window.DD_RUM.stopSession();
return true;
} else {
return false;
}
JS
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/contrib/selenium/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_relative "../patcher"

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

module Datadog
Expand Down Expand Up @@ -32,7 +32,7 @@ def to(url)
return result unless active_test

# Set the test's trace id as a cookie in browser session
cookie_hash = {name: Ext::COOKIE_TEST_EXECUTION_ID, value: active_test.trace_id.to_s}
cookie_hash = {name: CI::Ext::RUM::COOKIE_TEST_EXECUTION_ID, value: active_test.trace_id.to_s}
Datadog.logger.debug { "[Selenium] Setting cookie: #{cookie_hash}" }
@bridge.manage.add_cookie(cookie_hash)

Expand Down
43 changes: 0 additions & 43 deletions lib/datadog/ci/contrib/selenium/rum.rb

This file was deleted.

26 changes: 26 additions & 0 deletions lib/datadog/ci/ext/rum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Datadog
module CI
module Ext
# Defines constants for Git tags
module RUM
ENV_RUM_FLUSH_WAIT_MILLIS = "DD_CIVISIBILITY_RUM_FLUSH_WAIT_MILLIS"

COOKIE_TEST_EXECUTION_ID = "datadog-ci-visibility-test-execution-id"

SCRIPT_IS_RUM_ACTIVE = <<~JS
return !!window.DD_RUM
JS
SCRIPT_STOP_RUM_SESSION = <<~JS
if (window.DD_RUM && window.DD_RUM.stopSession) {
window.DD_RUM.stopSession();
return true;
} else {
return false;
}
JS
end
end
end
end
43 changes: 43 additions & 0 deletions lib/datadog/ci/utils/rum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require_relative "../ext/rum"
require_relative "../ext/test"
require_relative "parsing"

module Datadog
module CI
module Utils
# Provides functionality to interact with Datadog Real User Monitoring product
# via executing JavaScript code in the browser.
#
# Relevant docs: https://docs.datadoghq.com/real_user_monitoring/browser/
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}" }

Utils::Parsing.convert_to_bool(is_rum_active_script_result)
end

def self.stop_rum_session(script_executor)
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
end
end
end
end
end
8 changes: 0 additions & 8 deletions sig/datadog/ci/contrib/selenium/ext.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ module Datadog
module Selenium
module Ext
ENV_ENABLED: "DD_CIVISIBILITY_SELENIUM_ENABLED"

ENV_RUM_FLUSH_WAIT_MILLIS: "DD_CIVISIBILITY_RUM_FLUSH_WAIT_MILLIS"

COOKIE_TEST_EXECUTION_ID: "datadog-ci-visibility-test-execution-id"

SCRIPT_IS_RUM_ACTIVE: String

SCRIPT_STOP_RUM_SESSION: String
end
end
end
Expand Down
13 changes: 0 additions & 13 deletions sig/datadog/ci/contrib/selenium/rum.rbs

This file was deleted.

16 changes: 16 additions & 0 deletions sig/datadog/ci/ext/rum.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Datadog
module CI
module Ext
module RUM
ENV_RUM_FLUSH_WAIT_MILLIS: "DD_CIVISIBILITY_RUM_FLUSH_WAIT_MILLIS"

COOKIE_TEST_EXECUTION_ID: "datadog-ci-visibility-test-execution-id"

SCRIPT_IS_RUM_ACTIVE: String
SCRIPT_STOP_RUM_SESSION: String
end
end
end
end
11 changes: 11 additions & 0 deletions sig/datadog/ci/utils/rum.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Datadog
module CI
module Utils
module RUM
def self.is_rum_active?: (untyped script_executor) -> bool

def self.stop_rum_session: (untyped script_executor) -> void
end
end
end
end
8 changes: 4 additions & 4 deletions spec/datadog/ci/contrib/selenium/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@
expect(visited_urls).to eq(["http://www.example.com", "about:blank"])
expect(executed_scripts).to eq(
[
Datadog::CI::Contrib::Selenium::Ext::SCRIPT_IS_RUM_ACTIVE,
Datadog::CI::Contrib::Selenium::Ext::SCRIPT_STOP_RUM_SESSION,
Datadog::CI::Ext::RUM::SCRIPT_IS_RUM_ACTIVE,
Datadog::CI::Ext::RUM::SCRIPT_STOP_RUM_SESSION,
"window.sessionStorage.clear()",
"window.localStorage.clear()",
Datadog::CI::Contrib::Selenium::Ext::SCRIPT_IS_RUM_ACTIVE,
Datadog::CI::Contrib::Selenium::Ext::SCRIPT_STOP_RUM_SESSION
Datadog::CI::Ext::RUM::SCRIPT_IS_RUM_ACTIVE,
Datadog::CI::Ext::RUM::SCRIPT_STOP_RUM_SESSION
]
)

Expand Down

0 comments on commit aac60e3

Please sign in to comment.