LoginSignup
4
2

More than 5 years have passed since last update.

coffeeでseleniumする準備

Last updated at Posted at 2015-02-06

はじめに

自分がselenium勉強するために、
まずはselenium-webdriverをcoffeeでいじくり回すために書く備忘録。

selenium
http://www.seleniumhq.org/
selenium-webdriverのAPI docs
http://selenium.googlecode.com/git/docs/api/javascript/index.html

準備

  1. node.jsがインストールされてること
  2. coffeeがインストールされてること
    • npm install -g coffee-scriptしよう

selenium-webdriverをインストールしよう

適当にディレクトリをつくってそこに移動しよう

mkdir seleniumCoffee
cd seleniumCoffee

selenium-webdriverをインストールしよう

npm init
npm install selenium-webdriver

これでよし

coffeeでテストを書こう

定番のgoogle検索をしてみましょう

# firefox WebDriverを呼ぶやつ
firefox = require 'selenium-webdriver/firefox'
# エレメントを指定するのによく使うやつ
By = require 'selenium-webdriver'
    .By
# 画面描画や処理を待つのによく使うやつ
Until = require 'selenium-webdriver'
    .until

# firefox WebDriverでブラウザを立ち上げる
driver = new firefox.Driver()

# googleにアクセスする
driver.get 'https://google.co.jp'

# 検索ボックスにテキストを入力する
driver.findElement By.name 'q'
    .sendKeys 'selenium coffee'

# 検索ボタンを押す
driver.findElement By.name 'btnK'
    .click()

# タイトルに検索語が反映されるので、それを待つ
driver.wait Until.titleIs( 'selenium coffee - Google 検索' ), 1000

# ちょっと実行した時に一瞬過ぎて分からないし、練習なのでスリープ入れる
driver.sleep 2000

# ブラウザを閉じる
driver.quit()

こんな感じのコードになると思う。
もし動かない時は、btnKをbtnGにしたりするといいかもしれない。
タイミングの問題だと思う
jsにすると以下のようなかんじ。

var By, Until, driver, firefox;

firefox = require('selenium-webdriver/firefox');

By = require('selenium-webdriver').By;

Until = require('selenium-webdriver').until;

driver = new firefox.Driver();

driver.get('https://google.co.jp');

driver.findElement(By.name('q')).sendKeys('selenium coffee');

driver.findElement(By.name('btnK')).click();

driver.wait(Until.titleIs('selenium coffee - Google 検索'), 1000);

driver.sleep(2000);

driver.quit();

おわり

あとは、API docs見ながら色々遊ぼう

4
2
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
4
2