一个人如何从mongoDB中获得值,以实时运行testcafe测试,以便在测试期间输入该值?我有一个装有我希望TestCafe在实时测试中使用的数据的杂货集合。我希望在testcafe文件中使用.toArray((,但我什至无法获得MongoDB连接。我在网上还没有找到解决方案。
我已经尝试遵循以下步骤:
- 在我的test.js(testcafe file(代码中添加了mongoclient。
- 在此代码中,我尝试仅显示"连接到数据库"。
- 我从未在终端或其他任何地方看到"连接到数据库"。
import { Selector } from 'testcafe';
const MongoClient = require('mongodb').MongoClient
import fs from 'fs';
import { ClientFunction } from 'testcafe';
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'my_database_name';
// keyword begins as an empty string and is supposed to be populated from DB
var keyword = [];
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
const findDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('my_collection_name');
// Find some documents
collection.find({'a': 3}).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs);
callback(docs);
keyword.unshift(docs);
});
};
client.close();
});
keyword = keyword[0]
fixture `example`
.page `https://www.google.com/`;
test('mongodb keyword to google search', async t => {
await t
.wait(1000)
.maximizeWindow()
.wait(1000)
.typeText(Selector('input[name="q"]'), keyword) //docs['keyword']
.wait(1000)
.presKey('enter')
.wait(5000);
});
因此,我尝试显示一个简单的Google搜索,该搜索试图将关键字从MongoDB集合中插入Google搜索框,然后按搜索按钮。这应该很容易:
- 运行测试
- 应该连接到MongoDB,从" exampe_collection"获取一个关键字
- 将关键字键入Google并按搜索。
相反,我得到此错误:
(node:1900) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use|
Running tests in:
- Chrome 74.0.3729 / Windows 10.0.0
example
× MongoDB keyword to google search
1) The "text" argument is expected to be a non-empty string, but it was undefined.
Browser: Chrome 74.0.3729 / Windows 10.0.0
64 |test('mongodb keyword to google search', async t => {
65 | await t
66 | .wait(1000)
67 | .maximizeWindow()
68 | .wait(1000)
> 69 | .typeText(Selector('input[name="q"]'), keyword) //docs['keyword']
70 | .wait(1000)
71 | .presKey('enter')
72 | .wait(5000);
73 |
74 |});
at typeText (C:UsersEricGoogle Drive!GIF
PROJECTJavaScriptNodeJSTestCafestackoverflow_example.js:69:7)
at test (C:UsersTestCafePROJECTJavaScriptNodeJSTestCafestackoverflow_example.js:64:1)
at markeredfn (C:UsersUserAppDataRoamingnpmnode_modulestestcafesrcapiwrap-test-function.js:17:28)
at <anonymous> (C:UsersUserAppDataRoamingnpmnode_modulestestcafesrcapiwrap-test-function.js:7:5)
at fn (C:UsersUserAppDataRoamingnpmnode_modulestestcafesrctest-runindex.js:240:19)
at TestRun._executeTestFn
(C:UsersUserAppDataRoamingnpmnode_modulestestcafesrctest-runindex.js:236:38)
at _executeTestFn (C:UsersUserAppDataRoamingnpmnode_modulestestcafesrctest-runindex.js:289:24)
请让我知道是否有解决方案。我只想将数据库数据(从mongoDB(传递到测试中,因此在测试过程中使用了(也需要从数据库中加载URL,也需要知道它(。
1/1失败(2s(
MongoClient.connect
是具有回调函数的异步API。在您访问测试中的keyword
值时,不能保证其完成。您可以将此功能包装在承诺中,并在测试中使用await
关键字从数据库中检索结果:
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'my_database_name';
function findDocs (db) {
return new Promise((resolve, reject) => {
const collection = db.collection('my_collection_name');
// Find some documents
collection.find({'a': 3}).toArray(function(err, docs) {
if (err)
return reject(err);
console.log("Found the following records");
console.log(docs);
resolve(docs);
});
});
}
function getClient () {
return new Promise((resolve, reject) => {
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
if (err)
return reject(err);
console.log("Connected successfully to server");
resolve(client);
});
});
}
async function getKeywords () {
const client = await getClient();
const db = client.db(dbName);
try {
return await getDocs(db);
}
finally {
client.close();
}
}
fixture `example`
.page `https://www.google.com/`;
test('mongodb keyword to google search', async t => {
const keyword = await getKeywords();
await t
.wait(1000)
.maximizeWindow()
.wait(1000)
.typeText(Selector('input[name="q"]'), keyword) //docs['keyword']
.wait(1000)
.presKey('enter')
.wait(5000);
});