您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
搭建mocha測試環(huán)境并使用selenium進行測試
作者:網絡轉載 發(fā)布時間:[ 2014/3/25 9:44:15 ] 推薦標簽:selenium 測試環(huán)境

  三、測試實例
  以下是用BDD寫的一個測試示例test.js,該測試的目的是打開google主頁,搜索mocha,并檢查搜索結果網頁的title是否含有mocha關鍵字。
1 var should = require('chai').should();
2 var webdriver = require('selenium-webdriver');
3 var By = webdriver.By;
4
5 var builder = new webdriver.Builder().usingServer("http://127.0.0.1:4444/wd/hub");
6 builder.withCapabilities({ browserName : "firefox" });
7 var driver = builder.build();
8
9 describe ('first test', function () {
10     beforeEach(function () {
11         driver.get('http://www.google.com');
12     });
13
14     afterEach(function () {
15         driver.close();
16         driver.quit();
17     });
18
19     it('should have correct title', function (done) {
20         driver.findElement(By.css('#lst-ib')).sendKeys('mocha');
21         driver.findElement(By.css('[name="btnK"]')).click();
22         driver.sleep(3000);
23         driver.getTitle().then(function (title) {
24             title.should.contain('mocha');
25             done();
26         });
27     });
28 });
  在命令行中切換到測試目錄,如本例中新建的mocha-test目錄,輸入以下命令以啟動測試:
  mocha -t 30000 -R spec test.js
  該命令中的"-t 30000"是設置一個測試用例(一個it函數表示一個測試用例)運行的時間不超過30秒,"-R spec"是設置測試結果的輸出格式。關于mocha運行測試的更多參數不再贅述。

上一頁12下一頁
軟件測試工具 | 聯系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd