Skip to content

[ruby] BiDi code examples #1273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions examples/ruby/spec/bidi/bidi_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'selenium-webdriver'

RSpec.describe 'Integration Tests' do
let(:wait) { Selenium::WebDriver::Wait.new(timeout: 2) }
let(:driver) do
options = Selenium::WebDriver::Options.firefox
options.add_option(:web_socket_url, true)
Selenium::WebDriver.for :firefox, options: options
end

after do
driver.quit
end

it 'test navigate and listen to errors' do
log_entry = nil
log_inspector = Selenium::WebDriver::BiDi::LogInspector.new(driver)
log_inspector.on_javascript_exception { |log| log_entry = log }

id = driver.window_handle
browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, browsing_context_id: id)

info = browsing_context.navigate url: 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html'

expect(browsing_context.id).not_to be_nil
expect(info.navigation_id).to be_nil
expect(info.url).to include('/bidi/logEntryAdded.html')

wait.until { driver.find_element(id: 'jsException').displayed? }
driver.find_element(id: 'jsException').click
wait.until { !log_entry.nil? }

expect(log_entry).to have_attributes(
text: 'Error: Not working',
type: 'javascript',
level: Selenium::WebDriver::BiDi::LogInspector::LOG_LEVEL[:ERROR]
)
end
end
109 changes: 109 additions & 0 deletions examples/ruby/spec/bidi/browsing_context_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# frozen_string_literal: true

require 'selenium-webdriver'

RSpec.describe 'Browsing Context' do
let(:driver) do
options = Selenium::WebDriver::Options.firefox
options.add_option(:web_socket_url, true)
Selenium::WebDriver.for :firefox, options: options
end

after do
driver.quit
end

it 'test create a browsing context for given id' do
id = driver.window_handle
browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, browsing_context_id: id)
expect(browsing_context.id).to eq(id)
end

it 'test create a window' do
browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :window)
expect(browsing_context.id).not_to be_nil
end

it 'test create a window with a reference context' do
browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :window,
reference_context: driver.window_handle)
expect(browsing_context.id).not_to be_nil
end

it 'test create a tab' do
browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab)
expect(browsing_context.id).not_to be_nil
end

it 'test create a tab with a reference context' do
browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab,
reference_context: driver.window_handle)
expect(browsing_context.id).not_to be_nil
end

it 'test navigate to a url' do
browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab)

info = browsing_context.navigate url: 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html'

expect(browsing_context.id).not_to be_nil
expect(info.navigation_id).to be_nil
expect(info.url).to include('/bidi/logEntryAdded.html')
end

it 'test navigate to a url with readiness state' do
browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab)

info = browsing_context.navigate url: 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html',
readiness_state: :complete

expect(browsing_context.id).not_to be_nil
expect(info.navigation_id).to be_nil
expect(info.url).to include('/bidi/logEntryAdded.html')
end

it 'test get tree with a child' do
browsing_context_id = driver.window_handle
parent_window = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver,
browsing_context_id: browsing_context_id)
parent_window.navigate(url: 'https://www.selenium.dev/selenium/web/iframes.html',
readiness_state: :complete)

context_info = parent_window.get_tree
expect(context_info.children.size).to eq(1)
expect(context_info.id).to eq(browsing_context_id)
expect(context_info.children[0]['url']).to include('formPage.html')
end

it 'test get tree with depth' do
browsing_context_id = driver.window_handle
parent_window = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver,
browsing_context_id: browsing_context_id)
parent_window.navigate(url: 'https://www.selenium.dev/selenium/web/iframes.html',
readiness_state: :complete)

context_info = parent_window.get_tree(max_depth: 0)
expect(context_info.children).to be_nil
expect(context_info.id).to eq(browsing_context_id)
end

it 'test close a window' do
window1 = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :window)
window2 = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :window)

window2.close

expect { window1.get_tree }.not_to raise_error
expect { window2.get_tree }.to raise_error(Selenium::WebDriver::Error::WebDriverError)
end

it 'test close a tab' do
tab1 = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab)
tab2 = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab)

tab2.close

expect { tab1.get_tree }.not_to raise_error
expect { tab2.get_tree }.to raise_error(Selenium::WebDriver::Error::WebDriverError)
end
end
105 changes: 105 additions & 0 deletions examples/ruby/spec/bidi/log_inspector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# frozen_string_literal: true

require 'selenium-webdriver'

RSpec.describe 'Log Inspector' do
let(:wait) { Selenium::WebDriver::Wait.new(timeout: 2) }
let(:driver) do
options = Selenium::WebDriver::Options.firefox
options.add_option(:web_socket_url, true)
Selenium::WebDriver.for :firefox, options: options
end

after do
driver.quit
end

it 'test listen to console log' do
log_entry = nil
log_inspector = Selenium::WebDriver::BiDi::LogInspector.new(driver)
log_inspector.on_console_entry { |log| log_entry = log }

driver.get 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html'
driver.find_element(id: 'consoleLog').click
wait.until { !log_entry.nil? }

expect(log_entry).to have_attributes(
text: 'Hello, world!',
realm: nil,
type: 'console',
level: Selenium::WebDriver::BiDi::LogInspector::LOG_LEVEL[:INFO],
method: 'log',
stack_trace: nil
)
expect(log_entry.args.size).to eq(1)
end

it 'test listen to javascript log' do
log_entry = nil
log_inspector = Selenium::WebDriver::BiDi::LogInspector.new(driver)
log_inspector.on_javascript_log { |log| log_entry = log }

driver.get 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html'
driver.find_element(id: 'jsException').click
wait.until { !log_entry.nil? }

expect(log_entry).to have_attributes(
text: 'Error: Not working',
type: 'javascript',
level: Selenium::WebDriver::BiDi::LogInspector::LOG_LEVEL[:ERROR]
)
end

it 'test listen to javascript error log' do
log_entry = nil
log_inspector = Selenium::WebDriver::BiDi::LogInspector.new(driver)
log_inspector.on_javascript_exception { |log| log_entry = log }

driver.get 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html'
driver.find_element(id: 'jsException').click
wait.until { !log_entry.nil? }

expect(log_entry).to have_attributes(
text: 'Error: Not working',
type: 'javascript'
)
end

it 'test retrieve stack trace for a log' do
log_entry = nil
log_inspector = Selenium::WebDriver::BiDi::LogInspector.new(driver)
log_inspector.on_javascript_exception { |log| log_entry = log }

driver.get 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html'
driver.find_element(id: 'jsException').click
wait.until { !log_entry.nil? }

stack_trace = log_entry.stack_trace

expect(stack_trace).not_to be_nil
expect(stack_trace['callFrames'].size).to eq(3)
end

it 'test listen to logs with multiple consumers' do
log_entry1 = nil
log_entry2 = nil
log_inspector = Selenium::WebDriver::BiDi::LogInspector.new(driver)
log_inspector.on_javascript_exception { |log| log_entry1 = log }
log_inspector.on_javascript_exception { |log| log_entry2 = log }

driver.get 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html'
driver.find_element(id: 'jsException').click
wait.until { !log_entry1.nil? }
wait.until { !log_entry2.nil? }

expect(log_entry1).to have_attributes(
text: 'Error: Not working',
type: 'javascript'
)

expect(log_entry2).to have_attributes(
text: 'Error: Not working',
type: 'javascript'
)
end
end
Loading