一个人如何从MongoDB中获取一个值到实时运行的TestCafe测试中,以便在测试过程中将其键入?我有一个完整的MongoDB集合,我希望TestCafe在实时测试中使用这些数据。我希望在TestCafe文件中使用.toArray(),但看来它甚至无法获得MongoDB连接。我还没有找到在线解决方案。
我已经尝试按照以下步骤操作:
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 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搜索框,然后按搜索按钮。这应该很容易:
相反,我收到此错误:
(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:\Users\Eric\Google Drive\!GIF
PROJECT\JavaScript\NodeJS\TestCafe\stackoverflow_example.js:69:7)
at test (C:\Users\TestCafe\PROJECT\JavaScript\NodeJS\TestCafe\stackoverflow_example.js:64:1)
at markeredfn (C:\Users\User\AppData\Roaming\npm\node_modules\testcafe\src\api\wrap-test-function.js:17:28)
at <anonymous> (C:\Users\User\AppData\Roaming\npm\node_modules\testcafe\src\api\wrap-test-function.js:7:5)
at fn (C:\Users\User\AppData\Roaming\npm\node_modules\testcafe\src\test-run\index.js:240:19)
at TestRun._executeTestFn
(C:\Users\User\AppData\Roaming\npm\node_modules\testcafe\src\test-run\index.js:236:38)
at _executeTestFn (C:\Users\User\AppData\Roaming\npm\node_modules\testcafe\src\test-run\index.js:289:24)
请告诉我是否有解决方案。我只想将数据库数据(来自MongoDB)传递给TestCafe,以便在测试期间使用它(同样,如果我想从数据库中加载URL,也需要知道它)。
1/1失败(2s)
答案 0 :(得分:1)
MongoClient.connect
是带有回调函数的异步API。您在测试中访问keyword
值时,不能保证其完成。您可以将此函数包装在Promise中,并在测试中使用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);
});