Skip to content

Commit e73a62b

Browse files
committed
[rb] Maintain methods to classes map for DevTools
Fixes #11912
1 parent a143063 commit e73a62b

File tree

6 files changed

+64
-15
lines changed

6 files changed

+64
-15
lines changed

rb/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ rb_binary(
198198
args = [
199199
"-rselenium-webdriver",
200200
"-rselenium/devtools",
201+
"-Irb/lib",
201202
],
202203
main = "@bundle//:bin/pry",
203204
deps = [

rb/Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
selenium-devtools (0.111.0)
4+
selenium-devtools (0.112.0)
55
selenium-webdriver (~> 4.2)
66
selenium-webdriver (4.8.6)
77
rexml (~> 3.2, >= 3.2.5)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
# This file is automatically generated. Any changes will be lost!
21+
22+
Dir.glob("#{File.dirname(File.absolute_path(__FILE__))}/<%= version.downcase %>/*", &method(:require))
23+
24+
module Selenium
25+
module DevTools
26+
module <%= version %>
27+
METHODS_TO_CLASSES = {
28+
<% domains.each do |domain| %>
29+
<%= h.snake_case(domain[:domain]) %>: '<%= domain[:domain] %>',
30+
<% end %>
31+
}.freeze
32+
end # <%= version %>
33+
end # DevTools
34+
end # Selenium

rb/lib/selenium/devtools/support/cdp_client_generator.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ module DevTools
2626
module Support
2727
class CDPClientGenerator
2828
# Input JSON files are generated from PDL tasks.
29-
TEMPLATE_PATH = File.expand_path('cdp/domain.rb.erb', __dir__)
29+
DOMAIN_TEMPLATE_PATH = File.expand_path('cdp/domain.rb.erb', __dir__)
30+
LOADER_TEMPLATE_PATH = File.expand_path('cdp/loader.rb.erb', __dir__)
3031

3132
RESERVED_KEYWORDS = %w[end].freeze
3233

3334
def call(output_dir:, version:, **opts)
34-
@template = ERB.new(File.read(TEMPLATE_PATH))
35+
@domain_template = ERB.new(File.read(DOMAIN_TEMPLATE_PATH))
36+
@loader_template = ERB.new(File.read(LOADER_TEMPLATE_PATH))
3537
@output_dir = output_dir
3638
@loader_path = opts.delete(:loader_path) || "#{@output_dir}.rb"
3739
@version = version
@@ -48,13 +50,13 @@ def call(output_dir:, version:, **opts)
4850

4951
FileUtils.mkdir_p(@output_dir)
5052

51-
browser_protocol[:domains].each { |domain| process_domain(domain) }
52-
js_protocol[:domains].each { |domain| process_domain(domain) }
53-
require_file
53+
all_domains = browser_protocol[:domains] + js_protocol[:domains]
54+
all_domains.each { |domain| process_domain(domain) }
55+
process_loader(all_domains)
5456
end
5557

5658
def process_domain(domain)
57-
result = @template.result_with_hash(domain: domain, version: @version.upcase, h: self)
59+
result = @domain_template.result_with_hash(domain: domain, version: @version.upcase, h: self)
5860
filename = File.join(@output_dir, "#{snake_case(domain[:domain])}.rb")
5961
File.write(filename, remove_empty_lines(result))
6062
end
@@ -86,13 +88,9 @@ def remove_empty_lines(string)
8688
string.split("\n").grep_v(/^\s+$/).join("\n")
8789
end
8890

89-
def require_file
90-
# rubocop:disable Lint/InterpolationCheck
91-
dynamic_location = '#{File.dirname(File.absolute_path(__FILE__))}'
92-
# rubocop:enable Lint/InterpolationCheck
93-
94-
require_all = "Dir.glob(\"#{dynamic_location}/#{@version}/*\", &method(:require))"
95-
File.write(@loader_path, require_all)
91+
def process_loader(domains)
92+
result = @loader_template.result_with_hash(domains: domains, version: @version.upcase, h: self)
93+
File.write(@loader_path, remove_empty_lines(result))
9694
end
9795
end
9896
end

rb/lib/selenium/webdriver/devtools.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,17 @@ def send_cmd(method, **params)
5252
end
5353

5454
def method_missing(method, *_args)
55-
desired_class = "Selenium::DevTools::V#{Selenium::DevTools.version}::#{method.capitalize}"
55+
namespace = "Selenium::DevTools::V#{Selenium::DevTools.version}"
56+
methods_to_classes = "#{namespace}::METHODS_TO_CLASSES"
57+
58+
desired_class = if Object.const_defined?(methods_to_classes)
59+
# selenium-devtools 0.113 and newer
60+
"#{namespace}::#{Object.const_get(methods_to_classes)[method]}"
61+
else
62+
# selenium-devtools 0.112 and older
63+
"#{namespace}::#{method.capitalize}}"
64+
end
65+
5666
return unless Object.const_defined?(desired_class)
5767

5868
self.class.class_eval do

rb/spec/integration/selenium/webdriver/devtools_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ module WebDriver
2929
expect(driver.title).to eq('XHTML Test Page')
3030
end
3131

32+
it 'maps methods to classes' do
33+
expect(driver.devtools.css).not_to be_nil
34+
expect(driver.devtools.dom).not_to be_nil
35+
expect(driver.devtools.dom_debugger).not_to be_nil
36+
end
37+
3238
context 'when the devtools version is too high' do
3339
let(:existing_devtools_version) { driver.send(:devtools_version) }
3440
let(:imaginary_devtools_version) { existing_devtools_version + 1 }

0 commit comments

Comments
 (0)