Skip to content

Commit 873728e

Browse files
committed
Use duck-typing for pinned script execution
1 parent 8d656fe commit 873728e

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

rb/lib/selenium/webdriver/common/driver_extensions/has_pinned_scripts.rb

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@ module WebDriver
2222
module DriverExtensions
2323
module HasPinnedScripts
2424

25+
#
26+
# Returns the list of all pinned scripts.
27+
#
28+
# @return [Array<DevTools::PinnedScript>]
29+
#
30+
31+
def pinned_scripts
32+
@pinned_scripts ||= []
33+
end
34+
35+
#
36+
# Pins JavaScript snippet that is available during the whole
37+
# session on every page. This allows to store and call
38+
# scripts without sending them over the wire every time.
39+
#
40+
# @example
41+
# script = driver.pin_script('return window.location.href')
42+
# driver.execute_script(script)
43+
# # navigate to a new page
44+
# driver.execute_script(script)
45+
#
46+
# @param [String] script
47+
# @return [DevTools::PinnedScript]
48+
#
49+
2550
def pin_script(script)
2651
script = DevTools::PinnedScript.new(script)
2752
pinned_scripts << script
@@ -34,26 +59,18 @@ def pin_script(script)
3459
script
3560
end
3661

62+
#
63+
# Unpins script making it undefined for the subsequent calls.
64+
#
65+
# @param [DevTools::PinnedScript]
66+
#
67+
3768
def unpin_script(script)
3869
devtools.runtime.evaluate(expression: script.remove)
3970
devtools.page.remove_script_to_evaluate_on_new_document(identifier: script.devtools_identifier)
4071
pinned_scripts.delete(script)
4172
end
4273

43-
def pinned_scripts
44-
@pinned_scripts ||= []
45-
end
46-
47-
def execute_script(script, *args)
48-
script = script.call if script.is_a?(DevTools::PinnedScript)
49-
super(script, *args)
50-
end
51-
52-
def execute_async_script(script, *args)
53-
script = script.call if script.is_a?(DevTools::PinnedScript)
54-
super(script, *args)
55-
end
56-
5774
end # HasPinnedScripts
5875
end # DriverExtensions
5976
end # WebDriver

rb/lib/selenium/webdriver/devtools/pinned_script.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,26 @@ def initialize(script)
2929
@script = script
3030
end
3131

32+
#
33+
# @api private
34+
#
35+
3236
def callable
3337
"function __webdriver_#{key}(arguments) { #{script} }"
3438
end
3539

36-
def call
37-
"return __webdriver_#{key}(arguments)"
40+
#
41+
# @api private
42+
#
43+
44+
def to_json(*)
45+
%{"return __webdriver_#{key}(arguments)"}
3846
end
3947

48+
#
49+
# @api private
50+
#
51+
4052
def remove
4153
"__webdriver_#{key} = undefined"
4254
end

0 commit comments

Comments
 (0)