Class: MediawikiSelenium::BrowserFactory::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mediawiki_selenium/browser_factory/base.rb

Overview

Browser factories instantiate browsers of a certain type, configure them according to bound environmental variables, and cache them according to the uniqueness of that configuration.

Direct Known Subclasses

Chrome, Firefox, Phantomjs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser_name) ⇒ Base

Initializes new factory instances.

Parameters:

  • browser_name (Symbol)


70
71
72
73
74
75
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 70

def initialize(browser_name)
  @browser_name = browser_name
  @bindings = {}
  @browser_cache = {}
  @overrides = {}
end

Instance Attribute Details

#browser_nameObject (readonly)

Returns the value of attribute browser_name.



62
63
64
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 62

def browser_name
  @browser_name
end

Class Method Details

.bind {|values, browser_options| ... } ⇒ Object

Deprecated.

Use configure instead.

Binds environmental configuration to any browser created by factories of this type. Use of this method should generally be reserved for macro-style invocation in derived classes.

Examples:

Always configure Firefox's language according to :browser_language

module MediawikiSelenium::BrowserFactory
  class Firefox < Base
    configure(:browser_language) do |lang, options|
      options[:desired_capabilities][:firefox_profile]["intl.accept_languages"] = lang
    end
  end
end

Parameters:

  • names (Symbol)

    One or more option names.

Yields:

  • (values, browser_options)

    A block that binds the configuration to the browser options.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 38

def configure(*names, &blk)
  raise ArgumentError, 'no block given' unless block_given?

  key = names.length == 1 ? names.first : names
  default_bindings[key] ||= []
  default_bindings[key] << blk
end

.bindingsHash

All bindings for this factory class combined with those of super classes.

Returns:

  • (Hash)


45
46
47
48
49
50
51
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 45

def bindings
  if superclass <= Base
    default_bindings.merge(superclass.bindings) { |_key, old, new| old + new }
  else
    default_bindings
  end
end

.configure(*names) {|values, browser_options| ... } ⇒ Object

Binds environmental configuration to any browser created by factories of this type. Use of this method should generally be reserved for macro-style invocation in derived classes.

Examples:

Always configure Firefox's language according to :browser_language

module MediawikiSelenium::BrowserFactory
  class Firefox < Base
    configure(:browser_language) do |lang, options|
      options[:desired_capabilities][:firefox_profile]["intl.accept_languages"] = lang
    end
  end
end

Parameters:

  • names (Symbol)

    One or more option names.

Yields:

  • (values, browser_options)

    A block that binds the configuration to the browser options.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 29

def configure(*names, &blk)
  raise ArgumentError, 'no block given' unless block_given?

  key = names.length == 1 ? names.first : names
  default_bindings[key] ||= []
  default_bindings[key] << blk
end

.default_bindingsHash

Bindings for this factory class.

Returns:

  • (Hash)


57
58
59
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 57

def default_bindings
  @default_bindings ||= {}
end

Instance Method Details

#all_binding_keysArray

Returns a unique set of all the binding keys.

Returns:

  • (Array)


81
82
83
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 81

def all_binding_keys
  bindings.keys.flatten.uniq
end

#bindingsHash

Effective bindings for this factory, those defined at the class level and those defined for this instance.

Returns:

  • (Hash)


125
126
127
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 125

def bindings
  self.class.bindings.merge(@bindings) { |_key, old, new| old + new }
end

#browser_for(config) ⇒ Watir::Browser

Instantiate a browser using the given environmental configuration. Browsers are cached and reused as long as the configuration is the same.

Parameters:

  • config (Hash)

    Browser configuration.

Returns:

  • (Watir::Browser)

See Also:



139
140
141
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 139

def browser_for(config)
  @browser_cache[config] ||= new_browser_for(config)
end

#browser_options(config) ⇒ Hash

Browser options for the given configuration.

Parameters:

  • config (Hash)

Returns:

  • (Hash)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 149

def browser_options(config)
  config = config.merge(@overrides)

  options = default_browser_options.tap do |default_options|
    bindings.each do |(names, bindings_for_option)|
      bindings_for_option.each do |binding|
        values = config.values_at(*Array(names))

        unless values.any? { |value| value.nil? || value.to_s.empty? }
          binding.call(*values, default_options)
        end
      end
    end
  end

  finalize_options!(options)

  options
end

#configure(*names) {|values, browser_options| ... } ⇒ Object Also known as: bind

Binds environmental configuration to any browser created by this factory instance.

Examples:

Override the user agent according :browser_user_agent

factory = BrowserFactory.new(:firefox)
factory.configure(:browser_user_agent) do |agent, options|
  options[:desired_capabilities][:firefox_profile]["general.useragent.override"] = agent
end

Annotate the session with our build information

factory.configure(:job_name, :build_number) do |job, build, options|
  options[:desired_capabilities][:name] = "#{job} (#{build})"
end

Bindings aren't invoked unless all given options are configured

factory.configure(:foo, :bar) do |foo, bar, options|
  # this never happens!
  options[:desired_capabilities][:name] = "#{foo} #{bar}"
end
factory.browser_for(Environment.new(foo: "x"))

Parameters:

  • names (Symbol)

    One or more option names.

Yields:

  • (values, browser_options)

    A block that binds the configuration to the browser options.



111
112
113
114
115
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 111

def configure(*names, &blk)
  key = names.length == 1 ? names.first : names
  @bindings[key] ||= []
  @bindings[key] << (blk || proc {})
end

#each {|browser| ... } ⇒ Object

Iterate over each browser created by this factory.

Yields:

  • (browser)


173
174
175
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 173

def each(&blk)
  @browser_cache.values.each(&blk)
end

#new_browser_for(config) ⇒ Watir::Browser

A new browser for the given environmental configuration.

Parameters:

  • config (Hash)

    Browser configuration.

Returns:

  • (Watir::Browser)

See Also:



185
186
187
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 185

def new_browser_for(config)
  new_browser(browser_options(config))
end

#override(config) ⇒ Object

Always use the given configuration when setting up a new browser, regardless of what has been previously configured.

Parameters:

  • config (Hash)

    Configuration overrides.



194
195
196
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 194

def override(config)
  @overrides.merge!(config)
end

#teardown(_env, _status) ⇒ Hash{String => String}

Executes additional teardown tasks.

Parameters:

  • _env (Environment)

    Environment.

  • _status (Symbol)

    Status of the executed scenario.

Returns:

  • (Hash{String => String})

    Artifacts.

See Also:



207
208
209
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 207

def teardown(_env, _status)
  {}
end