Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/remote/features.rb
4014 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
module Selenium
21
module WebDriver
22
module Remote
23
module Features
24
REMOTE_COMMANDS = {
25
upload_file: [:post, 'session/:session_id/se/file'],
26
get_downloadable_files: [:get, 'session/:session_id/se/files'],
27
download_file: [:post, 'session/:session_id/se/files'],
28
delete_downloadable_files: [:delete, 'session/:session_id/se/files'],
29
fire_session_event: [:post, 'session/:session_id/se/event']
30
}.freeze
31
32
def add_commands(commands)
33
@command_list = command_list.merge(commands)
34
end
35
36
def command_list
37
@command_list ||= REMOTE_COMMANDS
38
end
39
40
def commands(command)
41
command_list[command]
42
end
43
44
def upload(local_file)
45
unless File.file?(local_file)
46
WebDriver.logger.error("File detector only works with files. #{local_file.inspect} isn`t a file!",
47
id: :file_detector)
48
raise Error::WebDriverError, "You are trying to upload something that isn't a file."
49
end
50
51
execute :upload_file, {}, {file: Zipper.zip_file(local_file)}
52
end
53
54
def upload_if_necessary(keys)
55
local_files = keys.first&.split("\n")&.filter_map { |key| @file_detector.call(Array(key)) }
56
return keys unless local_files&.any?
57
58
keys = local_files.map { |local_file| upload(local_file) }
59
Array(keys.join("\n"))
60
end
61
62
def downloadable_files
63
execute :get_downloadable_files
64
end
65
66
def download_file(name)
67
execute :download_file, {}, {name: name}
68
end
69
70
def delete_downloadable_files
71
execute :delete_downloadable_files
72
end
73
74
#
75
# Fires a custom session event to the remote server event bus.
76
# This allows test code to trigger server-side utilities that subscribe to
77
# the event bus.
78
#
79
# @param [String] event_type The type of event (e.g., "test:failed", "log:collect")
80
# @param [Hash] payload Optional data to include with the event
81
# @return [Hash] Response data including success status, event type, and timestamp
82
#
83
# @example Fire a simple event
84
# driver.fire_session_event("test:started")
85
#
86
# @example Fire an event with payload
87
# driver.fire_session_event("test:failed", {
88
# testName: "LoginTest",
89
# error: "Element not found"
90
# })
91
#
92
def fire_session_event(event_type, payload = nil)
93
params = {eventType: event_type}
94
params[:payload] = payload if payload
95
execute :fire_session_event, {}, params
96
end
97
end
98
end # Remote
99
end # WebDriver
100
end # Selenium
101
102