Skip to content

Commit 4c06350

Browse files
committed
spec helper manages setup and teardown
1 parent d5d2d5a commit 4c06350

File tree

5 files changed

+46
-96
lines changed

5 files changed

+46
-96
lines changed

examples/ruby/spec/browsers/chrome_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
RSpec.describe 'Chrome' do
66
describe 'Options' do
7-
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
8-
97
it 'basic options' do
10-
options = default_chrome_options
8+
options = Selenium::WebDriver::Chrome::Options.new
119
@driver = Selenium::WebDriver.for :chrome, options: options
1210
end
1311

@@ -114,18 +112,18 @@
114112
end
115113

116114
describe 'Special Features' do
115+
let(:driver) { start_driver }
116+
117117
it 'casts' do
118-
@driver = Selenium::WebDriver.for :chrome
119-
sinks = @driver.cast_sinks
118+
sinks = driver.cast_sinks
120119
unless sinks.empty?
121120
device_name = sinks.first['name']
122-
@driver.start_cast_tab_mirroring(device_name)
123-
expect { @driver.stop_casting(device_name) }.not_to raise_exception
121+
driver.start_cast_tab_mirroring(device_name)
122+
expect { driver.stop_casting(device_name) }.not_to raise_exception
124123
end
125124
end
126125

127126
it 'gets and sets network conditions' do
128-
@driver = Selenium::WebDriver.for :chrome
129127
@driver.network_conditions = {offline: false, latency: 100, throughput: 200}
130128
expect(@driver.network_conditions).to eq(
131129
'offline' => false,
@@ -136,7 +134,6 @@
136134
end
137135

138136
it 'gets the browser logs' do
139-
@driver = Selenium::WebDriver.for :chrome
140137
@driver.navigate.to 'https://www.selenium.dev/selenium/web/'
141138
sleep 1
142139
logs = @driver.logs.get(:browser)
@@ -145,17 +142,20 @@
145142
end
146143

147144
it 'sets permissions' do
148-
@driver = Selenium::WebDriver.for :chrome
149145
@driver.navigate.to 'https://www.selenium.dev/selenium/web/'
146+
150147
@driver.add_permission('camera', 'denied')
151148
@driver.add_permissions('clipboard-read' => 'denied', 'clipboard-write' => 'prompt')
149+
152150
expect(permission('camera')).to eq('denied')
153151
expect(permission('clipboard-read')).to eq('denied')
154152
expect(permission('clipboard-write')).to eq('prompt')
155153
end
156154
end
157155

158-
def driver_finder
156+
private
157+
158+
def chrome_location
159159
options = default_chrome_options
160160
service = Selenium::WebDriver::Service.chrome
161161
finder = Selenium::WebDriver::DriverFinder.new(options, service)

examples/ruby/spec/browsers/internet_explorer_spec.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,22 @@
2727

2828
it 'sets the file upload dialog timeout' do
2929
@options.file_upload_dialog_timeout = 2000
30-
driver = Selenium::WebDriver.for(:ie, options: @options)
31-
driver.quit
30+
@driver = Selenium::WebDriver.for(:ie, options: @options)
3231
end
3332

3433
it 'ensures a clean session' do
3534
@options.ensure_clean_session = true
36-
driver = Selenium::WebDriver.for(:ie, options: @options)
37-
driver.quit
35+
@driver = Selenium::WebDriver.for(:ie, options: @options)
3836
end
3937

4038
it 'ignores the zoom setting' do
4139
@options.ignore_zoom_level = true
42-
driver = Selenium::WebDriver.for(:ie, options: @options)
43-
driver.quit
40+
@driver = Selenium::WebDriver.for(:ie, options: @options)
4441
end
4542

4643
it 'ignores the protected mode settings' do
4744
@options.ignore_protected_mode_settings = true
48-
driver = Selenium::WebDriver.for(:ie, options: @options)
49-
driver.quit
45+
@driver = Selenium::WebDriver.for(:ie, options: @options)
5046
end
5147

5248
it 'adds the silent option' do
@@ -56,12 +52,12 @@
5652

5753
it 'sets the command line options' do
5854
@options.add_argument('-k')
59-
Selenium::WebDriver.for(:ie, options: @options)
55+
@driver = Selenium::WebDriver.for(:ie, options: @options)
6056
end
6157

6258
it 'launches ie with the create process api', skip: 'When using with IE 8 or higher, it needs a registry value' do
6359
@options.force_create_process_api = true
64-
Selenium::WebDriver.for(:ie, options: @options)
60+
@driver = Selenium::WebDriver.for(:ie, options: @options)
6561
expect(@options.instance_variable_get(:@options)['force_create_process_api'])
6662
.to eq({force_create_process_api: true})
6763
end

examples/ruby/spec/drivers/http_client_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
it 'uses the custom http client' do
1212
client = Selenium::WebDriver::Remote::Http::Default.new
13-
driver = Selenium::WebDriver.for :chrome, http_client: client
14-
driver.get(url)
15-
driver.quit
13+
@driver = Selenium::WebDriver.for :chrome, http_client: client
1614
end
1715
end

examples/ruby/spec/drivers/options_spec.rb

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,129 +5,104 @@
55
RSpec.describe 'Chrome' do
66
describe 'Driver Options' do
77
let(:chrome_location) { driver_finder && ENV.fetch('CHROME_BIN', nil) }
8-
let(:url) { 'https://www.selenium.dev/selenium/web/' }
8+
let(:url) { 'https://www.selenium.dev/selenium/web/web-form.html' }
99

1010
it 'page load strategy normal' do
1111
options = default_chrome_options
1212
options.page_load_strategy = :normal
1313

14-
driver = Selenium::WebDriver.for :chrome, options: options
15-
driver.get(url)
16-
driver.quit
14+
@driver = Selenium::WebDriver.for :chrome, options: options
15+
@driver.get(url)
1716
end
1817

1918
it 'page load strategy eager' do
2019
options = default_chrome_options
2120
options.page_load_strategy = :eager
2221

23-
driver = Selenium::WebDriver.for :chrome, options: options
24-
driver.get(url)
25-
driver.quit
22+
@driver = Selenium::WebDriver.for :chrome, options: options
23+
@driver.get(url)
2624
end
2725

2826
it 'page load strategy none' do
2927
options = default_chrome_options
3028
options.page_load_strategy = :none
3129

32-
driver = Selenium::WebDriver.for :chrome, options: options
33-
driver.get(url)
34-
driver.quit
30+
@driver = Selenium::WebDriver.for :chrome, options: options
31+
@driver.get(url)
3532
end
3633

37-
it 'sets remote capabilities', skip: 'this is example code that will not execute' do
34+
it 'sets remote capabilities' do
3835
options = Selenium::WebDriver::Options.firefox
3936
options.platform_name = 'Windows 10'
4037
options.browser_version = 'latest'
4138
cloud_options = {}
4239
cloud_options[:build] = my_test_build
4340
cloud_options[:name] = my_test_name
4441
options.add_option('cloud:options', cloud_options)
45-
driver = Selenium::WebDriver.for :remote, capabilities: options
46-
driver.get(url)
47-
driver.quit
4842
end
4943

5044
it 'accepts untrusted certificates' do
5145
options = default_chrome_options
5246
options.accept_insecure_certs = true
5347

54-
driver = Selenium::WebDriver.for :chrome, options: options
55-
driver.get(url)
56-
driver.quit
48+
@driver = Selenium::WebDriver.for :chrome, options: options
49+
@driver.get(url)
5750
end
5851

5952
it 'sets unhandled prompt behavior' do
6053
options = default_chrome_options
6154
options.unhandled_prompt_behavior = :accept
6255

63-
driver = Selenium::WebDriver.for :chrome, options: options
64-
driver.get(url)
65-
driver.quit
56+
@driver = Selenium::WebDriver.for :chrome, options: options
57+
@driver.get(url)
6658
end
6759

6860
it 'sets window rect' do
6961
options = Selenium::WebDriver::Options.firefox
7062
options.set_window_rect = true
7163

72-
driver = Selenium::WebDriver.for :firefox, options: options
73-
driver.get(url)
74-
driver.quit
64+
@driver = Selenium::WebDriver.for :firefox, options: options
65+
@driver.get(url)
7566
end
7667

7768
it 'sets strict file interactability' do
7869
options = default_chrome_options
7970
options.strict_file_interactability = true
8071

81-
driver = Selenium::WebDriver.for :chrome, options: options
82-
driver.get(url)
83-
driver.quit
72+
@driver = Selenium::WebDriver.for :chrome, options: options
73+
@driver.get(url)
8474
end
8575

8676
it 'sets the proxy' do
8777
options = default_chrome_options
8878
options.proxy = Selenium::WebDriver::Proxy.new(http: 'myproxy.com:8080')
8979

90-
driver = Selenium::WebDriver.for :chrome, options: options
91-
driver.get(url)
92-
driver.quit
80+
@driver = Selenium::WebDriver.for :chrome, options: options
81+
@driver.get(url)
9382
end
9483

9584
it 'sets the implicit timeout' do
9685
options = default_chrome_options
97-
options.timeouts = {implicit: 1}
86+
options.timeouts = {implicit: 1500}
9887

99-
driver = Selenium::WebDriver.for :chrome, options: options
100-
driver.get(url)
101-
driver.quit
88+
@driver = Selenium::WebDriver.for :chrome, options: options
89+
@driver.get(url)
90+
@driver.find_element(id: 'my-text-id')
10291
end
10392

10493
it 'sets the page load timeout' do
10594
options = default_chrome_options
10695
options.timeouts = {page_load: 400_000}
10796

108-
driver = Selenium::WebDriver.for :chrome, options: options
109-
driver.get(url)
110-
driver.quit
97+
@driver = Selenium::WebDriver.for :chrome, options: options
98+
@driver.get(url)
11199
end
112100

113101
it 'sets the script timeout' do
114102
options = default_chrome_options
115103
options.timeouts = {script: 40_000}
116104

117-
driver = Selenium::WebDriver.for :chrome, options: options
118-
driver.get(url)
119-
driver.quit
120-
end
121-
122-
it 'sets capabilities in the pre-selenium 4 way', skip: 'this is example code that will not execute' do
123-
caps = Selenium::WebDriver::Remote::Capabilities.firefox
124-
caps[:platform] = 'Windows 10'
125-
caps[:version] = '92'
126-
caps[:build] = my_test_build
127-
caps[:name] = my_test_name
128-
driver = Selenium::WebDriver.for :remote, url: cloud_url, desired_capabilities: caps
129-
driver.get(url)
130-
driver.quit
105+
@driver = Selenium::WebDriver.for :chrome, options: options
131106
end
132107
end
133108
end

examples/ruby/spec/interactions/virtual_authenticator_spec.rb

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require 'selenium-webdriver'
44

55
RSpec.describe 'Virtual Authenticator' do
6+
let(:driver) {start_session}
7+
68
let(:encoded_private_key) do
79
'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDbBOu5Lhs4vpowbCnmCyLUpIE7JM9sm9QXzye2G+jr+Kr' \
810
'MsinWohEce47BFPJlTaDzHSvOW2eeunBO89ZcvvVc8RLz4qyQ8rO98xS1jtgqi1NcBPETDrtzthODu/gd0sjB2Tk3TLuBGV' \
@@ -39,29 +41,22 @@
3941

4042
it 'creates virtual authenticator' do
4143
options = Selenium::WebDriver::VirtualAuthenticatorOptions.new(protocol: :u2f, resident_key: false)
42-
driver = Selenium::WebDriver.for :chrome
4344
@authenticator = driver.add_virtual_authenticator(options)
4445

4546
expect(@authenticator.valid?).to eq true
46-
47-
driver.quit
4847
end
4948

5049
it 'removes virtual authenticator' do
5150
options = Selenium::WebDriver::VirtualAuthenticatorOptions.new
52-
driver = Selenium::WebDriver.for :chrome
5351
@authenticator = driver.add_virtual_authenticator(options)
5452
@authenticator.remove!
5553

5654
expect(@authenticator.valid?).to eq false
57-
58-
driver.quit
5955
end
6056

6157
it 'creates and add resident credential' do
6258
options = Selenium::WebDriver::VirtualAuthenticatorOptions.new(protocol: :ctap2, resident_key: true,
6359
user_verification: true, user_verified: true)
64-
driver = Selenium::WebDriver.for :chrome
6560
@authenticator = driver.add_virtual_authenticator(options)
6661

6762
resident_credential = Selenium::WebDriver::Credential.resident(id: [1, 2, 3, 4],
@@ -76,13 +71,10 @@
7671

7772
credential_id = credential_list.first.id
7873
expect(credential_id).to eq [1, 2, 3, 4]
79-
80-
driver.quit
8174
end
8275

8376
it 'does not support resident credential when authenticator uses u2f protocol' do
8477
options = Selenium::WebDriver::VirtualAuthenticatorOptions.new(protocol: :u2f, resident_key: true)
85-
driver = Selenium::WebDriver.for :chrome
8678
@authenticator = driver.add_virtual_authenticator(options)
8779

8880
resident_credential = Selenium::WebDriver::Credential.resident(id: [1, 2, 3, 4],
@@ -95,13 +87,10 @@
9587
expect {
9688
@authenticator.add_credential(resident_credential)
9789
}.to raise_error(Selenium::WebDriver::Error::InvalidArgumentError)
98-
99-
driver.quit
10090
end
10191

10292
it 'creates and add non-resident credential' do
10393
options = Selenium::WebDriver::VirtualAuthenticatorOptions.new(protocol: :u2f, resident_key: false)
104-
driver = Selenium::WebDriver.for :chrome
10594
@authenticator = driver.add_virtual_authenticator(options)
10695

10796
non_resident_credential = Selenium::WebDriver::Credential.non_resident(id: [1, 2, 3, 4],
@@ -114,15 +103,12 @@
114103

115104
expect(credentials.length).to eq 1
116105
expect(credentials.first.id).to eq [1, 2, 3, 4]
117-
118-
driver.quit
119106
end
120107

121108
it 'gets all credentials' do
122109
options = Selenium::WebDriver::VirtualAuthenticatorOptions.new(protocol: :ctap2, resident_key: true,
123110
user_verification: true, user_verified: true)
124-
driver = Selenium::WebDriver.for :chrome
125-
@authenticator = driver.add_virtual_authenticator(options)
111+
@authenticator = @driver.add_virtual_authenticator(options)
126112

127113
resident_credential = Selenium::WebDriver::Credential.resident(id: [1, 2, 3, 4],
128114
rp_id: 'localhost',
@@ -141,13 +127,10 @@
141127
credentials = @authenticator.credentials
142128
expect(credentials.length).to eq 2
143129
expect(credentials.first.id).to eq [1, 2, 3, 4]
144-
145-
driver.quit
146130
end
147131

148132
it 'removes all credentials' do
149133
options = Selenium::WebDriver::VirtualAuthenticatorOptions.new
150-
driver = Selenium::WebDriver.for :chrome
151134
@authenticator = driver.add_virtual_authenticator(options)
152135

153136
non_resident_credential = Selenium::WebDriver::Credential.non_resident(id: [1, 2, 3, 4],
@@ -160,7 +143,5 @@
160143

161144
@authenticator.remove_all_credentials
162145
expect(@authenticator.credentials).to be_empty
163-
164-
driver.quit
165146
end
166147
end

0 commit comments

Comments
 (0)