anahorn

Selenium WebDriver Ruby: Part 2

Jun 4th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.66 KB | None | 0 0
  1. require 'rubygems'
  2. require 'selenium-webdriver'
  3.  
  4. browser = Selenium::WebDriver.for :firefox
  5. browser.get "http://localhost/page2"
  6.  
  7. wait = Selenium::WebDriver::Wait.new(:timeout => 15)
  8.  
  9. # Verify the existence of different elements and text
  10.  
  11. puts "Test Passed: Link with the href 'http://www.elegantcode.com/' found by XPATH" if wait.until {
  12.     browser.find_element(:xpath => "//a[@href='http://www.elegantcode.com/']").displayed?
  13. }
  14.  
  15. puts "Test Passed: Link with the id '2' found by XPATH" if wait.until {
  16.     browser.find_element(:xpath => "//a[@id='2']").displayed?
  17. }
  18.  
  19. puts "Test Passed: Link with the name 'slashdotlink' found by NAME locator" if wait.until {
  20.     browser.find_element(:name => "slashdotlink").displayed?
  21. }
  22.  
  23. puts "Test Passed: Link with the id '4' found by ID locator" if wait.until {
  24.     browser.find_element(:id => 4).displayed?
  25. }
  26.  
  27. puts "Display the text for link 1: " + browser.find_element(:id, 1).text if wait.until {
  28.     browser.find_element(:id, 1).displayed?
  29. }
  30.  
  31. # Direct match
  32. puts "Test Passed: Link with id 2 and text containing the text 'Yahoo' found" if wait.until {
  33.     browser.find_element(:id, 2).text == 'Yahoo'
  34. }
  35.  
  36. # Regexp match
  37. puts "Test Passed: Link with id 2 and text containing the text '...hoo...' found" if wait.until {
  38.     browser.find_element(:id, 2).text =~ /hoo/
  39. }
  40.  
  41. # Click on a link
  42. link = wait.until {
  43.     element = browser.find_element(:id, 3)
  44.     element if element.displayed?
  45. }
  46. link.click
  47.  
  48. # Check that the link with the given title is displayed
  49. puts "Test Passed: 'Submit Story' Link on slashdot found" if wait.until {
  50.     browser.find_element( :xpath, "//*[@title='Submit a story to Slashdot']" ).displayed?
  51. }
  52.  
  53. browser.quit
Advertisement
Add Comment
Please, Sign In to add comment