Skip to content

Instantly share code, notes, and snippets.

View titusfortner's full-sized avatar

Titus Fortner titusfortner

View GitHub Profile
@titusfortner
titusfortner / basic.rb
Created November 29, 2012 18:48
Basic Webdriver example
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.get "http://google.com"
element = driver.find_element(:name => "q")
element.send_keys "Cheese!"
element.submit
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until { driver.title.downcase.start_with? "cheese!" }
driver.quit
@titusfortner
titusfortner / abstracted.rb
Created November 29, 2012 18:51
Abstracted Webdriver Example
include UtilityModule
initialize_test
go_to "http://google.com"
search_term = "Cheese!"
submit_text({:name => "q"}, search_term)
wait_for_element_present(:id => "search")
raise unless get_title.include?(search_term.downcase)
end_test
@titusfortner
titusfortner / page_object.rb
Created November 29, 2012 18:52
Page Object Webdriver Example
class GoogleHome
def initialize driver
@driver = driver
end
def goto
go_to "http://google.com"
return self
end
def search_field
@titusfortner
titusfortner / regexp_selector
Created September 17, 2014 19:23
Stale Element Stack Traces
/home/titus/.rvm/gems/ruby-2.1.2/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/remote/bridge.rb:519:in `getElementAttribute'
/home/titus/.rvm/gems/ruby-2.1.2/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/common/element.rb:98:in `attribute'
/home/titus/workspace/watir-webdriver/lib/watir-webdriver/locators/element_locator.rb:186:in `fetch_value'
/home/titus/workspace/watir-webdriver/lib/watir-webdriver/locators/element_locator.rb:192:in `block in matches_selector?'
/home/titus/workspace/watir-webdriver/lib/watir-webdriver/locators/element_locator.rb:191:in `each'
/home/titus/workspace/watir-webdriver/lib/watir-webdriver/locators/element_locator.rb:191:in `all?'
/home/titus/workspace/watir-webdriver/lib/watir-webdriver/locators/element_locator.rb:191:in `matches_selector?'
/home/titus/workspace/watir-webdriver/lib/watir-webdriver/locators/button_locator.rb:66:in `matches_selector?'
/home/titus/workspace/watir-webdriver/lib/watir-webdriver/locators/element_locator.rb:152:in `block in wd_find_by_r
@titusfortner
titusfortner / current_code
Last active August 29, 2015 14:07
Failing Wait Tests
it "should not throw error when running error checks on closed window" do
browser.close
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 5
browser = Watir::Browser.start(WatirSpec.url_for("window_switching.html"), :chrome, http_client: client)
browser.a(:id => "open").click
window = browser.window(:title => "closeable window")
window.use
browser.a(:id => "close").click
expect { browser.driver.current_url }.to_not raise_error
@titusfortner
titusfortner / target_locator
Created October 20, 2014 17:29
Rescue NoSuchWindow
def window(id)
if block_given?
begin
original = @bridge.getCurrentWindowHandle
rescue Selenium::WebDriver::Error::NoSuchWindowError
original = nil
end
@bridge.switchToWindow id
@titusfortner
titusfortner / bug.rb
Last active August 29, 2015 14:10
ChromeDriver Issue #950
require 'selenium-webdriver'
first_window = File.open('window1.html', 'w').tap {|f| f.puts "<html><head><title><Window1</title></head><body><p>Click <a id='open' href='#' onclick='window.open(\"window2.html\")'>here</a></body></html>" }
first_window.close
File.open('window2.html', 'w').tap {|f| f.puts "<html><head><title>window1</title></head><body><p>Click <a id='close' href='#' onclick=\"window.close()\">close</a></p></body></html>" }.close
driver = Selenium::WebDriver.for :chrome
driver.navigate.to "file://#{File.expand_path(File.dirname(__FILE__))}/#{first_window.path}"
driver.first(id: 'open').click
driver.switch_to.window(driver.window_handles.last)
driver.first(id: 'close').click
#driver.window_handles ### If you un-comment out this line, the test will pass
@titusfortner
titusfortner / match_element.rb
Created December 29, 2014 18:22
Watir Webdriver
require 'watir-webdriver'
b = Watir::Browser.new
b.goto "#{your_url}"
# Match first span with class 'title'
b.span(class: 'title')
# Array of spans matching class 'title'
b.spans(class: 'title')
@titusfortner
titusfortner / current_behavior.rb
Last active August 29, 2015 14:13
Always locate
context 'when set to true' do
before do
Watir.always_locate = true
browser.goto WatirSpec.url_for('removed_element.html', :needs_server => true)
end
it '#exists? relocates the element when it is stale' do
element = browser.div(:id => "text")
expect(element.exists?).to be true
@titusfortner
titusfortner / alerts.rb
Created April 10, 2015 17:03
Escape Alerts
def ensure_no_alert
@alert.accept if alert?
end
def alert?
@alert = @driver.switch_to.alert
true
rescue Selenium::WebDriver::Error::NoAlertPresentError
false
end