Mario Stoilov Mario Stoilov - 6 months ago 14
Ruby Question

Execute code whenever page is navigated

I am looking for a way to detect whenever the current page is navigated in selenium so I can execute some code. I know in C# and Java there are events for this, but I cannot seem to find them in ruby. Can anyone point me in the right direction?

Answer

Here's an example, from the doc:

class NavigationListener < Selenium::WebDriver::Support::AbstractEventListener
  def initialize(log)
    @log = log
  end

  def before_navigate_to(url, driver)
    @log.info "navigating to #{url}"
  end

  def after_navigate_to(url, driver)
    @log.info "done navigating to #{url}"
  end
end

listener = NavigationListener.new(logger)
driver = Selenium::WebDriver.for :firefox, :listener => listener

It's probably incomplete but it's a start.

Comments