mocha+webdriverjs(+selenium)でテスト

2013-04-03T00:00:00+00:00 JavaScript Node.js

やってみた。テスト対象は以前に作ったJavaScriptなカレンダービューがあるブログシステム(独自)

※事前にselenium-server-standalone.jarをDLしといて起動しておく

var client = require("webdriverjs").remote({
  desiredCapabilities: {
    browserName: "chrome", // デフォルトはfirefoxな模様
    "chrome.binary": "/usr/bin/google-chrome" // chrome.binaryはデフォルトで/usr/bin/google-chromeな模様。場所が異なる場合は設定なりしておく
  },
  logLevel: "silent"
});

describe("アプリケーションのタイトルを取るテスト", function() {

  before(function() {
    client.init();
    client.url("http://localhost:5000");
  });

  after(function() {
    client.end();
  });

  it("タイトルは", function(done) {
    client.getTitle(function(title) {
      expect(title).to.be.equal("bloglious");

      done();
    });
  });

  it("今月のカレンダーの年月", function(done) {
    client.getText("td#calendar-label", function(label) {
      expect(label).to.have.property("value");
      expect(label.value).to.be.equal("2013/04");

      done();
    });
  });

  it("前月のカレンダーへ移動", function(done) {
    client.click("td#calendar-control-previous > a", function() {
      client.getText("td#calendar-label", function(label) {
        expect(label).to.have.property("value");
        expect(label.value).to.be.equal("2013/04"); // fail

        done();
      });
    });
  });
});

他にもsaveScreenshot(ファイル名, コールバック)っていうのがあって状況に応じてスクリーンショット等を取得したり出来る

今回はchromeで動かしているけど、Selenium自体がChromeを利用する際にはChromeDriverが必要になる。 https://code.google.com/p/chromedriver/downloads/list からダウンロードしてPATHの環境に沿ってコピーなり移動なりしておく。あとphantomjsも使う事も出来る

ちなみにSeleniumを使わずにzombie.jsを使ってやりたいっていう事案があるのならば http://architects.dzone.com/articles/functional-testing-nodejs が参考になるかも

HttpClientクラスで「java.io.IOException: Attempted read from closed stream」が出るケース MongoDB2.4で全文検索