Skip to content

Instantly share code, notes, and snippets.

@thash
Last active August 29, 2015 14:11
Show Gist options
  • Save thash/61ae3a24d4450767a4be to your computer and use it in GitHub Desktop.
Save thash/61ae3a24d4450767a4be to your computer and use it in GitHub Desktop.
s.fill_in :guess_3, with: 6857
s.fill_in :captcha, with: 71355
s.click_button 'Check'
s.save_screenshot 'check.png'
source 'https://rubygems.org'
gem 'capybara-webkit'
GEM
remote: https://rubygems.org/
specs:
capybara (2.4.4)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
capybara-webkit (1.3.1)
capybara (>= 2.0.2, < 2.5.0)
json
json (1.8.1)
mime-types (2.4.3)
mini_portile (0.6.1)
nokogiri (1.6.5)
mini_portile (~> 0.6.0)
rack (1.5.2)
rack-test (0.6.2)
rack (>= 1.0)
xpath (2.0.0)
nokogiri (~> 1.3)
PLATFORMS
ruby
DEPENDENCIES
capybara-webkit
rows.first.class
#=> Capybara::Node::Element
[28] pry(main)> ls rows.first
Capybara::Node::Finders#methods: all field_labeled find find_button find_by_id find_field find_link first
Capybara::Node::Actions#methods:
attach_file check choose click_button click_link click_link_or_button click_on fill_in select uncheck unselect
Capybara::Node::Matchers#methods:
== has_button? has_link? has_no_field? has_no_text? has_table?
assert_no_selector has_checked_field? has_no_button? has_no_link? has_no_unchecked_field? has_text?
assert_no_text has_content? has_no_checked_field? has_no_select? has_no_xpath? has_unchecked_field?
assert_selector has_css? has_no_content? has_no_selector? has_select? has_xpath?
assert_text has_field? has_no_css? has_no_table? has_selector? refute_selector
Capybara::Node::Base#methods: base find_css find_xpath parent session synchronize
Capybara::Node::Element#methods:
[] checked? disabled? drag_to inspect path right_click selected? tag_name trigger value
allow_reload! click double_click hover native reload select_option set text unselect_option visible?
s.find('.problem_content').text
=> "The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?"
within("#login-form") do
fill_in 'email', with: 'user@example.com'
fill_in 'password', with: 'password'
end
click_button 'ログイン'
s.save_screenshot 'signin.png'
require 'bundler/setup'
Bundler.require
Capybara.javascript_driver = :webkit
s = Capybara::Session.new(:webkit)
s.visit 'https://projecteuler.net/about'
s.click_link 'Sign In'
require 'prime'
13195.prime_division
# => [[5, 1], [7, 1], [13, 1], [29, 1]]
600851475143.prime_division
# => [[71, 1], [839, 1], [1471, 1], [6857, 1]]
600851475143.prime_division.map{|pair| pair.first }.max
# => 6857
s.fill_in :username, with: 'yourusername'
s.fill_in :password, with: 'yourpassword'
s.click_button 'Sign In'
class Problem < Struct.new(:id, :title, :solved)
def url
'https://projecteuler.net/problem=' + id.to_s
end
end
rows = s.all('#problems_table > tbody > tr').drop(1) # dropでヘッダー行を取り除く
# テーブルの情報を元に各行からProblemインスタンスを生成
problems = rows.map do |row|
cells = row.all('td')
Problem.new(*[cells[0].text.to_i, cells[1].text, cells[3].text.length > 0])
end
problems.take(4)
# => [#<struct Problem id=1, title="Multiples of 3 and 5", solved=true>,
# #<struct Problem id=2, title="Even Fibonacci numbers", solved=true>,
# #<struct Problem id=3, title="Largest prime factor", solved=false>,
# #<struct Problem id=4, title="Largest palindrome product", solved=false>]
# まだ解いてない問題をひとつ選び、問題ページを開く
problem = problems.select{|p| !p.solved }.first
s.visit problem.url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment