Skip to content

Commit

Permalink
test selenium integration with mocked Selenium::WebDriver::Remote::Br…
Browse files Browse the repository at this point in the history
…idge
  • Loading branch information
anmarchenko committed May 28, 2024
1 parent 07f8c09 commit 1ddc66a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/datadog/ci/contrib/selenium/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "datadog/tracing/contrib/patcher"

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

module Datadog
module CI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@

Then "visit page" do
visit "http://www.example.com"
Capybara.current_session.quit
rescue => e
p e.backtrace
end
51 changes: 50 additions & 1 deletion spec/datadog/ci/contrib/selenium/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,31 @@
let(:integration_name) { :cucumber }
end

let(:manager) { spy("manager", add_cookie: nil) }
let(:bridge) do
instance_double(
Selenium::WebDriver::Remote::Bridge,
create_session: nil,
browser: "mockbrowser",
capabilities: double("capabilities", browser_version: "mockversion", "[]": "mockcapabilities"),
window_handles: ["window"],
switch_to_window: true,
manage: manager,
find_elements_by: [],
extend: true,
send: true,
quit: true
)
end

let(:stdin) { StringIO.new }
let(:stdout) { StringIO.new }
let(:stderr) { StringIO.new }

# let(:stdin) { $stdin }
# let(:stdout) { $stdout }
# let(:stderr) { $stderr }

let(:kernel) { double(:kernel) }

# Cucumber runtime setup
Expand All @@ -35,19 +57,46 @@
end
let(:expected_test_run_code) { 0 }

# spies
let(:executed_scripts) { [] }
let(:visited_urls) { [] }

before do
expect(kernel).to receive(:exit).with(expected_test_run_code)
# expect(kernel).to receive(:exit).with(expected_test_run_code)
expect(Selenium::WebDriver::Remote::Bridge).to receive(:new).and_return(bridge)
expect(bridge).to receive(:execute_script) do |script|
executed_scripts << script
"true"
end
expect(bridge).to receive(:get) do |url|
visited_urls << url
end

# allow_any_instance_of(Selenium::WebDriver::Remote::Bridge).to receive(:get).and_return(nil)

allow(kernel).to receive(:exit)
cli.execute!(existing_runtime)
end

it "recognize the test as browser test and adds additional tags" do
expect(visited_urls).to eq(["http://www.example.com"])
expect(executed_scripts).to eq([Datadog::CI::Contrib::Selenium::Ext::SCRIPT_IS_RUM_ACTIVE])

expect(test_spans).to have(1).item

expect(manager).to have_received(:add_cookie).with(
{name: "datadog-ci-visibility-test-execution-id", value: first_test_span.trace_id.to_s}
)

expect(first_test_span).to have_test_tag(:type, "browser")
expect(first_test_span).to have_test_tag(:browser_driver, "selenium")
expect(first_test_span).to have_test_tag(
:browser_driver_version,
Gem.loaded_specs["selenium-webdriver"].version.to_s
)
expect(first_test_span).to have_test_tag(:browser_name, "mockbrowser")
expect(first_test_span).to have_test_tag(:browser_version, "mockversion")

expect(first_test_span).to have_test_tag(:is_rum_active, "true")
end
end

0 comments on commit 1ddc66a

Please sign in to comment.