Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/profile_helper.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
module Selenium
21
module WebDriver
22
#
23
# @api private
24
#
25
# Common methods for Chrome::Profile and Firefox::Profile
26
# Includers must implement #layout_on_disk
27
#
28
29
module ProfileHelper
30
def self.included(base)
31
base.extend ClassMethods
32
end
33
34
def self.decoded(json)
35
JSON.parse(json).fetch('zip')
36
end
37
38
def encoded
39
Zipper.zip(layout_on_disk)
40
end
41
42
def as_json(*)
43
{'zip' => encoded}
44
end
45
46
def to_json(*)
47
JSON.generate as_json
48
end
49
50
private
51
52
def create_tmp_copy(directory)
53
tmp_directory = Dir.mktmpdir('webdriver-rb-profilecopy')
54
55
# TODO: must be a better way..
56
FileUtils.rm_rf tmp_directory
57
FileUtils.mkdir_p File.dirname(tmp_directory), mode: 0o700
58
FileUtils.cp_r directory, tmp_directory
59
60
tmp_directory
61
end
62
63
def verify_model(model)
64
return unless model
65
66
raise Errno::ENOENT, model unless File.exist?(model)
67
raise Errno::ENOTDIR, model unless File.directory?(model)
68
69
model
70
end
71
72
module ClassMethods
73
def from_json(json)
74
data = decoded(json)
75
76
Tempfile.create do |zip_path|
77
File.open(zip_path, 'wb') { |zip_file| zip_file << Base64.decode64(data) }
78
79
new Zipper.unzip(zip_path)
80
end
81
end
82
end # ClassMethods
83
end # ProfileHelper
84
end # WebDriver
85
end # Selenium
86
87