Selenium WebDriver Ruby: Part 9 (JavaScript, Popups) 2

                Never    
Ruby
       
require 'rubygems'
require 'selenium-webdriver'

browser = Selenium::WebDriver.for :firefox
browser.get "http://localhost/page9"

wait = Selenium::WebDriver::Wait.new(:timeout => 15)

# Execute 1st JavaScript function on the page and Cancel the prompt box
browser.execute_script("enter_name()")
a = browser.switch_to.alert
if a.text == 'Please enter your name'
  a.dismiss
else
  a.accept
end

# Execute 2nd JavaScript function on the page and Accept the prompt box
browser.execute_script("enter_age()")
a = browser.switch_to.alert
if a.text == 'Please enter your name'
  a.dismiss
else
  a.send_keys("99")
  a.accept
end

# Find the age on the page by regexp
puts "Test Passed: Page 9 Validated" if wait.until {
 /99/.match(browser.page_source)
}

browser.get "http://localhost/page9"

# Execute any custom JavaScript code
puts browser.execute_script("return window.location.pathname")

browser.quit

Raw Text