Skip to content

Instantly share code, notes, and snippets.

@gongo
Last active December 22, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gongo/6509722 to your computer and use it in GitHub Desktop.
Save gongo/6509722 to your computer and use it in GitHub Desktop.
Selenium の Stale Element Reference Exception を出してみたい
source "https://rubygems.org"
gem 'capybara'
gem 'selenium-webdriver'
gem 'sinatra'
# -*- coding: utf-8 -*-
ENV['RACK_ENV'] = 'test'
require_relative './web'
require 'capybara'
require 'selenium-webdriver'
require 'test/unit'
require 'capybara/node/base'
module Capybara
module Node
class Base
old_sync = instance_method(:synchronize)
define_method :synchronize do |*args, &block|
retry_flag = true
begin
old_sync.bind(self).(*args, &block)
rescue ::Selenium::WebDriver::Error::StaleElementReferenceError => e
raise e unless retry_flag
retry_flag = false
retry
end
end
end
end
end
Capybara.register_driver :remote do |app|
caps = Selenium::WebDriver::Remote::Capabilities.firefox
url = "http://localhost:4444/wd/hub/"
opts = { desired_capabilities: caps, browser: :remote, url: url }
Capybara::Selenium::Driver.new(app, opts)
end
class HelloWorldTest < Test::Unit::TestCase
include Capybara::DSL
Capybara.default_driver = :selenium
Capybara.default_driver = :remote
def setup
Capybara.app = Sinatra::Application.new
end
def test_button_and_goto
visit '/'
page.click_button('push')
assert page.find(:css, 'h1').has_content?('FooBarBaz')
end
end
require 'sinatra'
require 'erb'
get '/' do
erb :index
end
get '/foo' do
erb :foo
end
post '/bar' do
sleep 2
'ok'
end
__END__
@@ layout
<!DOCTYPE html>
<body>
<%= yield %>
</body>
</html>
@@ index
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function() {
$('#foo').click(function() {
$.post(
'/bar',
{ 'baz':'hoge' },
function(data) {
location.href = '/foo';
}
)
});
});
</script>
<h1>Hello, World!</h1>
<input type="button" id="foo" value="push" />
@@ foo
<h1>FooBarBaz</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment