Handling Dialog Boxes Using Cucumber

Today I will share the code to handle dialog boxes using Cucumber .

Assuming that you are using cucumber with capybara ,following code can be used:


When /^I confirm popup$/ do
  page.driver.browser.switch_to.alert.accept    
end

When /^I dismiss popup$/ do
  page.driver.browser.switch_to.alert.dismiss
end

Using the above generic code ,popups can be accepted or rejected as required. It utilizes the selenium webdriver API to handle that as capybara is simply calling the Selenium API.

This can be handled directly using selenium webdriver as below:

require "selenium-webdriver"

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://mysite.com/page_with_alert.html"

driver.find_element(:name, 'element_with_alert_javascript').click
a = driver.switch_to.alert
if a.text == 'A value you are looking for'
  a.dismiss
else
  a.accept
end

Comments

Popular posts from this blog

Testing Webservice with SoapUI Having Windows Authentication

How to Re-run Failed Scenarios in Cucumber