You have to learn how to walk before you can run -- this is one of the rules I follow when I'm trying to learn something new. Although I was able to do Selenium Ruby + Capybara + Cucumber setup on my previous post about starting to learn Ruby and automation using it, I felt like I need to go back because I could have missed an important basic knowledge.
I completed the Ruby in 20 minutes exercises to get a feel of basic Ruby syntax and also the Chapter 1 of The Ruby Programming Language by O'Reilly, I started creating my first test in Selenium Ruby.
--
Creating my first test
Reference/s:
Ruby in 20 minutes: https://www.ruby-lang.org/en/documentation/quickstart/
Selenium WebDriver Documentation ("Introducing the Selenium-WebDriver API by Example" section): https://www.seleniumhq.org/docs/03_webdriver.jsp
If "Hello World" is the first thing that most programming language tutorials teaches you, for Selenium it's probably doing a Google search for "Cheese". So that's what I did.
Note that the example for the 2nd reference I provided already gives the code for this exercise, which is fine for me. When I'm trying to learn something, I follow the code provided then I will try to understand each code as I type it then I will make a guess on how the output will turn out.
The test goes like this:
Here's my code:
I completed the Ruby in 20 minutes exercises to get a feel of basic Ruby syntax and also the Chapter 1 of The Ruby Programming Language by O'Reilly, I started creating my first test in Selenium Ruby.
--
Creating my first test
Reference/s:
Ruby in 20 minutes: https://www.ruby-lang.org/en/documentation/quickstart/
Selenium WebDriver Documentation ("Introducing the Selenium-WebDriver API by Example" section): https://www.seleniumhq.org/docs/03_webdriver.jsp
If "Hello World" is the first thing that most programming language tutorials teaches you, for Selenium it's probably doing a Google search for "Cheese". So that's what I did.
Note that the example for the 2nd reference I provided already gives the code for this exercise, which is fine for me. When I'm trying to learn something, I follow the code provided then I will try to understand each code as I type it then I will make a guess on how the output will turn out.
The test goes like this:
- Open the browser of your choice
- Open Google.com
- Type in "Cheese" and submit
- Verify that the page title starts with "cheese"
- Open the first search result
- Close the browser
Here's my code:
# # First Test - "Cheese" Google Search # # References: # https://www.seleniumhq.org/docs/03_webdriver.jsp # https://www.rubydoc.info/gems/selenium-webdriver/0.0.28/Selenium/WebDriver # #load selenium-webdriver gem require 'selenium-webdriver' #initialize webdriver driver = Selenium::WebDriver.for :firefox driver.manage.window.maximize driver.manage.timeouts.implicit_wait = 10 # seconds #open URL driver.get "http://google.com/" #perform search for "Cheese" element = driver.find_element(:name,'q') element.send_keys("Cheese") element.submit #wait for browser to move to next page wait = Selenium::WebDriver::Wait.new(:timeout => 10) wait.until { !driver.title.downcase.start_with? "google"} #display page title puts "Page title is '#{driver.title}'" #verify page title is_startWith = driver.title.downcase.start_with? "cheese" puts "Page title starts with 'cheese?' " + is_startWith.to_s #click first link element = driver.find_elements(:xpath,'//a/h3') str_linkText = element[0].text element[0].click puts "Clicked 1st link '" + str_linkText + "'" #kill web driver instance driver.quit