下載 Selenium Serverhttp://selenium-release.storage.googleapis.com/2.40/selenium-server-standalone-2.40.0.jar
啟動 Selenium Server
java -jar selenium-server-standalone-2.40.0.jar
CentOS 6.5
# yum install nodejs npm # npm -g install selenium-webdriver
預設node_modules安裝在 /usr/lib/node_modules/selenium-webdriver
Ubuntu 13.04
sudo apt-get install nodejs sudo apt-get install npm sudo npm -g install selenium-webdriver
預設node_modules安裝在 /usr/local/lib/node_modules/selenium-webdriver
不是用-g參數npm install selenium-webdriver 將會安裝在當前目錄下
遠程控制Chrome參考
https://code.google.com/p/selenium/wiki/ChromeDriver下載後解壓運行裡面的執行檔
var driver = new webdriver.Builder().usingServer('http://192.168.6.20:9515/').withCapabilities(webdriver.Capabilities.chrome()).build();
Firefox 使用
var driver = new webdriver.Builder().usingServer('http://192.168.6.20:4444/wd/hub').withCapabilities(webdriver.Capabilities.firefox()).build();
例 6.3. NODE.JS selenium-webdriver 本地測試
selenium-server-standalone 與 測試腳本在同一台機器上
var webdriver = require('selenium-webdriver'); var driver = new webdriver.Builder(). withCapabilities(webdriver.Capabilities.chrome()). build(); driver.get('http://www.google.com'); driver.findElement(webdriver.By.name('q')).sendKeys('webdriver'); driver.findElement(webdriver.By.name('btnK')).click(); driver.wait(function() { return driver.getTitle().then(function(title) { return title === 'webdriver - Google Search'; }); }, 1000); driver.quit();
例 6.4. NODE.JS selenium-webdriver 遠程測試
從一台測試機遙控另台selenium-server-standalone
var webdriver = require('/usr/local/lib/node_modules/selenium-webdriver'); var builder = new webdriver.Builder(). usingServer('http://192.168.6.20:4444/wd/hub'). withCapabilities(webdriver.Capabilities.firefox()); var driver = builder.build(); driver.get('http://www.google.com'); driver.findElement(webdriver.By.name('q')).sendKeys('webdriver'); driver.findElement(webdriver.By.name('btnK')).click(); driver.wait(function() { return driver.getTitle().then(function(title) { return title === 'webdriver - Google Search'; }); }, 1000); driver.quit();
例 6.5. Unit Test
var assert = require('assert'), fs = require('fs'); var webdriver = require('..'), test = require('../testing'), remote = require('../remote'); test.describe('Google Search', function() { var driver; test.before(function() { driver = new webdriver.Builder(). withCapabilities(webdriver.Capabilities.chrome()). build(); }); test.it('should append query to title', function() { 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 'webdriver - Google Search' === title; }); }, 1000); }); test.after(function() { driver.quit(); }); });
安裝 mocha
npm install -g mocha
測試腳本
var assert = require('assert'), test = require('selenium-webdriver/testing'), webdriver = require('selenium-webdriver'); test.describe('Google Search', function() { test.it('should work', function() { //var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build(); var driver = new webdriver.Builder().usingServer('http://192.168.6.20:4444/wd/hub').withCapabilities(webdriver.Capabilities.firefox()).build(); driver.get('http://www.google.com'); var searchBox = driver.findElement(webdriver.By.name('q')); searchBox.sendKeys('simple programmer'); searchBox.getAttribute('value').then(function(value) { assert.equal(value, 'simple programmer'); }); driver.quit(); }); });
運行測試程序
# mocha mocha.js 1 passing (10s)
測試成功返回1 passing (10s)