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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/lapis_lazuli/browser.rb', line 83

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



203
204
205
206
207
208
# File 'lib/lapis_lazuli/browser.rb', line 203

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.



81
82
83
# File 'lib/lapis_lazuli/browser.rb', line 81

def browser_name
  @browser_name
end

#browser_wantedObject (readonly)

Returns the value of attribute browser_wanted.



81
82
83
# File 'lib/lapis_lazuli/browser.rb', line 81

def browser_wanted
  @browser_wanted
end

#optional_dataObject (readonly)

Returns the value of attribute optional_data.



81
82
83
# File 'lib/lapis_lazuli/browser.rb', line 81

def optional_data
  @optional_data
end

Class Method Details

.add_browser(b) ⇒ Object



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

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



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

def browsers
  return @@browsers
end

.check_world?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'lib/lapis_lazuli/browser.rb', line 68

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



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/lapis_lazuli/browser.rb', line 215

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|
      begin
        b.close reason, false
      rescue Exception => err
        # Provide some details
        @@world.log.debug("Failed to close the browser, probably chrome: #{err.to_s}")
      end
    end

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

.remove_browser(b) ⇒ Object



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

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

.set_world(w) ⇒ Object



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

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



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

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



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

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 "end"
    # Also ignored here - this is handled  in World.browser_destroy
  else
    if is_last_scenario?(scenario)
      # Close it
      LapisLazuli::Browser.close_all close_browser_after
    end
  end
end

#create(*args) ⇒ Object

Creates a new browser instance.



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

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

#destroy(world) ⇒ Object



210
211
212
213
# File 'lib/lapis_lazuli/browser.rb', line 210

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



99
100
101
102
103
104
105
# File 'lib/lapis_lazuli/browser.rb', line 99

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)


119
120
121
# File 'lib/lapis_lazuli/browser.rb', line 119

def is_open?
  return !@browser.nil?
end

#quitObject

Same as close



162
163
164
# File 'lib/lapis_lazuli/browser.rb', line 162

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)


196
197
198
199
200
201
# File 'lib/lapis_lazuli/browser.rb', line 196

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

#restartObject

Close and create a new browser



135
136
137
138
139
# File 'lib/lapis_lazuli/browser.rb', line 135

def restart
  world.log.debug "Restarting browser"
  @browser.close
  self.start
end

#start(*args) ⇒ Object

Start the browser if it’s not yet open.



125
126
127
128
129
130
131
# File 'lib/lapis_lazuli/browser.rb', line 125

def start(*args)
  if @browser.nil?
    @browser = init(*args)
    # Add this browser to the list of all browsers
    LapisLazuli::Browser.add_browser(self)
  end
end

#worldObject



113
114
115
# File 'lib/lapis_lazuli/browser.rb', line 113

def world
  @@world
end