anahorn

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

Jun 4th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.92 KB | None | 0 0
  1. require 'rubygems'
  2. require 'selenium-webdriver'
  3.  
  4. browser = Selenium::WebDriver.for :firefox
  5. browser.get "http://localhost/page9"
  6.  
  7. wait = Selenium::WebDriver::Wait.new(:timeout => 15)
  8.  
  9. # Execute 1st JavaScript function on the page and Cancel the prompt box
  10. browser.execute_script("enter_name()")
  11. a = browser.switch_to.alert
  12. if a.text == 'Please enter your name'
  13.   a.dismiss
  14. else
  15.   a.accept
  16. end
  17.  
  18. # Execute 2nd JavaScript function on the page and Accept the prompt box
  19. browser.execute_script("enter_age()")
  20. a = browser.switch_to.alert
  21. if a.text == 'Please enter your name'
  22.   a.dismiss
  23. else
  24.   a.send_keys("99")
  25.   a.accept
  26. end
  27.  
  28. # Find the age on the page by regexp
  29. puts "Test Passed: Page 9 Validated" if wait.until {
  30.  /99/.match(browser.page_source)
  31. }
  32.  
  33. browser.get "http://localhost/page9"
  34.  
  35. # Execute any custom JavaScript code
  36. puts browser.execute_script("return window.location.pathname")
  37.  
  38. browser.quit
Advertisement
Add Comment
Please, Sign In to add comment