私はTDDメソッドを使用してWebプログラムを開発しています。したがって、テストはSelenium WebDriverライブラリを使用しています。しかし、私のプログラムが正しく動作しないのだろうか。以下はソースコードです。アサーション中にMochaとSeleniumのWebDriverエラーが発生しました
公共/ index.htmlを:
<h1 class="hello">Hello, world!</h1>
テスト/ test.js:
'use strict'
var assert = require('assert')
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until
var driver;
before(() => {
driver = new webdriver.Builder()
.forBrowser('chrome')
.build()
})
describe('Index page',() => {
before(() => {
driver.get('http://localhost:8080/')
})
it('should show hello greetings',() => {
let hello = driver.findElement(By.css('h1.hello'))
assert.equal(hello.getText(), 'Hello, world!')
})
})
package.json:
{
"name": "foobar",
"version": "1.0.0",
"description": "",
"main": "index.js",
"devDependencies": {
"mocha": "^2.5.3"
"selenium-webdriver": "^2.53.2"
},
"scripts": {
"start": "http-server public",
"test": "mocha"
},
"author": "",
"license": "ISC"
}
そして、私は以下のコマンドを実行しました。だから、
C:\Projects\foobar>npm install
私はnpm test
コマンドを使用して、すべてのテストを実行すると、それは常に実行したり、されなかったにかかわらず、npm start
コマンドに失敗しました。
C:\Projects\foobar>npm test
> [email protected] test C:\Projects\foobar
> mocha
Index page
1) should show hello greetings
0 passing (62ms)
1 failing
1) Index page should show hello greetings:
AssertionError: ManagedPromise {
flow_:
ControlFlow {
propagateUnhandledRejections_: true,
activeQueue_:
TaskQueue {
== 'Hello, world!'
at Context.<anonymous> (C:\Projects\foobar\test\test.js:22:16)
at callFn (C:\Projects\foobar\node_modules\mocha\lib\runnable.js:326:21)
at Test.Runnable.run (C:\Projects\foobar\node_modules\mocha\lib\runnable.js:319:7)
at Runner.runTest (C:\Projects\foobar\node_modules\mocha\lib\runner.js:422:10)
at C:\Projects\foobar\node_modules\mocha\lib\runner.js:528:12
at next (C:\Projects\foobar\node_modules\mocha\lib\runner.js:342:14)
at C:\Projects\foobar\node_modules\mocha\lib\runner.js:352:7
at next (C:\Projects\foobar\node_modules\mocha\lib\runner.js:284:14)
at Immediate._onImmediate(C:\Projects\foobar\node_modules\mocha\lib\runner.js:320:5)
npm ERR! Test failed. See above for more details.
私は主な問題はhello.getText()
スニペットだと思います。 getText()
メソッドは、hello要素のテキストを取得できませんでした。
'h1.hello'は有効なクエリではありません。 – cviejo
' findElement(By.css( 'h1.hello')) '。あなたの 'html'では' h1'は 'hello'という値を持つ' class'属性を持っていません。要素を見つけることができるので、これを 'html'サンプルに入れるのを忘れてしまったと思いますか? – RemcoW
@cviejo @RemcoWありがとうございます。しかし、 '
Hello、world!に変更すると!
'、違いはありません。 – signal