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)


67
68
69
70
71
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 67

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

Instance Attribute Details

#browser_nameObject (readonly)

Returns the value of attribute browser_name.



59
60
61
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 59

def browser_name
  @browser_name
end

Class Method Details

.bind(*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
    bind(: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 bind(*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)


42
43
44
45
46
47
48
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 42

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

.default_bindingsHash

Bindings for this factory class.

Returns:

  • (Hash)


54
55
56
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 54

def default_bindings
  @default_bindings ||= {}
end

Instance Method Details

#all_binding_keysArray

Returns a unique set of all the binding keys.

Returns:

  • (Array)


77
78
79
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 77

def all_binding_keys
  bindings.keys.flatten.uniq
end

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

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.bind(:browser_user_agent) do |agent, options|
  options[:desired_capabilities][:firefox_profile]["general.useragent.override"] = agent
end

Annotate the session with our build information

factory.bind(: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.bind(: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.



107
108
109
110
111
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 107

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

#bindingsHash

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

Returns:

  • (Hash)


118
119
120
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 118

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:



132
133
134
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 132

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)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 142

def browser_options(config)
  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

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

Iterate over each browser created by this factory.

Yields:

  • (browser)


164
165
166
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 164

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:



176
177
178
# File 'lib/mediawiki_selenium/browser_factory/base.rb', line 176

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

#teardown(_env, _status) ⇒ Object

Executes additional teardown tasks.

Parameters:

  • env (Environment)

    Environment.

  • status (Symbol)

    Status of the executed scenario.



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

def teardown(_env, _status)
  # abstract
end