Skip to content

Instantly share code, notes, and snippets.

View DylanLacey's full-sized avatar

The Gentlehacker DylanLacey

View GitHub Profile
@DylanLacey
DylanLacey / edited_test.rb
Last active December 14, 2015 06:49
An example of how to convert exported Selenium IDE tests into runnable tests with Sauce
#!/usr/bin/env ruby
# rubygems manages gem loading
require "rubygems"
require "selenium-webdriver"
# test::unit is now part of the standard suite
require "test/unit"
class RubyExample < Test::Unit::TestCase
@DylanLacey
DylanLacey / attr.rb
Created March 5, 2013 22:25
Class Level Methods, how does THAT work?!
tracked_attr_accessor :something
def self.tracked_attr_accessor(*methods)
methods.each do |method|
attr_reader method
self.define_method("#{method}=") do |arg|
if method != arg
updated_fields << method
instance_variable_set("@#{method}", arg)
end
@DylanLacey
DylanLacey / auth_by_itself.rb
Created March 22, 2013 04:35
Using RestClient with the Sauce Labs REST API
http = "https://saucelabs.com/rest/v1/#{username}/jobs/#{job_id}"
body = {"name" => "Log In Test"}.to_json
RestClient::Request.execute(
:method => :put,
:url => http,
:user => username,
:password => access_key,
:headers => {:content_type => "application/json"},
@DylanLacey
DylanLacey / Gemfile
Last active December 15, 2015 08:58
Using Sauce with Cucumber-Rails
group :test do
gem 'cucumber-rails', :require => false
gem 'sauce-cucumber', :require => false
end
@DylanLacey
DylanLacey / aspec.rb
Created May 28, 2013 07:46
Spec_helper
require "spec_helper"
describe "Some functionality", :js => true do
it "does a thing" do
visit "a page"
page.driver.browser.switch_to :alert
end
end
@DylanLacey
DylanLacey / a_step_definition.rb
Created June 12, 2013 11:12
Basic instructions for running Cucumber with Sauce, without using the sauce-cucumber gem. If you're not opposed to the sauce-cucumber gem, it gives you job status reporting, naming, and some other niceties.
## features/step_definitions/a_step_definition.rb
Given /$Anything$/
$driver.navigate.to "http://www.something.awesome.com"
end
@DylanLacey
DylanLacey / upload_a_file.rb
Last active December 20, 2015 19:19
Using setFileDetector from Cucumber with Capybara
Given /^I upload (\w+)$/ do |filepath|
# page.driver.browser gives you the Selenium object
selenium = page.driver.browser
selenium.file_detector = lambda do |args|
# Capybara checks the file exists so we can just return the first argument, the file path
args.first.to_s
end
# Scope to a specific form
@DylanLacey
DylanLacey / sauce_formatter.rb
Created September 24, 2013 17:59
Hijacking the "Pretty" formatter to tag individual actions for Sauce with the step being executed
require "cucumber/formatter/pretty"
module Sauce
class SauceFormatter < Cucumber::Formatter::Pretty
def initialize(step_mother, io, options)
super
end
def before_step(step)
message = "Cucumber step -- #{step.name}"
@DylanLacey
DylanLacey / gist:8311425
Created January 8, 2014 03:42
Debugging what's calling quit in Capybara
def quit
STDERR.puts "Capybara::Selenium::Driver.quit called by #{caller}"
@browser.quit if @browser
rescue Errno::ECONNREFUSED
require 'selenium-webdriver'
require 'selenium/webdriver/remote/http/persistent'
http_client = ::Selenium::WebDriver::Remote::Http::Persistent.new
url = "http://#{ENV["SAUCE_USERNAME"]}:#{ENV["SAUCE_ACCESS_KEY"]}@ondemand.saucelabs.com:80/wd/hub"
capabilities = Selenium::WebDriver::Remote::Capabilities.safari
capabilities.platform = 'OS X 10.6'
capabilities.version = '5'
Capybara.register_driver :remote_browser do |app|
Capybara::Selenium::Driver.new(app,
:browser => :remote, :url => url,