jenkins-bot has submitted this change and it was merged.
Change subject: Firefox environment bindings and specs
......................................................................
Firefox environment bindings and specs
Change-Id: I1418fcd52421aee39a015dbb16a119ed906188fe
---
M lib/mediawiki_selenium/browser_factory.rb
M lib/mediawiki_selenium/browser_factory/base.rb
M lib/mediawiki_selenium/browser_factory/firefox.rb
M lib/mediawiki_selenium/environment.rb
A spec/browser_factory/firefox_spec.rb
5 files changed, 127 insertions(+), 41 deletions(-)
Approvals:
Dduvall: Looks good to me, approved
jenkins-bot: Verified
diff --git a/lib/mediawiki_selenium/browser_factory.rb
b/lib/mediawiki_selenium/browser_factory.rb
index 10660a9..f753cd5 100644
--- a/lib/mediawiki_selenium/browser_factory.rb
+++ b/lib/mediawiki_selenium/browser_factory.rb
@@ -7,7 +7,11 @@
autoload :Chrome, "mediawiki_selenium/browser_factory/chrome"
autoload :Phantomjs, "mediawiki_selenium/browser_factory/phantomjs"
- # Resolves a new factory for the given browser name.
+ # Resolves and instantiates a new factory for the given browser name.
+ #
+ # @example Create a new firefox browser factory
+ # factory = BrowserFactory.new(:firefox)
+ # # => #<MediawikiSelenium::BrowserFactory::Firefox>
#
# @param name [Symbol] Browser name.
#
diff --git a/lib/mediawiki_selenium/browser_factory/base.rb
b/lib/mediawiki_selenium/browser_factory/base.rb
index 9ba0319..b562c30 100644
--- a/lib/mediawiki_selenium/browser_factory/base.rb
+++ b/lib/mediawiki_selenium/browser_factory/base.rb
@@ -48,18 +48,6 @@
@browser_cache[config] ||= Watir::Browser.new(type,
browser_options(config))
end
- protected
-
- def capabilities
- Selenium::WebDriver::Remote::Capabilities.send(type)
- end
-
- def http_client
- Selenium::WebDriver::Remote::Http::Default.new
- end
-
- private
-
def browser_options(config)
{ http_client: http_client, desired_capabilities: capabilities }.tap
do |watir_options|
bindings.each do |(name, bindings_for_option)|
@@ -70,6 +58,16 @@
end
end
end
+
+ protected
+
+ def capabilities
+ Selenium::WebDriver::Remote::Capabilities.send(type)
+ end
+
+ def http_client
+ Selenium::WebDriver::Remote::Http::Default.new
+ end
end
end
end
diff --git a/lib/mediawiki_selenium/browser_factory/firefox.rb
b/lib/mediawiki_selenium/browser_factory/firefox.rb
index 56bc029..7cd61c3 100644
--- a/lib/mediawiki_selenium/browser_factory/firefox.rb
+++ b/lib/mediawiki_selenium/browser_factory/firefox.rb
@@ -7,26 +7,19 @@
opts[:desired_capabilities][:firefox_profile]["dom.max_chrome_script_run_time"]
= timeout
end
+ bind(:browser_language) do |language, opts|
+ opts[:desired_capabilities][:firefox_profile]["intl.accept_languages"]
= language
+ end
+
+ bind(:browser_user_agent) do |user_agent, opts|
+
opts[:desired_capabilities][:firefox_profile]["general.useragent.override"] =
user_agent
+ end
+
+ protected
+
def capabilities
super.tap do |capabilities|
capabilities[:firefox_profile] =
Selenium::WebDriver::Firefox::Profile.new
- end
- end
-
- def profile
- Selenium::WebDriver::Firefox::Profile.new.tap do |profile|
- bind(:browser_timeout) do |timeout|
- profile["dom.max_script_run_time"] = timeout
- profile["dom.max_chrome_script_run_time"] = timeout
- end
-
- bind(:browser_language) do |language|
- profile["intl.accept_languages"] = language
- end
-
- bind(:browser_user_agent) do |user_agent|
- profile["general.useragent.override"] = user_agent
- end
end
end
end
diff --git a/lib/mediawiki_selenium/environment.rb
b/lib/mediawiki_selenium/environment.rb
index 58a5da6..339cb73 100644
--- a/lib/mediawiki_selenium/environment.rb
+++ b/lib/mediawiki_selenium/environment.rb
@@ -41,8 +41,12 @@
@config = other.config.clone
end
- # Two environments are considered equal if they have identical
- # configuration.
+ # Whether the given environment is equal to this one. Two environments are
+ # considered equal if they have identical configuration.
+ #
+ # @param other [Environment]
+ #
+ # @return [true, false]
#
def ==(other)
@config == other.config
@@ -64,10 +68,17 @@
# Binds new possible configuration for the given browser.
#
+ # @example Allow setting of Firefox's language by way of :browser_language
+ # env = Environment.new(browser_language: "eo")
+ # env.bind(:firefox, :browser_language) do |language, options|
+ #
options[:desired_capabilities][:firefox_profile]["intl.accept_languages"] =
language
+ # end
+ #
# @param browser_name [Symbol] Browser name.
# @param option_name [Symbol] Option name.
#
- # @yield []
+ # @yield [value, browser_options] A block that binds the configuration to
+ # the browser options.
#
def bind(browser_name, option_name, &blk)
browser_factory(browser_name).bind(option_name, &blk)
@@ -81,6 +92,12 @@
browser_factory.browser_for(self)
end
+ # Factory used to instantiate and open new browsers.
+ #
+ # @param type [Symbol] Browser name.
+ #
+ # @return [BrowserFactory]
+ #
def browser_factory(type = browser_name)
type = type.to_s.downcase.to_sym
@@ -97,6 +114,13 @@
lookup(:browser).downcase.to_sym
end
+ # Returns the configured value for the given env variable name.
+ #
+ # @param key [Symbol] Environment variable name.
+ # @param id [Symbol] Alternative variable suffix.
+ #
+ # @return [String]
+ #
def lookup(key, id = nil)
key = "#{key}_#{id}" unless id.nil?
key = key.to_sym
@@ -117,6 +141,13 @@
end
end
+ # Returns the configured values for the given env variable names.
+ #
+ # @param key [Array<Symbol>] Environment variable names.
+ # @param id [Symbol] Alternative variable suffix.
+ #
+ # @return [Array<String>]
+ #
def lookup_all(keys, id = nil)
keys.each.with_object({}) do |key, hash|
value = lookup(key, id)
@@ -143,16 +174,16 @@
# options that correspond to the given ones but have the given ID
# appended.
#
- # @example Overwrite :option with the value from :option_b
- # # given an environment with { option: "x", option_b: "y", ... }
- # env.with_alternative(:option, :b) do |env|
- # env # => { option: "y", ... }
+ # @example Overwrite :foo with the :b alternative
+ # # given an environment with { foo: "x", foo_b: "y", ... }
+ # env.with_alternative(:foo, :b) do |env|
+ # env # => { foo: "y", ... }
# end
#
- # @example Overwrite both :option and :other with :option_b and :other_b
- # # given { option: "x", option_b: "y", other: "w", other_b: "z" }
- # env.with_alternative([:option, :other], :b) do |env|
- # env # => { option: "y", other: "z", ... }
+ # @example Overwrite both :foo and :bar with the :b alternatives
+ # # given { foo: "x", foo_b: "y", bar: "w", bar_b: "z" }
+ # env.with_alternative([:foo, :bar], :b) do |env|
+ # env # => { foo: "y", bar: "z", ... }
# end
#
# @param names [Array|Symbol] Configuration option or options.
diff --git a/spec/browser_factory/firefox_spec.rb
b/spec/browser_factory/firefox_spec.rb
new file mode 100644
index 0000000..43ca07d
--- /dev/null
+++ b/spec/browser_factory/firefox_spec.rb
@@ -0,0 +1,60 @@
+require "spec_helper"
+
+module MediawikiSelenium::BrowserFactory
+ describe Firefox do
+ let(:factory_class) { Firefox }
+ let(:factory) { factory_class.new(:firefox) }
+
+ describe ".default_bindings" do
+ subject { factory_class.default_bindings }
+
+ it { is_expected.to include(:browser_language) }
+ it { is_expected.to include(:browser_timeout) }
+ it { is_expected.to include(:browser_user_agent) }
+ end
+
+ describe "#browser_options" do
+ subject { factory.browser_options(config) }
+
+ let(:profile) { double(Selenium::WebDriver::Firefox::Profile) }
+
+ before do
+ expect(Selenium::WebDriver::Firefox::Profile).to
receive(:new).and_return(profile)
+ end
+
+ context "given a custom browser_timeout" do
+ let(:config) { { browser_timeout: "30" } }
+
+ it "sets dom.max_script_run_time to the given number" do
+ expect(profile).to receive(:[]=).with("dom.max_script_run_time", 30)
+ allow(profile).to receive(:[]=)
+ subject
+ end
+
+ it "sets dom.max_chrome_script_run_time to the given number" do
+ expect(profile).to
receive(:[]=).with("dom.max_chrome_script_run_time", 30)
+ allow(profile).to receive(:[]=)
+ subject
+ end
+ end
+
+ context "given a custom browser_language" do
+ let(:config) { { browser_language: "eo" } }
+
+ it "sets intl.accept_languages to the given language" do
+ expect(profile).to receive(:[]=).with("intl.accept_languages", "eo")
+ subject
+ end
+ end
+
+ context "given a custom browser_user_agent" do
+ let(:config) { { browser_user_agent: "FooBot" } }
+
+ it "sets general.useragent.override to the given string" do
+ expect(profile).to receive(:[]=).with("general.useragent.override",
"FooBot")
+ subject
+ end
+ end
+ end
+ end
+end
--
To view, visit https://gerrit.wikimedia.org/r/169851
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I1418fcd52421aee39a015dbb16a119ed906188fe
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/selenium
Gerrit-Branch: env-abstraction-layer
Gerrit-Owner: Dduvall <[email protected]>
Gerrit-Reviewer: Dduvall <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits