Skip to content

Instantly share code, notes, and snippets.

@snobutaka
Last active March 18, 2018 02:39
Show Gist options
  • Save snobutaka/a0250e269c653165307a34ca6c5d8d61 to your computer and use it in GitHub Desktop.
Save snobutaka/a0250e269c653165307a34ca6c5d8d61 to your computer and use it in GitHub Desktop.
Hello Selenium with Ruby
#!/usr/bin/env ruby
# coding: utf-8
require 'test/unit'
require 'uri'
require 'selenium-webdriver' # http://www.rubydoc.info/gems/selenium-webdriver/
#
# 各種検索エンジンで「Ruby」を検索すると,
# トップヒットが Ruby の公式サイト ("www.ruby-lang.org") となることをテストする.
#
class SearchRankTest < Test::Unit::TestCase
Keyword = 'Ruby'
ExpectingSite = 'www.ruby-lang.org'
def setup()
@driver = Selenium::WebDriver.for :chrome
@driver.manage.timeouts.implicit_wait = 5 # seconds
end
def teardown()
@driver.quit()
end
#
# Google の I'm Feeling Lucky をテストする.
#
def test_feeling_lucky_ruby()
@driver.get('http://google.com')
@driver.find_element(:id => 'lst-ib').send_keys(Keyword)
@driver.find_element(:xpath => '//*[@id="tsf"]/div[2]/div[3]/center/input[2]').click()
@driver.save_screenshot('./feeling-lucky-ruby.png')
assert_equal(ExpectingSite, URI.parse(@driver.current_url).host)
end
#
# DuckDuckGo をテストする.
#
def test_duckduckgo_ruby()
@driver.get('https://duckduckgo.com')
@driver.find_element(:id => 'search_form_input_homepage').send_keys(Keyword)
@driver.find_element(:id => 'search_button_homepage').click()
@driver.find_element(:id => 'r1-0').click()
@driver.save_screenshot('./duckduckgo-ruby.png')
assert_equal(ExpectingSite, URI.parse(@driver.current_url).host)
end
#
# Yahoo! Japan をテストする.
#
def test_yahoojapan_ruby()
@driver.get('https://www.yahoo.co.jp')
@driver.find_element(:id => 'srchtxt').send_keys(Keyword)
@driver.find_element(:id => 'srchbtn').click()
@driver.find_element(:xpath => '//*[@id="WS2m"]/div[1]/div[1]/h3/a').click()
@driver.save_screenshot('./yahoo-ruby.png')
assert_equal(ExpectingSite, URI.parse(@driver.current_url).host)
end
end
#!/usr/bin/env ruby
# coding: utf-8
require 'test/unit'
require 'uri'
require 'selenium-webdriver' # http://www.rubydoc.info/gems/selenium-webdriver/
#
# 各種検索エンジンで「Ruby」を検索すると,
# トップヒットが [Ruby の公式サイト](www.ruby-lang.org) となることをテストする.
#
class SearchRankTest < Test::Unit::TestCase
Keyword = 'Ruby'
ExpectingSite = 'www.ruby-lang.org'
def setup()
options = Selenium::WebDriver::Firefox::Options.new()
options.add_argument('-headless')
@driver = Selenium::WebDriver.for(:firefox, options: options)
@driver.manage.timeouts.implicit_wait = 5 # seconds
end
def teardown()
@driver.quit()
end
#
# Google の I'm Feeling Lucky をテストする.
#
def test_feeling_lucky_ruby()
@driver.get('http://google.com')
@driver.find_element(:id => 'lst-ib').send_keys(Keyword)
@driver.find_element(:xpath => '//*[@id="tsf"]/div[2]/div[3]/center/input[2]').click()
@driver.save_screenshot('./feeling-lucky-ruby.png')
assert_equal(ExpectingSite, URI.parse(@driver.current_url).host)
end
#
# DuckDuckGo をテストする.
#
def test_duckduckgo_ruby()
@driver.get('https://duckduckgo.com')
@driver.find_element(:id => 'search_form_input_homepage').send_keys(Keyword)
@driver.find_element(:id => 'search_button_homepage').click()
@driver.find_element(:id => 'r1-0').click()
@driver.save_screenshot('./duckduckgo-ruby.png')
assert_equal(ExpectingSite, URI.parse(@driver.current_url).host)
end
#
# Yahoo! Japan をテストする.
#
def test_yahoojapan_ruby()
@driver.get('https://www.yahoo.co.jp')
@driver.find_element(:id => 'srchtxt').send_keys(Keyword)
@driver.find_element(:id => 'srchbtn').click()
@driver.find_element(:xpath => '//*[@id="WS2m"]/div[1]/div[1]/h3/a').click()
@driver.save_screenshot('./yahoo-ruby.png')
assert_equal(ExpectingSite, URI.parse(@driver.current_url).host)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment