Path: blob/trunk/rb/spec/integration/selenium/webdriver/bidi/script_spec.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819require_relative '../spec_helper'2021module Selenium22module WebDriver23describe Script, exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},24only: {browser: %i[chrome edge firefox]} do25after { |example| reset_driver!(example: example) }2627# Helper to match the expected pattern of `script.StackFrame` objects.28# https://w3c.github.io/webdriver-bidi/#type-script-StackFrame29#30# Pass in any fields you want to check more specific values for, e.g:31# a_stack_frame('functionName' => 'someFunction')32def a_stack_frame(**options)33include({34'columnNumber' => an_instance_of(Integer),35'functionName' => an_instance_of(String),36'lineNumber' => an_instance_of(Integer),37'url' => an_instance_of(String)38}.merge(options))39end4041it 'errors when bidi not enabled' do42reset_driver!(web_socket_url: false) do |driver|43msg = /BiDi must be enabled by setting #web_socket_url to true in options class/44expect { driver.script }.to raise_error(WebDriver::Error::WebDriverError, msg)45end46end4748it 'logs console messages' do49driver.navigate.to url_for('bidi/logEntryAdded.html')5051log_entries = []52driver.script.add_console_message_handler { |log| log_entries << log }5354driver.find_element(id: 'jsException').click55driver.find_element(id: 'consoleLog').click5657wait.until { log_entries.any? }58expect(log_entries.size).to eq(1)59log_entry = log_entries.first60expect(log_entry).to be_a BiDi::LogHandler::ConsoleLogEntry61expect(log_entry.type).to eq 'console'62expect(log_entry.level).to eq 'info'63expect(log_entry.method).to eq 'log'64expect(log_entry.text).to eq 'Hello, world!'65expect(log_entry.args).to eq [66{'type' => 'string', 'value' => 'Hello, world!'}67]68expect(log_entry.timestamp).to be_an_integer69expect(log_entry.source).to match(70'context' => an_instance_of(String),71'realm' => an_instance_of(String)72)73# Stack traces on console messages are optional.74expect(log_entry.stack_trace).to be_nil.or match(75# Some browsers include stack traces from parts of the runtime, so we76# just check the first frames that come from user code.77'callFrames' => start_with(78a_stack_frame('functionName' => 'helloWorld'),79a_stack_frame('functionName' => 'onclick')80)81)82end8384it 'logs multiple console messages' do85driver.navigate.to url_for('bidi/logEntryAdded.html')8687log_entries = []88driver.script.add_console_message_handler { |log| log_entries << log }89driver.script.add_console_message_handler { |log| log_entries << log }9091driver.find_element(id: 'jsException').click92driver.find_element(id: 'consoleLog').click9394wait.until { log_entries.size > 1 }95expect(log_entries.size).to eq(2)96end9798it 'removes console message handler' do99driver.navigate.to url_for('bidi/logEntryAdded.html')100101log_entries = []102id = driver.script.add_console_message_handler { |log| log_entries << log }103driver.script.add_console_message_handler { |log| log_entries << log }104105driver.find_element(id: 'consoleLog').click106107wait.until { log_entries.size > 1 }108109driver.script.remove_console_message_handler(id)110111driver.find_element(id: 'consoleLog').click112113wait.until { log_entries.size > 2 }114expect(log_entries.size).to eq(3)115end116117it 'logs javascript errors' do118driver.navigate.to url_for('bidi/logEntryAdded.html')119120log_entries = []121driver.script.add_javascript_error_handler { |log| log_entries << log }122123driver.find_element(id: 'consoleLog').click124driver.find_element(id: 'jsException').click125126wait.until { log_entries.any? }127expect(log_entries.size).to eq(1)128log_entry = log_entries.first129expect(log_entry).to be_a BiDi::LogHandler::JavaScriptLogEntry130expect(log_entry.type).to eq 'javascript'131expect(log_entry.level).to eq 'error'132expect(log_entry.text).to eq 'Error: Not working'133expect(log_entry.timestamp).to be_an_integer134expect(log_entry.source).to match(135'context' => an_instance_of(String),136'realm' => an_instance_of(String)137)138expect(log_entry.stack_trace).to match(139# Some browsers include stack traces from parts of the runtime, so we140# just check the first frames that come from user code.141'callFrames' => start_with(142a_stack_frame('functionName' => 'createError'),143a_stack_frame('functionName' => 'onclick')144)145)146end147148it 'errors removing non-existent handler' do149expect {150driver.script.remove_console_message_handler(12345)151}.to raise_error(Error::WebDriverError, /Callback with ID 12345 does not exist/)152end153end154end155end156157158