Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/firefox/profile_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
module Firefox
25
describe Profile, only: {browser: %i[firefox]} do
26
let(:profile) { described_class.new }
27
28
def read_generated_prefs(from = nil)
29
prof = from || profile
30
dir = prof.layout_on_disk
31
32
File.read(File.join(dir, 'user.js'))
33
end
34
35
it '#from_name' do
36
ini = instance_double(ProfilesIni)
37
allow(described_class).to receive(:ini).and_return(ini)
38
allow(ini).to receive(:[]).and_return('not nil')
39
described_class.from_name('default')
40
41
expect(ini).to have_received(:[]).with('default')
42
end
43
44
it 'uses default preferences' do
45
expect(read_generated_prefs).to include('user_pref("browser.newtabpage.enabled", false)',
46
'user_pref("browser.startup.homepage", "about:blank")',
47
'user_pref("startup.homepage_welcome_url", "about:blank")',
48
'user_pref("browser.usedOnWindows10.introURL", "about:blank")',
49
'user_pref("network.captive-portal-service.enabled", false)',
50
'user_pref("security.csp.enable", false)')
51
end
52
53
it 'can override welcome page' do
54
profile['startup.homepage_welcome_url'] = 'http://google.com'
55
56
expect(read_generated_prefs).to include('user_pref("browser.startup.homepage", "about:blank")',
57
'user_pref("startup.homepage_welcome_url", "http://google.com")')
58
end
59
60
it 'sets additional preferences' do
61
profile['foo.number'] = 123
62
profile['foo.boolean'] = true
63
profile['foo.string'] = 'bar'
64
65
expect(read_generated_prefs).to include('user_pref("foo.number", 123)',
66
'user_pref("foo.boolean", true)',
67
'user_pref("foo.string", "bar")')
68
end
69
70
it 'is serializable to JSON' do
71
profile['foo.boolean'] = true
72
73
new_profile = described_class.from_json(profile.to_json)
74
expect(read_generated_prefs(new_profile)).to include('user_pref("foo.boolean", true)')
75
end
76
77
it 'properlies handle escaped characters' do
78
profile['foo'] = 'C:\\r\\n'
79
80
expect(read_generated_prefs).to include('user_pref("foo", "C:\\\\r\\\\n");')
81
end
82
83
it 'lets the user override some specific prefs' do
84
profile['browser.startup.page'] = 'http://example.com'
85
86
expect(read_generated_prefs).to include(%{user_pref("browser.startup.page", "http://example.com")})
87
end
88
89
it 'raises an error if the value given is not a string, number or boolean' do
90
expect { profile['foo.bar'] = [] }.to raise_error(TypeError)
91
end
92
93
it 'raises an error if the value is already stringified' do
94
expect { profile['foo.bar'] = '"stringified"' }.to raise_error(ArgumentError)
95
end
96
97
it 'can configure a manual proxy' do
98
proxy = Proxy.new(
99
http: 'foo:123',
100
ftp: 'bar:234',
101
ssl: 'baz:345',
102
no_proxy: 'localhost'
103
)
104
105
profile.proxy = proxy
106
expect(read_generated_prefs).to include('user_pref("network.proxy.http", "foo")',
107
'user_pref("network.proxy.http_port", 123)',
108
'user_pref("network.proxy.ftp", "bar")',
109
'user_pref("network.proxy.ftp_port", 234)',
110
'user_pref("network.proxy.ssl", "baz")',
111
'user_pref("network.proxy.ssl_port", 345)',
112
'user_pref("network.proxy.no_proxies_on", "localhost")',
113
'user_pref("network.proxy.type", 1)')
114
end
115
116
it 'can configure a PAC proxy' do
117
profile.proxy = Proxy.new(pac: 'http://foo/bar.pac')
118
119
expect(read_generated_prefs).to include('user_pref("network.proxy.autoconfig_url", "http://foo/bar.pac"',
120
'user_pref("network.proxy.type", 2)')
121
end
122
123
it 'can configure an auto-detected proxy' do
124
profile.proxy = Proxy.new(auto_detect: true)
125
126
expect(read_generated_prefs).to include('user_pref("network.proxy.type", 4)')
127
end
128
129
it 'can install extension' do
130
firebug = File.expand_path('../../../../../../third_party/firebug/firebug-1.5.0-fx.xpi', __dir__)
131
profile.add_extension(firebug)
132
extension_directory = File.expand_path('extensions/[email protected]', profile.layout_on_disk)
133
expect(Dir.exist?(extension_directory)).to be(true)
134
end
135
136
it 'can install web extension without id' do
137
mooltipass = File.expand_path('../../../../../../third_party/firebug/mooltipass-1.1.87.xpi', __dir__)
138
profile.add_extension(mooltipass)
139
extension_directory = File.expand_path('extensions/[email protected]', profile.layout_on_disk)
140
expect(Dir.exist?(extension_directory)).to be(true)
141
end
142
143
it 'can install web extension with id' do
144
ext = File.expand_path('../../../../../../third_party/firebug/favourite_colour-1.1-an+fx.xpi', __dir__)
145
profile.add_extension(ext)
146
extension_directory = File.expand_path('extensions/[email protected]',
147
profile.layout_on_disk)
148
expect(Dir.exist?(extension_directory)).to be(true)
149
end
150
end
151
end # Firefox
152
end # WebDriver
153
end # Selenium
154
155