mediawiki_selenium 0.4.3 → 1.0.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitreview +1 -1
  3. data/.rspec +1 -0
  4. data/.yardopts +1 -0
  5. data/Gemfile +1 -1
  6. data/README.md +108 -55
  7. data/bin/mediawiki-selenium-init +5 -0
  8. data/lib/mediawiki_selenium.rb +10 -19
  9. data/lib/mediawiki_selenium/browser_factory.rb +24 -0
  10. data/lib/mediawiki_selenium/browser_factory/base.rb +212 -0
  11. data/lib/mediawiki_selenium/browser_factory/chrome.rb +27 -0
  12. data/lib/mediawiki_selenium/browser_factory/firefox.rb +34 -0
  13. data/lib/mediawiki_selenium/browser_factory/phantomjs.rb +21 -0
  14. data/lib/mediawiki_selenium/configuration_error.rb +4 -0
  15. data/lib/mediawiki_selenium/environment.rb +494 -0
  16. data/lib/mediawiki_selenium/initializer.rb +19 -0
  17. data/lib/mediawiki_selenium/page_factory.rb +38 -0
  18. data/lib/mediawiki_selenium/remote_browser_factory.rb +87 -0
  19. data/lib/mediawiki_selenium/step_definitions.rb +5 -0
  20. data/lib/mediawiki_selenium/support.rb +3 -0
  21. data/lib/mediawiki_selenium/support/env.rb +3 -127
  22. data/lib/mediawiki_selenium/support/hooks.rb +23 -34
  23. data/lib/mediawiki_selenium/support/modules/api_helper.rb +44 -5
  24. data/lib/mediawiki_selenium/support/pages.rb +4 -0
  25. data/lib/mediawiki_selenium/support/pages/api_page.rb +1 -0
  26. data/lib/mediawiki_selenium/support/pages/login_page.rb +3 -12
  27. data/lib/mediawiki_selenium/support/pages/random_page.rb +2 -12
  28. data/lib/mediawiki_selenium/support/pages/reset_preferences_page.rb +3 -12
  29. data/lib/mediawiki_selenium/version.rb +1 -1
  30. data/mediawiki_selenium.gemspec +9 -3
  31. data/spec/api_helper_spec.rb +84 -0
  32. data/spec/browser_factory/base_spec.rb +211 -0
  33. data/spec/browser_factory/chrome_spec.rb +36 -0
  34. data/spec/browser_factory/firefox_spec.rb +60 -0
  35. data/spec/browser_factory/phantomjs_spec.rb +38 -0
  36. data/spec/environment_spec.rb +474 -0
  37. data/spec/page_factory_spec.rb +61 -0
  38. data/spec/remote_browser_factory_spec.rb +50 -0
  39. data/spec/spec_helper.rb +4 -0
  40. data/templates/tests/browser/environments.yml +35 -0
  41. data/templates/tests/browser/features/support/env.rb +6 -0
  42. metadata +122 -20
  43. data/lib/mediawiki_selenium/support/modules/sauce_helper.rb +0 -13
  44. data/lib/mediawiki_selenium/support/modules/url_module.rb +0 -21
  45. data/spec/README +0 -2
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ module MediawikiSelenium::BrowserFactory
4
+ describe Phantomjs do
5
+ let(:factory_class) { Phantomjs }
6
+ let(:factory) { factory_class.new(:phantomjs) }
7
+
8
+ describe ".default_bindings" do
9
+ subject { factory_class.default_bindings }
10
+
11
+ it { is_expected.to include(:browser_language) }
12
+ it { is_expected.not_to include(:browser_timeout) }
13
+ it { is_expected.to include(:browser_user_agent) }
14
+ end
15
+
16
+ describe "#browser_options" do
17
+ subject { factory.browser_options(config) }
18
+
19
+ context "given a custom browser_language" do
20
+ let(:config) { { browser_language: "eo" } }
21
+
22
+ it "sets phantomjs.page.customHeaders.Accept-Language to the given language" do
23
+ capabilities = subject[:desired_capabilities]
24
+ expect(capabilities["phantomjs.page.customHeaders.Accept-Language"]).to eq("eo")
25
+ end
26
+ end
27
+
28
+ context "given a custom browser_user_agent" do
29
+ let(:config) { { browser_user_agent: "FooBot" } }
30
+
31
+ it "sets phantomjs.page.settings.userAgent to the given string" do
32
+ capabilities = subject[:desired_capabilities]
33
+ expect(capabilities["phantomjs.page.settings.userAgent"]).to eq("FooBot")
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,474 @@
1
+ require "spec_helper"
2
+
3
+ module MediawikiSelenium
4
+ describe Environment do
5
+ subject { env }
6
+
7
+ let(:env) { Environment.new(config) }
8
+ let(:config) { minimum_config }
9
+
10
+ let(:minimum_config) do
11
+ {
12
+ browser: browser,
13
+ mediawiki_api_url: mediawiki_api_url,
14
+ mediawiki_url: mediawiki_url,
15
+ mediawiki_user: mediawiki_user,
16
+ mediawiki_password: mediawiki_password,
17
+ }
18
+ end
19
+
20
+ let(:browser) { "firefox" }
21
+ let(:mediawiki_api_url) { "http://an.example/wiki/api.php" }
22
+ let(:mediawiki_url) { "http://an.example/wiki/" }
23
+ let(:mediawiki_user) { "mw user" }
24
+ let(:mediawiki_password) { "mw password" }
25
+
26
+ describe ".load" do
27
+ subject { Environment.load(name, extra) }
28
+
29
+ let(:name) { "foo" }
30
+ let(:extra) { {} }
31
+
32
+ before do
33
+ expect(YAML).to receive(:load_file).with("environments.yml").
34
+ and_return("foo" => { "x" => "a", "y" => "b" })
35
+ end
36
+
37
+ it "returns a new environment" do
38
+ expect(subject).to be_a(Environment)
39
+ end
40
+
41
+ it "uses the given configuration in `environments.yml`" do
42
+ expect(subject[:x]).to eq("a")
43
+ expect(subject[:y]).to eq("b")
44
+ end
45
+
46
+ context "when the given environment does not exist in `environments.yml`" do
47
+ let(:name) { "bar" }
48
+
49
+ it "raises a ConfigurationError" do
50
+ expect { subject }.to raise_error(ConfigurationError, "unknown environment `bar`")
51
+ end
52
+ end
53
+
54
+ context "when extra configuration is given" do
55
+ let(:extra) { { x: "c" } }
56
+
57
+ it "overwrites the loaded configuration" do
58
+ expect(subject[:x]).to eq("c")
59
+ expect(subject[:y]).to eq("b")
60
+ end
61
+ end
62
+ end
63
+
64
+ describe ".load_default" do
65
+ subject { Environment.load_default }
66
+
67
+ it "loads the environment configuration specified by MEDIAWIKI_ENVIRONMENT" do
68
+ expect(ENV).to receive(:[]).with("MEDIAWIKI_ENVIRONMENT").and_return("foo")
69
+ expect(Environment).to receive(:load).with("foo", ENV)
70
+ subject
71
+ end
72
+ end
73
+
74
+ describe "#==" do
75
+ subject { env == other }
76
+
77
+ context "given an environment with the same configuration" do
78
+ let(:other) { Environment.new(config) }
79
+
80
+ it "considers them equal" do
81
+ expect(subject).to be(true)
82
+ end
83
+ end
84
+
85
+ context "given an environment with different configuration" do
86
+ let(:other) { Environment.new(config.merge(some: "extra")) }
87
+
88
+ it "considers them not equal" do
89
+ expect(subject).to be(false)
90
+ end
91
+ end
92
+ end
93
+
94
+ describe "#as_user" do
95
+ context "when both an alternative user and password are defined" do
96
+ let(:config) do
97
+ {
98
+ mediawiki_user: "user",
99
+ mediawiki_password: "pass",
100
+ mediawiki_user_b: "user b",
101
+ mediawiki_password_b: "pass b",
102
+ }
103
+ end
104
+
105
+ it "yields the alternative user and password in the new environment" do
106
+ expect { |block| env.as_user(:b, &block) }.to yield_with_args("user b", "pass b")
107
+
108
+ env.as_user(:b) do
109
+ expect(env[:mediawiki_user]).to eq("user b")
110
+ expect(env[:mediawiki_password]).to eq("pass b")
111
+ end
112
+ end
113
+ end
114
+
115
+ context "when an alternative for the password isn't defined" do
116
+ let(:config) do
117
+ {
118
+ mediawiki_user: "user",
119
+ mediawiki_password: "pass",
120
+ mediawiki_user_b: "user b",
121
+ }
122
+ end
123
+
124
+ it "falls back to using the base defined password" do
125
+ expect { |block| env.as_user(:b, &block) }.to yield_with_args("user b", "pass")
126
+
127
+ env.as_user(:b) do
128
+ expect(env[:mediawiki_user]).to eq("user b")
129
+ expect(env[:mediawiki_password]).to eq("pass")
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ describe "#browser_factory" do
136
+ subject { env.browser_factory }
137
+
138
+ it "is a factory for the configured browser" do
139
+ expect(subject).to be_a(BrowserFactory::Firefox)
140
+ end
141
+
142
+ context "given an explicit type of browser" do
143
+ subject { env.browser_factory(:chrome) }
144
+
145
+ it "is a factory for that type" do
146
+ expect(subject).to be_a(BrowserFactory::Chrome)
147
+ end
148
+ end
149
+
150
+ context "caching in a cloned environment" do
151
+ let(:env1) { env }
152
+ let(:env2) { env1.clone }
153
+
154
+ let(:factory1) { env.browser_factory(browser1) }
155
+ let(:factory2) { env2.browser_factory(browser2) }
156
+
157
+ context "with the same local/remote behavior as before" do
158
+ before do
159
+ expect(env1).to receive(:remote?).at_least(:once).and_return(false)
160
+ expect(env2).to receive(:remote?).at_least(:once).and_return(false)
161
+ end
162
+
163
+ context "and the same type of browser as before" do
164
+ let(:browser1) { :firefox }
165
+ let(:browser2) { :firefox }
166
+
167
+ it "returns a cached factory" do
168
+ expect(factory1).to be(factory2)
169
+ end
170
+ end
171
+
172
+ context "and a different type of browser than before" do
173
+ let(:browser1) { :firefox }
174
+ let(:browser2) { :chrome }
175
+
176
+ it "returns a new factory" do
177
+ expect(factory1).not_to be(factory2)
178
+ end
179
+ end
180
+ end
181
+
182
+ context "with different local/remote behavior as before" do
183
+ before do
184
+ expect(env1).to receive(:remote?).at_least(:once).and_return(false)
185
+ expect(env2).to receive(:remote?).at_least(:once).and_return(true)
186
+ end
187
+
188
+ context "but the same type of browser as before" do
189
+ let(:browser1) { :firefox }
190
+ let(:browser2) { :firefox }
191
+
192
+ it "returns a cached factory" do
193
+ expect(factory1).not_to be(factory2)
194
+ end
195
+ end
196
+
197
+ context "and a different type of browser than before" do
198
+ let(:browser1) { :firefox }
199
+ let(:browser2) { :chrome }
200
+
201
+ it "returns a new factory" do
202
+ expect(factory1).not_to be(factory2)
203
+ end
204
+ end
205
+ end
206
+ end
207
+ end
208
+
209
+ describe "#browser_name" do
210
+ subject { env.browser_name }
211
+
212
+ let(:browser) { "Chrome" }
213
+
214
+ it "is always a lowercase symbol" do
215
+ expect(subject).to be(:chrome)
216
+ end
217
+
218
+ context "missing browser configuration" do
219
+ let(:browser) { nil }
220
+
221
+ it "defaults to :firefox" do
222
+ expect(subject).to be(:firefox)
223
+ end
224
+ end
225
+ end
226
+
227
+ describe "#env" do
228
+ subject { env.env }
229
+
230
+ it { is_expected.to be(env) }
231
+ end
232
+
233
+ describe "#in_browser" do
234
+ it "executes in the new environment with a new browser session" do
235
+ expect { |block| env.in_browser(:a, &block) }.to yield_with_args(:a)
236
+
237
+ env.in_browser(:a) do
238
+ expect(env[:_browser_session]).to eq(:a)
239
+ end
240
+ end
241
+
242
+ context "given browser configuration overrides" do
243
+ it "executes in the new environment with the prefixed overrides" do
244
+ expect { |block| env.in_browser(:a, language: "eo", &block) }.
245
+ to yield_with_args("eo", :a)
246
+
247
+ env.in_browser(:a, language: "eo") do
248
+ expect(env[:browser_language]).to eq("eo")
249
+ expect(env[:_browser_session]).to eq(:a)
250
+ end
251
+ end
252
+ end
253
+ end
254
+
255
+ describe "#lookup" do
256
+ subject { env.lookup(key, options) }
257
+
258
+ let(:config) { { foo: "foo_value", foo_b: "foo_b_value", bar: "bar_value" } }
259
+
260
+ context "for a key that exists" do
261
+ let(:key) { :foo }
262
+ let(:options) { {} }
263
+
264
+ it "returns the configuration" do
265
+ expect(subject).to eq("foo_value")
266
+ end
267
+ end
268
+
269
+ context "for a key that doesn't exist" do
270
+ let(:key) { :baz }
271
+
272
+ context "given no default value" do
273
+ let(:options) { {} }
274
+
275
+ it "raises a ConfigurationError" do
276
+ expect { subject }.to raise_error(ConfigurationError)
277
+ end
278
+ end
279
+
280
+ context "given a default value" do
281
+ let(:options) { { default: default } }
282
+ let(:default) { double(Object) }
283
+
284
+ it { is_expected.to be(default) }
285
+ end
286
+ end
287
+
288
+ context "for an alternative that exists" do
289
+ let(:key) { :foo }
290
+ let(:options) { { id: :b } }
291
+
292
+ it "returns the configured alternative" do
293
+ expect(subject).to eq("foo_b_value")
294
+ end
295
+ end
296
+
297
+ context "for an alternative that doesn't exist" do
298
+ let(:key) { :foo }
299
+
300
+ context "given no default value" do
301
+ let(:options) { { id: :c } }
302
+
303
+ it "raises a ConfigurationError" do
304
+ expect { subject }.to raise_error(ConfigurationError)
305
+ end
306
+ end
307
+
308
+ context "given a default value" do
309
+ let(:options) { { id: :c, default: default } }
310
+ let(:default) { double(Object) }
311
+
312
+ it { is_expected.to be(default) }
313
+ end
314
+ end
315
+ end
316
+
317
+ describe "#on_wiki" do
318
+ let(:config) do
319
+ {
320
+ mediawiki_url: "http://an.example/wiki",
321
+ mediawiki_url_b: "http://altb.example/wiki",
322
+ mediawiki_url_c: "http://altc.example/wiki",
323
+ }
324
+ end
325
+
326
+ it "executes in the new environment using the alternative wiki URL" do
327
+ expect { |block| env.on_wiki(:b, &block) }.to yield_with_args("http://altb.example/wiki")
328
+
329
+ env.on_wiki(:b) do
330
+ expect(env[:mediawiki_url]).to eq("http://altb.example/wiki")
331
+ end
332
+ end
333
+ end
334
+
335
+ describe "#user" do
336
+ subject { env.user(id) }
337
+
338
+ let(:config) { { mediawiki_user: "mw_user", mediawiki_user_b: "mw_user_b" } }
339
+
340
+ context "given no alternative ID" do
341
+ let(:id) { nil }
342
+
343
+ it "returns the configured :mediawiki_user" do
344
+ expect(subject).to eq("mw_user")
345
+ end
346
+ end
347
+
348
+ context "given an alternative ID" do
349
+ let(:id) { :b }
350
+
351
+ it "returns the configured alternative :mediawiki_user" do
352
+ expect(subject).to eq("mw_user_b")
353
+ end
354
+ end
355
+ end
356
+
357
+ describe "#user_label" do
358
+ subject { env.user_label(id) }
359
+
360
+ let(:config) { { mediawiki_user: "mw_user", mediawiki_user_b: "mw_user_b" } }
361
+
362
+ context "given no alternative ID" do
363
+ let(:id) { nil }
364
+
365
+ it "returns the configured :mediawiki_user with underscores replaced" do
366
+ expect(subject).to eq("mw user")
367
+ end
368
+ end
369
+
370
+ context "given an alternative ID" do
371
+ let(:id) { :b }
372
+
373
+ it "returns the configured alternative :mediawiki_user with underscores replaced" do
374
+ expect(subject).to eq("mw user b")
375
+ end
376
+ end
377
+ end
378
+
379
+ describe "#wiki_url" do
380
+ subject { env.wiki_url(url) }
381
+
382
+ let(:env) { Environment.new(mediawiki_url: "http://an.example/wiki/") }
383
+
384
+ context "with no given url" do
385
+ let(:url) { nil }
386
+
387
+ it "is the configured :mediawiki_url" do
388
+ expect(subject).to eq("http://an.example/wiki/")
389
+ end
390
+ end
391
+
392
+ context "when the given URL is a relative path" do
393
+ let(:url) { "some/path" }
394
+
395
+ it "is the configured :mediawiki_url with the path appended" do
396
+ expect(subject).to eq("http://an.example/wiki/some/path")
397
+ end
398
+ end
399
+
400
+ context "when the given URL is an absolute path" do
401
+ let(:url) { "/some/path" }
402
+
403
+ it "is the configured :mediawiki_url with the path replaced" do
404
+ expect(subject).to eq("http://an.example/some/path")
405
+ end
406
+ end
407
+
408
+ context "when the given URL is an absolute URL" do
409
+ let(:url) { "http://another.example" }
410
+
411
+ it "is given absolute URL" do
412
+ expect(subject).to eq("http://another.example")
413
+ end
414
+ end
415
+
416
+ context "when the given URL is a relative path with a namespace" do
417
+ let(:url) { "some:path" }
418
+
419
+ it "is the configured :mediawiki_url with the path replaced" do
420
+ expect(subject).to eq("http://an.example/wiki/some:path")
421
+ end
422
+ end
423
+
424
+ end
425
+
426
+ describe "#with_alternative" do
427
+ let(:config) do
428
+ {
429
+ mediawiki_url: "http://a.example/wiki",
430
+ mediawiki_url_b: "http://b.example/wiki",
431
+ mediawiki_api_url: "http://a.example/api",
432
+ mediawiki_api_url_b: "http://b.example/api",
433
+ }
434
+ end
435
+
436
+ context "given one option name and an ID" do
437
+ it "executes in the new environment that substitutes it using the alternative" do
438
+ expect { |block| env.with_alternative(:mediawiki_url, :b, &block) }.
439
+ to yield_with_args("http://b.example/wiki")
440
+
441
+ env.with_alternative(:mediawiki_url, :b) do
442
+ expect(env[:mediawiki_url]).to eq("http://b.example/wiki")
443
+ end
444
+ end
445
+ end
446
+
447
+ context "given multiple option names and an ID" do
448
+ it "executes in the new environment that substitutes both using the alternatives" do
449
+ expect { |block| env.with_alternative([:mediawiki_url, :mediawiki_api_url], :b, &block) }.
450
+ to yield_with_args("http://b.example/wiki", "http://b.example/api")
451
+
452
+ env.with_alternative([:mediawiki_url, :mediawiki_api_url], :b) do
453
+ expect(env[:mediawiki_url]).to eq("http://b.example/wiki")
454
+ expect(env[:mediawiki_api_url]).to eq("http://b.example/api")
455
+ end
456
+ end
457
+ end
458
+
459
+ context "following block evaluation" do
460
+ it "restores the original configuration" do
461
+ env.with_alternative(:mediawiki_url, :b)
462
+ expect(env[:mediawiki_url]).to eq("http://a.example/wiki")
463
+ end
464
+
465
+ context "when an exception is raised within the block" do
466
+ it "restores the original configuration and lets the exception be raised" do
467
+ expect { env.with_alternative(:mediawiki_url, :b) { raise "error" } }.to raise_error
468
+ expect(env[:mediawiki_url]).to eq("http://a.example/wiki")
469
+ end
470
+ end
471
+ end
472
+ end
473
+ end
474
+ end