Skip to main content

First Time Selenium Ruby in Windows

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:
  1. Open the browser of your choice
  2. Open Google.com
  3. Type in "Cheese" and submit
  4. Verify that the page title starts with "cheese"
  5. Open the first search result
  6. 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

Popular posts from this blog

Reframing how I identify bug root causes

It's been more than a year now since I've set up our bug tracking in JIRA. In there, I've set up an Issue Root Cause custom field where it had the following options: Incorrect coding Unclear / Missing / Incorrect Requirements Unclear / Missing / Incorrect Design Insufficient / Duplicated / Incorrect Testing Deployment Issue Environment Third-party issue My thoughts when I listed these as options is so that it would be easy to identify which team is responsible for the cause why we had that bug. They're pretty straightforward -  Incorrect coding  is of course when the developers didn't follow the expectations,  Unclear / Missing / Incorrect Requirements is because there's a gap in the requirement, and so on. And also, it's because that was the way it's done in my previous company so that's also my initial knowledge source. Recently, I've been reading a few articles regarding Shift Left, reducing silos, and generally how quality is a team activit...

QA Tools: Custom Test Case Generator in Google Sheet using Google App Script

When testers have to document test cases, it's usually done in the traditional format of putting all your test cases and steps in one sheet. Once it accumulates, for me it can be overwhelming to look at. It looks something like this: As a solution, I decided to make a tool that will help me focus on only drafting my test scenarios and look at my test cases one at a time. I'd like to share this here with everyone else who's like me. Hopefully, it can make your testing journey even a little better. Test Case Generator Tool This tool is for QA or non-QAs who need to write test cases. What it can do: Write your test scenarios in one tab Focus on writing steps for each test case one sheet at a time Generate an import file Generate a test execution document Sample test execution document: Scroll to the bottom to see how it works. Sheet Purpose Test Scenarios This is where you set the details for this test set/the project/story/epic where you're creating the test cases u...

A Bug's Life! Defect Management Process in Software Testing

First things first, what does a "bug" mean in the software development process? A bug is what we call a behavior that is different from what's expected based on the provided requirements. A tester's main purpose is to find and report bugs in the system as early as possible. In some companies, it's sometimes called a "defect". They actually mean slightly different if we go by the book. In this post, I'll be covering the lifecycle of a software bug which are issues that are found during the development phase. You're testing the system and you found a bug. What is the first thing you do? a) You immediately write a bug report b) You try to reproduce it c) You complain "How could this very obvious bug reach QA? Dev should've spotted this earlier ugh." The formal answer is B (I'll just let you figure out what's the informal answer), It's common for a QA to be gaslighted. "I cannot reproduce this bug.", "It doesn...