21
16

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.

WebDriverをChromeのJavaScriptで試した

Posted at

環境:Windows7 64-bit

セットアップ

Selenium Serverの項の Download version 2.41.0のところ。

  • Selenium Serverを起動
go_server.cmd
java -jar selenium-server-standalone-2.41.0.jar -Dwebdriver.chrome.driver=chromedriver.exe

go_server.cmdをダブルクリックすると以下のログが出力された。

5 16, 2014 5:24:00 午後 org.openqa.grid.selenium.GridLauncher main
情報: Launching a standalone server
Setting system property webdriver.chrome.driver to chromedriver.exe
17:24:01.032 INFO - Java: Oracle Corporation 24.51-b03
17:24:01.033 INFO - OS: Windows 7 6.1 x86
17:24:01.039 INFO - v2.41.0, with Core v2.41.0. Built from revision 3192d8a
17:24:01.173 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
17:24:01.174 INFO - Version Jetty/5.1.x
17:24:01.175 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
17:24:01.176 INFO - Started HttpContext[/selenium-server,/selenium-server]
17:24:01.176 INFO - Started HttpContext[/,/]
17:24:01.239 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@18dafec
17:24:01.239 INFO - Started HttpContext[/wd,/wd]
17:24:01.242 INFO - Started SocketListener on 0.0.0.0:4444
17:24:01.243 INFO - Started org.openqa.jetty.jetty.Server@1713b42

「RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub 」のURLが
JavaScriptで書くテストコードの接続先。

  • NODE.jsをmsiでインストール http://nodejs.org/

  • Seleniumの公式JavaScriptバインディング、selenium-webdriverをnpmで取得する

> node --version
v0.10.28

> npm --version
1.4.9

> npm install mocha
 :
npm http 304 https://registry.npmjs.org/lru-cache
mocha@1.18.2 node_modules\mocha
├── debug@0.8.1
├── diff@1.0.7
├── growl@1.7.0
├── commander@2.0.0
├── mkdirp@0.3.5
├── jade@0.26.3 (commander@0.6.1, mkdirp@0.3.0)
└── glob@3.2.3 (inherits@2.0.1, graceful-fs@2.0.3, minimatch@0.2.14)

> npm install selenium-webdriver
npm http GET https://registry.npmjs.org/selenium-webdriver
npm http 304 https://registry.npmjs.org/selenium-webdriver
selenium-webdriver@2.41.0 node_modules\selenium-webdriver

試す

try.js

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
    usingServer('http://localhost:4444/wd/hub').
    withCapabilities(webdriver.Capabilities.chrome()).
    build();

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
  return driver.getTitle().then(function(title) {
    return title === 'webdriver - Google 検索';
  });
}, 1000);

driver.quit();

> node try.js

https://www.npmjs.org/package/selenium-webdriver
https://code.google.com/p/selenium/wiki/WebDriverJs には
ChromeならネイティブサポートがあるからSelenium Serverは不要ですとあるが、
すんなりいかなかったのでサーバーを使った。こいつ動くぞ。

21
16
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
21
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?