Class: LapisLazuli::Browser

Overview

Extension to the Watir browser

This class handles initialization, for the most part. BrowserModules included here can rely on world being set to the current cucumber world object, and for some WorldModules to exist in it (see assertions in constructor).

Constant Summary collapse

@@world =
nil
@@cached_browser_options =
{}
@@browsers =
[]

Constants included from LapisLazuli::BrowserModule::Interaction

LapisLazuli::BrowserModule::Interaction::DEFAULT_CLICK_TYPES

Constants included from ArgParse

ArgParse::ERROR_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#assertions, #assertions=

Methods included from GenericModule::XPath

#xp_and, #xp_contains, #xp_not, #xp_or

Methods included from LapisLazuli::BrowserModule::Remote

#remote_browser_config

Methods included from LapisLazuli::BrowserModule::Interaction

#click_type, #click_types, #force_click, #js_click, #on_click

Methods included from ArgParse

#make_list_from_item, #make_list_from_nested, #parse_args

Methods included from LapisLazuli::BrowserModule::Screenshots

#screenshot_name, #take_screenshot

Methods included from LapisLazuli::BrowserModule::Wait

#multi_wait, #multi_wait_all, #wait, #wait_all

Methods included from LapisLazuli::BrowserModule::Find

#find, #find_all, #multi_find, #multi_find_all, #pick_one

Methods included from LapisLazuli::BrowserModule::Error

#get_html_errors, #get_http_status, #get_js_errors, #has_error?

Methods included from Ast

#is_cucumber_2?, #is_last_scenario?, #is_scenario?, #is_table_row?, #scenario_id

Constructor Details

#initialize(*args) ⇒ Browser

Returns a new instance of Browser.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lapis_lazuli/browser.rb', line 79

def initialize(*args)
  # The class only works with some modules loaded; they're loaded by the
  # Browser module, but we can't be sure that's been used.
  LapisLazuli::Browser.check_world?

  self.start(*args)

  # Add registered world modules.
  if not LapisLazuli::WorldModule::Browser.browser_modules.nil?
    LapisLazuli::WorldModule::Browser.browser_modules.each do |ext|
      self.extend(ext)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



200
201
202
203
204
205
# File 'lib/lapis_lazuli/browser.rb', line 200

def method_missing(meth, *args, &block)
  if !@browser.nil? and @browser.respond_to? meth
    return @browser.send(meth.to_s, *args, &block)
  end
  return super
end

Instance Attribute Details

#browser_nameObject (readonly)

Returns the value of attribute browser_name.



77
78
79
# File 'lib/lapis_lazuli/browser.rb', line 77

def browser_name
  @browser_name
end

#browser_wantedObject (readonly)

Returns the value of attribute browser_wanted.



77
78
79
# File 'lib/lapis_lazuli/browser.rb', line 77

def browser_wanted
  @browser_wanted
end

#optional_dataObject (readonly)

Returns the value of attribute optional_data.



77
78
79
# File 'lib/lapis_lazuli/browser.rb', line 77

def optional_data
  @optional_data
end

Class Method Details

.add_browser(b) ⇒ Object



50
51
52
53
54
# File 'lib/lapis_lazuli/browser.rb', line 50

def add_browser(b)
  # Add destructor for all browsers
  Runtime.instance.set_if(self, :browsers, LapisLazuli::Browser.method(:close_all))
  @@browsers.push(b)
end

.browsersObject



46
47
48
# File 'lib/lapis_lazuli/browser.rb', line 46

def browsers
  return @@browsers
end

.check_world?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
# File 'lib/lapis_lazuli/browser.rb', line 64

def check_world?
  assert @@world.respond_to?(:config), "Need to include LapisLazuli::WorldModule::Config in your cucumber world."
  assert @@world.respond_to?(:log), "Need to include LapisLazuli::WorldModule::Logging in your cucumber world."
  assert @@world.respond_to?(:error), "Need to include LapisLazuli::WorldModule::Error in your cucumber world."
  assert @@world.respond_to?(:has_proxy?), "Need to include LapisLazuli::WorldModule::Proxy in your cucumber world."
end

.close_all(reason = nil) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/lapis_lazuli/browser.rb', line 212

def self.close_all(reason=nil)
  # A running browser should exist and we are allowed to close it
  if @@browsers.length != 0 and @@world.env_or_config("close_browser_after") != "never"
    # Notify user
    @@world.log.debug("Closing all browsers")
    # Close each browser
    @@browsers.each do |b|
      b.close reason, true
    end

    # Make sure the array is cleared
    @@browsers = []
  end
end

.remove_browser(b) ⇒ Object



56
57
58
# File 'lib/lapis_lazuli/browser.rb', line 56

def remove_browser(b)
  @@browsers.delete(b)
end

.set_world(w) ⇒ Object



60
61
62
# File 'lib/lapis_lazuli/browser.rb', line 60

def set_world(w)
  @@world = w
end

Instance Method Details

#close(reason = nil, remove_from_list = true) ⇒ Object

Closes the browser and updates LL so that it will open a new one if needed



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/lapis_lazuli/browser.rb', line 141

def close(reason = nil, remove_from_list=true)
  if not @browser.nil?
    if not reason.nil?
      reason = " after #{reason}"
    else
      reason = ""
    end

    world.log.debug "Closing browser#{reason}: #{@browser}"
    @browser.close
    if remove_from_list
      LapisLazuli::Browser.remove_browser(self)
    end
    @browser = nil
  end
end

#close_after_scenario(scenario) ⇒ Object

Close after scenario will close the browser depending on the close_browser_after configuration

Valid config options: feature, scenario, end, never Default: feature



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/lapis_lazuli/browser.rb', line 170

def close_after_scenario(scenario)
  # Determine the config
  close_browser_after = world.env_or_config("close_browser_after")
  case close_browser_after
    when "scenario"
      # We always close it
      LapisLazuli::Browser.close_all close_browser_after
    when "never"
      # Do nothing: party time, excellent!
    when "feature"
      if is_last_scenario?(scenario)
        # Close it
        LapisLazuli::Browser.close_all close_browser_after
      end
    else # close after 'end' is now default
      # Also ignored here - this is handled  in World.browser_destroy
  end
end

#create(*args) ⇒ Object

Creates a new browser instance.



105
106
107
# File 'lib/lapis_lazuli/browser.rb', line 105

def create(*args)
  return Browser.new(*args)
end

#destroy(world) ⇒ Object



207
208
209
210
# File 'lib/lapis_lazuli/browser.rb', line 207

def destroy(world)
  # Primary browser should also close other browsers
  LapisLazuli::Browser.close_all("end")
end

#initialize_copy(source) ⇒ Object

Support browser.dup to create a duplicate



95
96
97
98
99
100
101
# File 'lib/lapis_lazuli/browser.rb', line 95

def initialize_copy(source)
  super
  @optional_data = @optional_data.dup
  @browser = create_driver(@browser_wanted, @optional_data)
  # Add this browser to the list of all browsers
  LapisLazuli::Browser.add_browser(self)
end

#is_open?Boolean

Return if the browser is open

Returns:

  • (Boolean)


115
116
117
# File 'lib/lapis_lazuli/browser.rb', line 115

def is_open?
  return !@browser.nil?
end

#quitObject

Same as close



160
161
162
# File 'lib/lapis_lazuli/browser.rb', line 160

def quit
  self.close
end

#respond_to?(meth) ⇒ Boolean

Map any missing method to the browser object Example ll.browser.goto “www.spritecloud.com

Returns:

  • (Boolean)


193
194
195
196
197
198
# File 'lib/lapis_lazuli/browser.rb', line 193

def respond_to?(meth)
  if !@browser.nil? and @browser.respond_to? meth
    return true
  end
  return super
end

#restart(*args) ⇒ Object

Close and create a new browser



133
134
135
136
137
# File 'lib/lapis_lazuli/browser.rb', line 133

def restart(*args)
  world.log.debug "Restarting browser"
  self.close
  self.start(*args)
end

#start(*args) ⇒ Object

Start the browser if it’s not yet open.



121
122
123
124
125
126
127
128
129
# File 'lib/lapis_lazuli/browser.rb', line 121

def start(*args)
  if @browser.nil?
    @browser = init(*args)
    # Add this browser to the list of all browsers
    LapisLazuli::Browser.add_browser(self)
    # Making sure all browsers are gracefully closed when the exit event is triggered.
    at_exit {LapisLazuli::Browser::close_all 'exit event trigger'}
  end
end

#worldObject



109
110
111
# File 'lib/lapis_lazuli/browser.rb', line 109

def world
  @@world
end