anahorn

Selenium WebDriver Ruby: Part 4 (Images) 2

Jun 4th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.10 KB | None | 0 0
  1. require 'rubygems'
  2. require 'selenium-webdriver'
  3.  
  4. browser = Selenium::WebDriver.for :firefox
  5. browser.get "http://localhost/page4"
  6.  
  7. wait = Selenium::WebDriver::Wait.new(:timeout => 15)
  8.  
  9. # Check that the image exists using different attributes and xpath
  10.  
  11. puts "Test Passed: Found a graphic with the path 'images/WaterFaucet.jpg' in the source filename" if wait.until {
  12.     browser.find_element(:xpath => "//img[@src='images/WaterFaucet.jpg']").displayed?
  13. }
  14.  
  15. puts "Test Passed: Found a graphic with the name watergraphic" if wait.until {
  16.     browser.find_element(:name, "watergraphic").displayed?
  17. }
  18.  
  19. puts "Test Passed: Found a graphic with 'Image of water faucet' as alt text" if wait.until {
  20.     browser.find_element(:xpath => "//img[@alt='Image of water faucet']").displayed?
  21. }
  22.  
  23. # Click the image button
  24. image = wait.until {
  25.     element = browser.find_element(:xpath => "//img[@src='images/totoro.gif']")
  26.     element if element.displayed?
  27. }
  28. image.click
  29.  
  30. # Check the text on the new page
  31. puts "Test Passed: Page 1 Validated" if wait.until {
  32.     /Test Automation Training/.match(browser.page_source)
  33. }
  34.  
  35. browser.quit
Advertisement
Add Comment
Please, Sign In to add comment