Webdriverio 5 用于单元测试和功能测试



我有一个使用node和webpack编写的项目。

我正在为应用程序编写测试,由于我的应用程序的输出是可视化的,我使用了webdriverio 5,它运行得很好(功能测试(。但是,当我添加单元测试时,它只是失败,尽管在测试报告中它显示它是假的,日志表明它失败了。

这是我的package.json文件的样子

{
"name": "webdriverio-test",
"version": "1.0.0",
"description": "",
"main": "app/index.js",
"scripts": {
"install-selenium": "./node_modules/.bin/selenium-standalone install",
"start-selenium": "./node_modules/.bin/selenium-standalone start",
"test": "./node_modules/.bin/wdio wdio.conf.js",
"allure-report": "allure generate allure-results --clean && allure open"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@wdio/allure-reporter": "^5.12.1",
"@wdio/cli": "^5.12.5",
"@wdio/local-runner": "^5.12.5",
"@wdio/mocha-framework": "^5.12.1",
"@wdio/selenium-standalone-service": "^5.12.1",
"@wdio/sync": "^5.12.3"
}
}

这是我的测试文件的外观

const assert = require('assert'); 
const index = require('../../app/index').default 
describe('test browser', function () { 
it('should have the right title', function ()  { 
browser.url('https://webdriver.io'); 
const title = browser.getTitle(); 
//assert.strictEqual(title, 'WebdriverIO · Next-gen WebDriver test framework for Node.js'); 
console.log("**********I am here*************"); 
assert.equal(5,5); 
}); 
}); 

describe('test code', function () { 
it('should have do the right thing', function ()  { 
const c = index.add(3,4); 
assert.equal(c, 7); 
}); 
}); 

当我使用 webdriverio 命令运行此代码时,我得到以下堆栈跟踪

> webdriverio-test@1.0.0 test /Users/adsf/Desktop/test_scripts/webdriverio-test
> wdio wdio.conf.js

Execution of 1 spec files started at 2019-09-06T15:21:35.952Z
2019-09-06T15:21:35.979Z INFO @wdio/cli:Launcher: Run onPrepare hook
2019-09-06T15:21:36.093Z ERROR @wdio/cli:utils: A service failed in the 'onPrepare' hook
Error: Could not request headers from       https://chromedriver.storage.googleapis.com/2.43/chromedriver_mac64.zip: Error: read ECONNRESET
at Request.<anonymous> (/Users/adsf/Desktop/test_scripts/webdriverio-test/node_modules/selenium-standalone/lib/install.js:552:8)
at Object.onceWrapper (events.js:286:20)
at Request.emit (events.js:198:13)
at Request.EventEmitter.emit (domain.js:448:20)
at Request.onRequestError (/Users/adsf/Desktop/test_scripts/webdriverio-test/node_modules/request/request.js:881:8)
at ClientRequest.emit (events.js:198:13)
at ClientRequest.EventEmitter.emit (domain.js:448:20)
at TLSSocket.socketErrorListener (_http_client.js:392:9)
at TLSSocket.emit (events.js:198:13)
at TLSSocket.EventEmitter.emit (domain.js:448:20)
Continue...
2019-09-06T15:21:36.096Z INFO @wdio/local-runner: Start worker 0-0 with arg: wdio.conf.js
[0-0] RUNNING in chrome - /test/specs/basic.js
[0-0] 2019-09-06T15:21:36.381Z INFO @wdio/local-runner: Run worker command: run
[0-0] 2019-09-06T15:21:36.434Z INFO webdriver: [POST] http://127.0.0.1:4444/wd/hub/session
[0-0] 2019-09-06T15:21:36.434Z INFO webdriver: DATA { capabilities:
{ alwaysMatch: { browserName: 'chrome' }, firstMatch: [ {} ] },
desiredCapabilities: { browserName: 'chrome' } }
[0-0] 2019-09-06T15:21:37.385Z INFO webdriver: COMMAND navigateTo("https://webdriver.io/")
[0-0] 2019-09-06T15:21:37.386Z INFO webdriver: [POST] http://127.0.0.1:4444/wd/hub/session/3e34d38c50817313e3bab65cf8af5e51/url
[0-0] 2019-09-06T15:21:37.386Z INFO webdriver: DATA { url: 'https://webdriver.io/' }
[0-0] 2019-09-06T15:21:38.901Z INFO webdriver: COMMAND getTitle()
[0-0] 2019-09-06T15:21:38.901Z INFO webdriver: [GET] http://127.0.0.1:4444/wd/hub/session/3e34d38c50817313e3bab65cf8af5e51/title
[0-0] 2019-09-06T15:21:38.908Z INFO webdriver: RESULT WebdriverIO · Next-gen WebDriver test framework for Node.js
[0-0] **********I am here*************
[0-0] FAILED in chrome - /test/specs/basic.js
2019-09-06T15:21:39.335Z INFO @wdio/cli:Launcher: Run onComplete hook
Spec Files:      0 passed, 1 failed, 1 total (100% completed) in 00:00:03 
2019-09-06T15:21:39.336Z INFO @wdio/local-runner: Shutting down spawned worker
2019-09-06T15:21:39.590Z INFO @wdio/local-runner: Waiting for 0 to shut down gracefully
2019-09-06T15:21:39.590Z INFO @wdio/local-runner: shutting down
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! webdriverio-test@1.0.0 test: `wdio wdio.conf.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the webdriverio-test@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:

我的问题是我可以使用 wdio 运行单元测试和功能测试,还是使用 mocha 和 wdio 来运行这些测试是个好主意?

当我运行浏览器测试时,所有浏览器测试都运行良好

我相信在一个文件中运行不同类型的测试不是一个好主意(因为只有第一个描述可以工作 - 对于您的示例(,并且日志将根据 wdio 设置编写。我在不同的文件中运行了您的示例,它工作正常 - 2 通过 最好将 UI 测试移动到具有 Wdio 的 package.json 设置的同一文件夹,并使用 mocha 将单元测试移动到另一个单独的文件夹。

最新更新