yudi2312
yudi2312

Reputation: 323

Continue execution in Cucumber in case of any errors

I'm building an automation suite using Ruby/Selenium-WebDriver/Cucumber. What I want to achieve is to resume the cucumber scenario in case of any unexpected errors.

For e.g. Unexpected pop-ups

I might get a modal dialog at any point in the application. I want my code to close the pop-ups whenever the exception occurs and resume the execution.

The point of doing this is, the automation suite will run for multiple days on multiple systems. There won't be any kind of monitoring except logs and output reports. I don't want these unwanted exception to hamper the execution.

Upvotes: 1

Views: 1188

Answers (2)

Justin Ko
Justin Ko

Reputation: 46836

Given that the alerts can be opened at any time, the best option may be to use an AbstractEventListener. This allows you to perform actions before (or after) Selenium interacts with the browser. This means that you could call your alert closing code right before each interaction.

The sample event listener would be defined like:

class AlertListener < Selenium::WebDriver::Support::AbstractEventListener
  def close_alerts(driver)
    # Assuming you want to handle the dialogs using Watir code instead of Selenium,
    # convert the Selenium::WebDriver to a Watir::Browser
    browser = Watir::Browser.new(driver)

    # Run whatever code you have for handling the dialog instances
    browser.alert.ok if browser.alert.exists?
  end

  def before_change_value_of(element, driver)
    close_alerts(driver)
  end

  def before_click(element, driver)
    close_alerts(driver)
  end

  def before_close(driver)
    close_alerts(driver)
  end

  def before_execute_script(script, driver)
    close_alerts(driver)
  end

  def before_find(by, what, driver)
    close_alerts(driver)
  end

  def before_navigate_back(driver)
    close_alerts(driver)
  end

  def before_navigate_forward(driver)
    close_alerts(driver)
  end

  def before_navigate_to(url, driver)
    close_alerts(driver)
  end

  def before_quit(driver)
    close_alerts(driver)
  end    
end

Note that you would replace the close_alerts method with whatever code you have already written for handling the alerts. The event listener is Selenium, which means you need to either write Selenium code or convert the element/driver to Watir (which is what is done in the example).

Once you have the listener created, you need to pass it to the browser during initialization:

listener = AlertListener.new
browser = Watir::Browser.new :chrome, :listener => listener

Upvotes: 2

Stipe
Stipe

Reputation: 510

You could get your goal by accepting any alert that pops up on the screen.

You can do this in following steps:

Given(/^I should see the error message and accept it$/) do
  def alert_accept
    end
end

So whenever there is a popup it will accept it and continue forward.

You can find this step as well here:

https://github.com/ModusCreateOrg/cucumber-watir/blob/appium/features/step_definitions/appium_steps.rb

Upvotes: 0

Related Questions