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::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?

Constructor Details

#initialize(*args) ⇒ Browser

Returns a new instance of Browser.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lapis_lazuli/browser.rb', line 73

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



214
215
216
217
218
219
# File 'lib/lapis_lazuli/browser.rb', line 214

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_argsObject (readonly)

Returns the value of attribute browser_args.



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

def browser_args
  @browser_args
end

Class Method Details

.add_browser(b) ⇒ Object



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

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



42
43
44
# File 'lib/lapis_lazuli/browser.rb', line 42

def browsers
  return @@browsers
end

.check_world?Boolean

Returns:

  • (Boolean)


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

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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/lapis_lazuli/browser.rb', line 226

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



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

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

.set_world(w) ⇒ Object



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

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/lapis_lazuli/browser.rb', line 134

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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/lapis_lazuli/browser.rb', line 163

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.



98
99
100
# File 'lib/lapis_lazuli/browser.rb', line 98

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

#destroy(world) ⇒ Object



221
222
223
224
# File 'lib/lapis_lazuli/browser.rb', line 221

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



89
90
91
92
93
94
# File 'lib/lapis_lazuli/browser.rb', line 89

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

#is_last_scenario?(scenario) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/lapis_lazuli/browser.rb', line 183

def is_last_scenario?(scenario)
  begin
    feature_file = File.read(scenario.location.file)
    gherkin_object = Gherkin::Parser.new.parse(feature_file)
    last_line = gherkin_object[:feature][:children].last[:scenario][:examples].last[:table_body].last[:location][:line] rescue nil
    unless last_line
      last_line = gherkin_object[:feature][:children].last[:scenario][:location][:line] rescue nil
    end
    if last_line
      return last_line == scenario.location.line
    else
      warn 'Failed to find the last line of the feature trying to determine if this is the last scenario running.'
      return false
    end
  rescue Exception => e
    warn 'Something went wrong trying to determine if this is the last sceanrio of the feature.'
    warn e
  end
end

#is_open?Boolean

Return if the browser is open

Returns:

  • (Boolean)


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

def is_open?
  return !@browser.nil?
end

#quitObject

Same as close



153
154
155
# File 'lib/lapis_lazuli/browser.rb', line 153

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)


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

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



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

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.



114
115
116
117
118
119
120
121
122
# File 'lib/lapis_lazuli/browser.rb', line 114

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



102
103
104
# File 'lib/lapis_lazuli/browser.rb', line 102

def world
  @@world
end