3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

matzの写真を5秒間表示するサンプル【Selenium入門】

Posted at

Seleniumとは

Selenium(セレニウム)はブラウザのオートメーションツール
自動でブラウザを操作することでWebサイトの動作テストを行うことができる

TL;DR

matzの顔写真を5秒間表示するサンプル

aaa.gif

環境

terminal
Mac OS Sieera 10.12.6

$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin14]

Install

terminal
$ brew install chromedriver
$ gem install selenium-webdriver 

matzの顔写真を5秒間表示するサンプル

test.rb
require "selenium-webdriver"

# Chrome用のドライバを指定
driver = Selenium::WebDriver.for :chrome

# Googleにアクセス
driver.get "http://google.com"

# name属性にqが設定されている要素を取得
element = driver.find_element(:name, 'q')

# 検索したい文字列を入力
element.send_keys("ruby wiki")

# name属性にbtnGが設定されている要素を取得
element = driver.find_element(:name, 'btnK')

# 取得した要素をクリック
element.click

# aタグのテキストを指定して要素を取得、クリック
driver.find_element(:link_text => "Ruby - Wikipedia - ウィキペディア").click

# wikiの検索窓に値を指定して検索
driver.find_element(:id, 'searchInput').send_keys("まつもとゆきひろ")
driver.find_element(:id, 'searchButton').click

# mazの画像をクリック
driver.find_element(:class, 'image').click

# sleep
sleep 5

# 終了
driver.quit

所感

ページのテストやダミーデータの登録処理の自動化にかなり使えそう!
実行状況が可視化されるのは良いですね

チートシート作ったので是非!
http://qiita.com/wMETAw/items/8ea0ff9c7a63ca6134b9

リンク

seleniumメソッド一覧(公式サイト)
Webブラウザの自動操作 (Selenium with Rubyの実例集)
Selenium の基本的な使い方のサンプル集
http://qiita.com/edo_m18/items/ba7d8a95818e9c0552d9
http://qiita.com/adebadayo/items/22134c00123663bc70b4

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?