Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/integration/selenium/webdriver/bidi/script_spec.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
require_relative '../spec_helper'
21
22
module Selenium
23
module WebDriver
24
describe Script, exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},
25
only: {browser: %i[chrome edge firefox]} do
26
after { |example| reset_driver!(example: example) }
27
28
# Helper to match the expected pattern of `script.StackFrame` objects.
29
# https://w3c.github.io/webdriver-bidi/#type-script-StackFrame
30
#
31
# Pass in any fields you want to check more specific values for, e.g:
32
# a_stack_frame('functionName' => 'someFunction')
33
def a_stack_frame(**options)
34
include({
35
'columnNumber' => an_instance_of(Integer),
36
'functionName' => an_instance_of(String),
37
'lineNumber' => an_instance_of(Integer),
38
'url' => an_instance_of(String)
39
}.merge(options))
40
end
41
42
it 'errors when bidi not enabled' do
43
reset_driver!(web_socket_url: false) do |driver|
44
msg = /BiDi must be enabled by setting #web_socket_url to true in options class/
45
expect { driver.script }.to raise_error(WebDriver::Error::WebDriverError, msg)
46
end
47
end
48
49
it 'logs console messages' do
50
driver.navigate.to url_for('bidi/logEntryAdded.html')
51
52
log_entries = []
53
driver.script.add_console_message_handler { |log| log_entries << log }
54
55
driver.find_element(id: 'jsException').click
56
driver.find_element(id: 'consoleLog').click
57
58
wait.until { log_entries.any? }
59
expect(log_entries.size).to eq(1)
60
log_entry = log_entries.first
61
expect(log_entry).to be_a BiDi::LogHandler::ConsoleLogEntry
62
expect(log_entry.type).to eq 'console'
63
expect(log_entry.level).to eq 'info'
64
expect(log_entry.method).to eq 'log'
65
expect(log_entry.text).to eq 'Hello, world!'
66
expect(log_entry.args).to eq [
67
{'type' => 'string', 'value' => 'Hello, world!'}
68
]
69
expect(log_entry.timestamp).to be_an_integer
70
expect(log_entry.source).to match(
71
'context' => an_instance_of(String),
72
'realm' => an_instance_of(String)
73
)
74
# Stack traces on console messages are optional.
75
expect(log_entry.stack_trace).to be_nil.or match(
76
# Some browsers include stack traces from parts of the runtime, so we
77
# just check the first frames that come from user code.
78
'callFrames' => start_with(
79
a_stack_frame('functionName' => 'helloWorld'),
80
a_stack_frame('functionName' => 'onclick')
81
)
82
)
83
end
84
85
it 'logs multiple console messages' do
86
driver.navigate.to url_for('bidi/logEntryAdded.html')
87
88
log_entries = []
89
driver.script.add_console_message_handler { |log| log_entries << log }
90
driver.script.add_console_message_handler { |log| log_entries << log }
91
92
driver.find_element(id: 'jsException').click
93
driver.find_element(id: 'consoleLog').click
94
95
wait.until { log_entries.size > 1 }
96
expect(log_entries.size).to eq(2)
97
end
98
99
it 'removes console message handler' do
100
driver.navigate.to url_for('bidi/logEntryAdded.html')
101
102
log_entries = []
103
id = driver.script.add_console_message_handler { |log| log_entries << log }
104
driver.script.add_console_message_handler { |log| log_entries << log }
105
106
driver.find_element(id: 'consoleLog').click
107
108
wait.until { log_entries.size > 1 }
109
110
driver.script.remove_console_message_handler(id)
111
112
driver.find_element(id: 'consoleLog').click
113
114
wait.until { log_entries.size > 2 }
115
expect(log_entries.size).to eq(3)
116
end
117
118
it 'logs javascript errors' do
119
driver.navigate.to url_for('bidi/logEntryAdded.html')
120
121
log_entries = []
122
driver.script.add_javascript_error_handler { |log| log_entries << log }
123
124
driver.find_element(id: 'consoleLog').click
125
driver.find_element(id: 'jsException').click
126
127
wait.until { log_entries.any? }
128
expect(log_entries.size).to eq(1)
129
log_entry = log_entries.first
130
expect(log_entry).to be_a BiDi::LogHandler::JavaScriptLogEntry
131
expect(log_entry.type).to eq 'javascript'
132
expect(log_entry.level).to eq 'error'
133
expect(log_entry.text).to eq 'Error: Not working'
134
expect(log_entry.timestamp).to be_an_integer
135
expect(log_entry.source).to match(
136
'context' => an_instance_of(String),
137
'realm' => an_instance_of(String)
138
)
139
expect(log_entry.stack_trace).to match(
140
# Some browsers include stack traces from parts of the runtime, so we
141
# just check the first frames that come from user code.
142
'callFrames' => start_with(
143
a_stack_frame('functionName' => 'createError'),
144
a_stack_frame('functionName' => 'onclick')
145
)
146
)
147
end
148
149
it 'errors removing non-existent handler' do
150
expect {
151
driver.script.remove_console_message_handler(12345)
152
}.to raise_error(Error::WebDriverError, /Callback with ID 12345 does not exist/)
153
end
154
end
155
end
156
end
157
158