Skip to content

Instantly share code, notes, and snippets.

View tourdedave's full-sized avatar

Dave Piacente tourdedave

View GitHub Profile
require 'selenium-webdriver'
class BasePage
def initialize(driver)
@driver = driver
end
def visit(url)
@driver.get url
Feature: The shopping cart should be able to accept products chosen by user.
# This would be better written from the users's perspective since that is what the scenarios entail
# e.g., Feature: Users can add products to their shopping cart
Scenario: Anonymous user adds product to the cart
Given I am on a product page
When I click on Add To Cart button
And I click on the Add to Cart button in the product description modal
Then I should see "$68.99" in the grand total section
And I should see "You have 1 item ($68.99) in your shopping cart." in the cart summary
require 'selenium-webdriver'
describe 'Login' do
before(:each) do
@driver = Selenium::WebDriver.for :firefox
end
after(:each) do
@driver.quit
source 'https://rubygems.org'
gem 'rspec', '~> 2.14.0'
gem 'selenium-webdriver'

The First Thing You Need To Know

Selenium is a software robot sent from the future to help us test web applications. But keep in mind that it's not one of those fancy shape-shifting robots than can run really fast. Instead it's more like one of those really strong robots that's not very fast and is best suited for accomplishing a certain objective.

That is to say -- Selenium is really good at a specific set of things. If you know what those are and stick to them then you will be able to easily write reliable, scalable, and maintainable tests that you and your team can trust.

But before we go too much further, there are a few things you'll want to get sorted prior to writing your first test.

Define a Test Strategy

How To Write a Good Acceptance Test

In order to write Selenium tests that are maintainable, resilient, and performant, there are some simple guidelines to follow:

  • Write atomic and autonomous tests
  • Group like tests together in small batches
  • Be descriptive
  • Use a Test Runner
  • Store tests in a Version Control System
<article class="markdown-body entry-content" itemprop="mainContentOfPage"><h2>
<a name="user-content-the-problem" class="anchor" href="#the-problem" aria-hidden="true"><span class="octicon octicon-link"></span></a>The Problem</h2>
<p>Sometimes you'll run into an app that has functionality hidden behind a right-click menu (a.k.a. context menu). These menus tend to be system level menus that are untouchable by Selenium. So how do you test this functionality?</p>
<h2>
<a name="user-content-a-solution" class="anchor" href="#a-solution" aria-hidden="true"><span class="octicon octicon-link"></span></a>A Solution</h2>
<p>By leveraging <a href="https://selenium.googlecode.com/git/docs/api/rb/Selenium/WebDriver/ActionBuilder.html">Selenium's Action Builder</a> we can issue a right-click command (a.k.a. <a href="https://selenium.googlecode.com/git/docs/api/rb/Selenium/WebDriver/ActionBuilder.html#context_click-instance_method"><code>context_click</code></a>).</p>
source 'https://rubygems.org'
gem 'rspec', '~> 2.14.0'
gem 'selenium-webdriver'
gem 'sauce_whisk'
gem 'sauce-connect'
gem 'parallel_tests'
gem 'rake'
gem 'rspec_junit_formatter'
require 'selenium-webdriver'
require 'rspec/expectations'
require 'rest-client'
include RSpec::Matchers
def setup
@driver = Selenium::WebDriver.for :firefox
end
desc "Run tests in Safari"
task :safari, :version, :os do |t, args|
ENV['browser'] = 'safari'
ENV['browser_version'] = args[:version]
ENV['operating_system'] = args[:os]
launch_in_parallel('config/cloud.rb')
end