sauce_bindings 1.1.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,15 +13,13 @@ module SauceBindings
13
13
  {browserName: 'chrome',
14
14
  browserVersion: 'latest',
15
15
  platformName: 'Windows 10',
16
- 'sauce:options': {build: 'TEMP BUILD: 11'}}
16
+ 'sauce:options': {username: 'foo',
17
+ accessKey: '123',
18
+ build: 'TEMP BUILD: 11'}}
17
19
  end
18
20
 
19
- def expect_request
20
- se3 = {desiredCapabilities: default_capabilities,
21
- capabilities: {firstMatch: [default_capabilities]}}.to_json
22
- se4 = {capabilities: {alwaysMatch: default_capabilities}}.to_json
23
-
24
- body = Selenium::WebDriver::VERSION[0] == '3' ? se3 : se4
21
+ def expect_request(caps = default_capabilities)
22
+ body = {capabilities: {alwaysMatch: caps}}.to_json
25
23
 
26
24
  endpoint ||= 'https://ondemand.us-west-1.saucelabs.com/wd/hub/session'
27
25
  stub_request(:post, endpoint).with(body: body).to_return(valid_response)
@@ -29,115 +27,165 @@ module SauceBindings
29
27
 
30
28
  before do
31
29
  allow_any_instance_of(Selenium::WebDriver::Remote::Http::Default).to receive(:use_proxy?).and_return(false)
32
- allow(ENV).to receive(:[]).with('BUILD_TAG').and_return('')
33
- allow(ENV).to receive(:[]).with('BUILD_NAME').and_return('TEMP BUILD')
34
- allow(ENV).to receive(:[]).with('BUILD_NUMBER').and_return('11')
35
- allow(ENV).to receive(:[]).with('SAUCE_USERNAME').and_return('foo')
36
- allow(ENV).to receive(:[]).with('SAUCE_ACCESS_KEY').and_return('123')
37
30
  end
38
31
 
39
32
  describe '#new' do
40
33
  it 'creates default Options instance if none is provided' do
41
- session = Session.new
42
-
43
- expected_results = {url: 'https://foo:123@ondemand.us-west-1.saucelabs.com:443/wd/hub',
44
- desired_capabilities: {'browserName' => 'chrome',
45
- 'browserVersion' => 'latest',
46
- 'platformName' => 'Windows 10',
47
- 'sauce:options' => {'build' => 'TEMP BUILD: 11'}}}
48
- expect(session.to_selenium).to eq expected_results
34
+ ClimateControl.modify(**BUILD_ENV) do
35
+ @session = Session.new
36
+ end
37
+
38
+ expected_caps = {'browserName' => 'chrome',
39
+ 'browserVersion' => 'latest',
40
+ 'platformName' => 'Windows 10',
41
+ 'sauce:options' => {'build' => 'TEMP BUILD: 11',
42
+ 'username' => 'foo',
43
+ 'accessKey' => '123'}}
44
+ ClimateControl.modify(**SAUCE_ACCESS) do
45
+ @results = @session.to_selenium
46
+ end
47
+ expect(@results[:url]).to eq 'https://ondemand.us-west-1.saucelabs.com/wd/hub'
48
+ expect(@results[:capabilities].as_json).to eq expected_caps
49
49
  end
50
50
 
51
51
  it 'uses provided Options class' do
52
- sauce_opts = Options.chrome(browser_version: '123',
53
- platform_name: 'Mac',
54
- idle_timeout: 4)
55
- session = Session.new(sauce_opts)
56
-
57
- expected_results = {url: 'https://foo:123@ondemand.us-west-1.saucelabs.com:443/wd/hub',
58
- desired_capabilities: {'browserName' => 'chrome',
59
- 'browserVersion' => '123',
60
- 'platformName' => 'Mac',
61
- 'sauce:options' => {'idleTimeout' => 4,
62
- 'build' => 'TEMP BUILD: 11'}}}
63
- expect(session.to_selenium).to eq expected_results
52
+ ClimateControl.modify(**BUILD_ENV) do
53
+ @sauce_opts = Options.chrome(browser_version: '123',
54
+ platform_name: 'Mac',
55
+ idle_timeout: 4)
56
+ end
57
+
58
+ session = Session.new(@sauce_opts)
59
+
60
+ expected_caps = {'browserName' => 'chrome',
61
+ 'browserVersion' => '123',
62
+ 'platformName' => 'Mac',
63
+ 'sauce:options' => {'idleTimeout' => 4,
64
+ 'build' => 'TEMP BUILD: 11',
65
+ 'username' => 'foo',
66
+ 'accessKey' => '123'}}
67
+
68
+ ClimateControl.modify(**SAUCE_ACCESS) do
69
+ @results = session.to_selenium
70
+ end
71
+
72
+ expect(@results[:url]).to eq 'https://ondemand.us-west-1.saucelabs.com/wd/hub'
73
+ expect(@results[:capabilities].as_json).to eq expected_caps
64
74
  end
65
75
 
66
76
  it 'defaults to US West data Center' do
67
- session = Session.new
68
- expect(session.data_center).to eq :US_WEST
77
+ ClimateControl.modify(**BUILD_ENV) do
78
+ @session = Session.new
79
+ end
80
+
81
+ expect(@session.data_center).to eq :US_WEST
69
82
  end
70
83
 
71
84
  it 'uses provided Data Center' do
72
- session = Session.new(data_center: :EU_CENTRAL)
73
-
74
- expected_results = {url: 'https://foo:123@ondemand.eu-central-1.saucelabs.com:443/wd/hub',
75
- desired_capabilities: {'browserName' => 'chrome',
76
- 'browserVersion' => 'latest',
77
- 'platformName' => 'Windows 10',
78
- 'sauce:options' => {'build' => 'TEMP BUILD: 11'}}}
79
- expect(session.to_selenium).to eq expected_results
85
+ ClimateControl.modify(**BUILD_ENV) do
86
+ @session = Session.new(data_center: :EU_CENTRAL)
87
+ end
88
+
89
+ expected_caps = {'browserName' => 'chrome',
90
+ 'browserVersion' => 'latest',
91
+ 'platformName' => 'Windows 10',
92
+ 'sauce:options' => {'build' => 'TEMP BUILD: 11',
93
+ 'username' => 'foo',
94
+ 'accessKey' => '123'}}
95
+ ClimateControl.modify(**SAUCE_ACCESS) do
96
+ @results = @session.to_selenium
97
+ end
98
+
99
+ expect(@results[:url]).to eq 'https://ondemand.eu-central-1.saucelabs.com/wd/hub'
100
+ expect(@results[:capabilities].as_json).to eq expected_caps
80
101
  end
81
102
 
82
103
  it 'uses provided Event Listener' do
83
104
  listener = instance_double(Selenium::WebDriver::Support::AbstractEventListener)
84
- session = Session.new(listener: listener)
105
+ ClimateControl.modify(**BUILD_ENV) do
106
+ @session = Session.new(listener: listener)
107
+ end
85
108
 
86
- expect(session.to_selenium[:listener]).to eq listener
109
+ ClimateControl.modify(**SAUCE_ACCESS) do
110
+ expect(@session.to_selenium[:listener]).to eq listener
111
+ end
87
112
  end
88
113
 
89
114
  it 'uses provided HTTP Client' do
90
115
  http_client = instance_double(Selenium::WebDriver::Remote::Http::Default)
91
- session = Session.new(http_client: http_client)
92
116
 
93
- expect(session.to_selenium[:http_client]).to eq http_client
117
+ ClimateControl.modify(**BUILD_ENV) do
118
+ @session = Session.new(http_client: http_client)
119
+ end
120
+
121
+ ClimateControl.modify(**SAUCE_ACCESS) do
122
+ expect(@session.to_selenium[:http_client]).to eq http_client
123
+ end
94
124
  end
95
125
 
96
126
  it 'raises exception if data center is invalid' do
97
- expect { Session.new(data_center: :FOO) }.to raise_exception(ArgumentError)
127
+ ClimateControl.modify(**BUILD_ENV) do
128
+ expect { Session.new(data_center: :FOO) }.to raise_exception(ArgumentError)
129
+ end
98
130
  end
99
131
  end
100
132
 
101
133
  describe '#start' do
102
134
  it 'starts the session and returns Selenium Driver instance' do
103
135
  expect_request
136
+ ClimateControl.modify(**BUILD_ENV, **SAUCE_ACCESS) do
137
+ @driver = Session.new.start
138
+ end
104
139
 
105
- driver = Session.new.start
106
- expect(driver).to be_a Selenium::WebDriver::Driver
107
- end
108
-
109
- it 'uses username and access key from ENV' do
110
- session = Session.new
111
-
112
- expect(session.url).to include('foo:123')
140
+ expect(@driver).to be_a Selenium::WebDriver::Driver
113
141
  end
114
142
 
115
143
  it 'raises exception if no username set' do
116
- allow(ENV).to receive(:[]).with('SAUCE_USERNAME')
117
-
118
- expect { Session.new.start }.to raise_exception(ArgumentError)
144
+ ClimateControl.modify SAUCE_USERNAME: nil do
145
+ expect { Session.new.start }.to raise_exception(ArgumentError)
146
+ end
119
147
  end
120
148
 
121
149
  it 'raises exception if no access key set' do
122
- allow(ENV).to receive(:[]).with('SAUCE_ACCESS_KEY')
123
-
124
- expect { Session.new.start }.to raise_exception(ArgumentError)
150
+ ClimateControl.modify SAUCE_ACCESS_KEY: nil do
151
+ expect { Session.new.start }.to raise_exception(ArgumentError)
152
+ end
125
153
  end
126
154
  end
127
155
 
128
156
  describe '#stop' do
129
157
  it 'quits the driver' do
130
- driver = instance_double(Selenium::WebDriver::Remote::Driver, session_id: '1234')
131
- allow(Selenium::WebDriver::Driver).to receive(:for).and_return(driver)
132
- allow(driver).to receive :quit
133
- allow(SauceWhisk::Jobs).to receive(:change_status).with('1234', true)
158
+ expect_request
159
+ ClimateControl.modify(**BUILD_ENV) do
160
+ @session = Session.new
161
+ end
134
162
 
135
- session = Session.new
136
- session.start
137
- session.stop(true)
163
+ ClimateControl.modify(**SAUCE_ACCESS) do
164
+ @driver = @session.start
165
+ end
166
+
167
+ allow(@session).to receive :print_results
168
+ allow(@driver).to receive :quit
169
+ allow(SauceWhisk::Jobs).to receive(:change_status).with('0', false)
170
+ @session.stop(false)
171
+
172
+ expect(@driver).to have_received(:quit)
173
+ expect(SauceWhisk::Jobs).to have_received(:change_status).with('0', false)
174
+ end
175
+
176
+ it 'raises error when argument value is not boolean' do
177
+ expect_request
178
+
179
+ ClimateControl.modify(**BUILD_ENV) do
180
+ @session = Session.new
181
+ end
182
+
183
+ ClimateControl.modify(**SAUCE_ACCESS) do
184
+ @driver = @session.start
185
+ end
186
+ allow(@driver).to receive :quit
138
187
 
139
- expect(driver).to have_received(:quit)
140
- expect(SauceWhisk::Jobs).to have_received(:change_status).with('1234', true)
188
+ expect { @session.stop('String') }.to raise_error(ArgumentError)
141
189
  end
142
190
  end
143
191
 
@@ -146,7 +194,7 @@ module SauceBindings
146
194
  session = Session.new
147
195
  session.data_center = :US_EAST
148
196
 
149
- expect(session.url).to eq('https://foo:123@ondemand.us-east-1.saucelabs.com:443/wd/hub')
197
+ expect(session.url).to eq('https://ondemand.us-east-1.saucelabs.com/wd/hub')
150
198
  end
151
199
 
152
200
  it 'raises exception if data center is invalid' do
@@ -166,27 +214,332 @@ module SauceBindings
166
214
  end
167
215
  end
168
216
 
169
- describe '#listner=' do
217
+ describe '#listener=' do
170
218
  it 'uses provided Event Listener' do
171
219
  listener = instance_double(Selenium::WebDriver::Support::AbstractEventListener)
172
- session = Session.new
173
- session.listener = listener
220
+ ClimateControl.modify(**BUILD_ENV) do
221
+ @session = Session.new
222
+ end
223
+
224
+ @session.listener = listener
174
225
 
175
- expect(session.to_selenium[:listener]).to eq listener
226
+ ClimateControl.modify(**SAUCE_ACCESS) do
227
+ expect(@session.to_selenium[:listener]).to eq listener
228
+ end
176
229
  end
177
230
  end
178
231
 
179
232
  describe '#url=' do
180
233
  it 'allows user to override default URL' do
181
- session = Session.new
182
- session.url = 'https://bar:321@mycustomurl/foo/wd/hub:8080'
183
-
184
- expected_results = {url: 'https://bar:321@mycustomurl/foo/wd/hub:8080',
185
- desired_capabilities: {'browserName' => 'chrome',
186
- 'browserVersion' => 'latest',
187
- 'platformName' => 'Windows 10',
188
- 'sauce:options' => {'build' => 'TEMP BUILD: 11'}}}
189
- expect(session.to_selenium).to eq expected_results
234
+ ClimateControl.modify(**BUILD_ENV) do
235
+ @session = Session.new
236
+ end
237
+
238
+ @session.url = 'https://mycustomurl/foo/wd/hub:8080'
239
+
240
+ expected_caps = {'browserName' => 'chrome',
241
+ 'browserVersion' => 'latest',
242
+ 'platformName' => 'Windows 10',
243
+ 'sauce:options' => {'build' => 'TEMP BUILD: 11',
244
+ 'username' => 'foo',
245
+ 'accessKey' => '123'}}
246
+
247
+ ClimateControl.modify(**SAUCE_ACCESS) do
248
+ @results = @session.to_selenium
249
+ end
250
+ expect(@results[:url]).to eq 'https://mycustomurl/foo/wd/hub:8080'
251
+ expect(@results[:capabilities].as_json).to eq expected_caps
252
+ end
253
+ end
254
+
255
+ describe '#annotate' do
256
+ it 'raises exception if session not started' do
257
+ ClimateControl.modify(**BUILD_ENV) do
258
+ @session = Session.new
259
+ end
260
+
261
+ expect { @session.annotate('Comment') }.to raise_error(SessionNotStartedError)
262
+ end
263
+
264
+ it 'accepts annotation' do
265
+ expect_request
266
+ ClimateControl.modify(**BUILD_ENV) do
267
+ @session = Session.new
268
+ end
269
+
270
+ ClimateControl.modify(**SAUCE_ACCESS) do
271
+ @driver = @session.start
272
+ end
273
+ allow(@driver).to receive :quit
274
+ allow(@driver).to receive(:execute_script)
275
+
276
+ @session.annotate('comment')
277
+
278
+ expect(@driver).to have_received(:execute_script).with('sauce:context=comment')
279
+ end
280
+ end
281
+
282
+ describe '#pause' do
283
+ it 'raises exception if session not started' do
284
+ ClimateControl.modify(**BUILD_ENV) do
285
+ @session = Session.new
286
+ end
287
+
288
+ expect { @session.pause }.to raise_error(SessionNotStartedError)
289
+ end
290
+
291
+ it 'pauses test' do
292
+ expect_request
293
+ ClimateControl.modify(**BUILD_ENV) do
294
+ @session = Session.new
295
+ end
296
+
297
+ ClimateControl.modify(**SAUCE_ACCESS) do
298
+ @driver = @session.start
299
+ end
300
+ allow(@driver).to receive :quit
301
+ allow(@driver).to receive(:execute_script)
302
+
303
+ message = "\nThis test has been stopped; no more driver commands will be accepted\n\n" \
304
+ "You can take manual control of the test from the Sauce Labs UI here: https://app.saucelabs.com/tests/0\n"
305
+ expect { @session.pause }.to output(message).to_stdout
306
+
307
+ expect(@driver).to have_received(:execute_script).with('sauce: break')
308
+ end
309
+ end
310
+
311
+ describe '#enable_logs' do
312
+ it 'raises exception if session not started' do
313
+ ClimateControl.modify(**BUILD_ENV) do
314
+ @session = Session.new
315
+ end
316
+
317
+ expect { @session.enable_logging }.to raise_error(SessionNotStartedError)
318
+ end
319
+
320
+ it 'enables logs' do
321
+ expect_request
322
+ ClimateControl.modify(**BUILD_ENV) do
323
+ @session = Session.new
324
+ end
325
+
326
+ ClimateControl.modify(**SAUCE_ACCESS) do
327
+ @driver = @session.start
328
+ end
329
+ allow(@driver).to receive :quit
330
+ allow(@driver).to receive(:execute_script)
331
+
332
+ @session.enable_logging
333
+
334
+ expect(@driver).to have_received(:execute_script).with('sauce: enable log')
335
+ end
336
+ end
337
+
338
+ describe '#disable_logs' do
339
+ it 'raises exception if session not started' do
340
+ ClimateControl.modify(**BUILD_ENV) do
341
+ @session = Session.new
342
+ end
343
+
344
+ expect { @session.disable_logging }.to raise_error(SessionNotStartedError)
345
+ end
346
+
347
+ it 'disables logs' do
348
+ expect_request
349
+ ClimateControl.modify(**BUILD_ENV) do
350
+ @session = Session.new
351
+ end
352
+
353
+ ClimateControl.modify(**SAUCE_ACCESS) do
354
+ @driver = @session.start
355
+ end
356
+ allow(@driver).to receive :quit
357
+ allow(@driver).to receive(:execute_script)
358
+
359
+ @session.disable_logging
360
+
361
+ expect(@driver).to have_received(:execute_script).with('sauce: disable log')
362
+ end
363
+ end
364
+
365
+ describe '#stop_network' do
366
+ it 'raises exception if session not started' do
367
+ ClimateControl.modify(**BUILD_ENV) do
368
+ @session = Session.new
369
+ end
370
+
371
+ expect { @session.stop_network }.to raise_error(SessionNotStartedError)
372
+ end
373
+
374
+ it 'raises exception if session not on a Mac' do
375
+ expect_request
376
+ ClimateControl.modify(**BUILD_ENV) do
377
+ @session = Session.new
378
+ end
379
+
380
+ ClimateControl.modify(**SAUCE_ACCESS) do
381
+ @driver = @session.start
382
+ end
383
+ allow(@driver).to receive :quit
384
+ allow(@driver).to receive(:execute_script)
385
+
386
+ error = /Can only start or stop the network on a Mac/
387
+ expect { @session.stop_network }.to raise_error(InvalidPlatformError, error)
388
+ end
389
+
390
+ it 'stops network' do
391
+ caps = default_capabilities.merge(platformName: 'mac')
392
+ expect_request(caps)
393
+ ClimateControl.modify(**BUILD_ENV) do
394
+ @session = Session.new(Options.chrome(platform_name: 'mac'))
395
+ end
396
+
397
+ ClimateControl.modify(**SAUCE_ACCESS) do
398
+ @driver = @session.start
399
+ end
400
+ allow(@driver).to receive :quit
401
+ allow(@driver).to receive(:execute_script)
402
+
403
+ @session.stop_network
404
+
405
+ expect(@driver).to have_received(:execute_script).with('sauce: stop network')
406
+ end
407
+ end
408
+
409
+ describe '#start_network' do
410
+ it 'raises exception if session not started' do
411
+ ClimateControl.modify(**BUILD_ENV) do
412
+ @session = Session.new
413
+ end
414
+
415
+ expect { @session.start_network }.to raise_error(SessionNotStartedError)
416
+ end
417
+
418
+ it 'raises exception if session not on a Mac' do
419
+ expect_request
420
+ ClimateControl.modify(**BUILD_ENV) do
421
+ @session = Session.new
422
+ end
423
+
424
+ ClimateControl.modify(**SAUCE_ACCESS) do
425
+ @driver = @session.start
426
+ end
427
+ allow(@driver).to receive :quit
428
+ allow(@driver).to receive(:execute_script)
429
+
430
+ error = /Can only start or stop the network on a Mac/
431
+ expect { @session.start_network }.to raise_error(InvalidPlatformError, error)
432
+ end
433
+
434
+ it 'starts network' do
435
+ caps = default_capabilities.merge(platformName: 'mac')
436
+ expect_request(caps)
437
+ ClimateControl.modify(**BUILD_ENV) do
438
+ @session = Session.new(Options.chrome(platform_name: 'mac'))
439
+ end
440
+
441
+ ClimateControl.modify(**SAUCE_ACCESS) do
442
+ @driver = @session.start
443
+ end
444
+
445
+ allow(@driver).to receive :quit
446
+ allow(@driver).to receive(:execute_script)
447
+
448
+ @session.start_network
449
+
450
+ expect(@driver).to have_received(:execute_script).with('sauce: start network')
451
+ end
452
+ end
453
+
454
+ describe '#change_name' do
455
+ it 'raises exception if session not started' do
456
+ ClimateControl.modify(**BUILD_ENV) do
457
+ @session = Session.new
458
+ end
459
+
460
+ expect { @session.change_name('New Name') }.to raise_error(SessionNotStartedError)
461
+ end
462
+
463
+ it 'changes the test name' do
464
+ expect_request
465
+ ClimateControl.modify(**BUILD_ENV) do
466
+ @session = Session.new
467
+ end
468
+
469
+ ClimateControl.modify(**SAUCE_ACCESS) do
470
+ @driver = @session.start
471
+ end
472
+
473
+ allow(@driver).to receive :quit
474
+ allow(@driver).to receive(:execute_script)
475
+
476
+ @session.change_name('New Name')
477
+
478
+ expect(@driver).to have_received(:execute_script).with('sauce:job-name=New Name')
479
+ end
480
+ end
481
+
482
+ describe '#tags=' do
483
+ it 'raises exception if session not started' do
484
+ ClimateControl.modify(**BUILD_ENV) do
485
+ @session = Session.new
486
+ end
487
+
488
+ expect { @session.add_tags([]) }.to raise_error(SessionNotStartedError)
489
+ end
490
+
491
+ it 'accepts single tag' do
492
+ expect_request
493
+ ClimateControl.modify(**BUILD_ENV) do
494
+ @session = Session.new
495
+ end
496
+
497
+ ClimateControl.modify(**SAUCE_ACCESS) do
498
+ @driver = @session.start
499
+ end
500
+
501
+ allow(@driver).to receive :quit
502
+ allow(@driver).to receive(:execute_script)
503
+
504
+ @session.add_tags 'foo'
505
+
506
+ expect(@driver).to have_received(:execute_script).with('sauce:job-tags=foo')
507
+ end
508
+
509
+ it 'accepts multiple tags as String' do
510
+ expect_request
511
+ ClimateControl.modify(**BUILD_ENV) do
512
+ @session = Session.new
513
+ end
514
+
515
+ ClimateControl.modify(**SAUCE_ACCESS) do
516
+ @driver = @session.start
517
+ end
518
+
519
+ allow(@driver).to receive :quit
520
+ allow(@driver).to receive(:execute_script)
521
+
522
+ @session.add_tags 'foo,bar'
523
+
524
+ expect(@driver).to have_received(:execute_script).with('sauce:job-tags=foo,bar')
525
+ end
526
+
527
+ it 'accepts multiple tags as Array' do
528
+ expect_request
529
+ ClimateControl.modify(**BUILD_ENV) do
530
+ @session = Session.new
531
+ end
532
+
533
+ ClimateControl.modify(**SAUCE_ACCESS) do
534
+ @driver = @session.start
535
+ end
536
+
537
+ allow(@driver).to receive :quit
538
+ allow(@driver).to receive(:execute_script)
539
+
540
+ @session.add_tags %w[foo bar]
541
+
542
+ expect(@driver).to have_received(:execute_script).with('sauce:job-tags=foo,bar')
190
543
  end
191
544
  end
192
545
  end