有什么方法可以在 TestCafe 中使用 MongoDB 吗?
Is there any way to use MongoDB with TestCafe?
如何从 MongoDB 中获取值到实时 运行 TestCafe 测试中,以便它在测试期间输入该值?我有一个 MongoDB collection 充满数据的数据,我希望 TestCafe 在实时测试期间使用这些数据。我希望在 TestCafe 文件中使用 .toArray() 但我什至无法获得 MongoDB 连接。我还没有在网上找到解决方案。
我已经尝试按照以下步骤操作:
- 将 MongoClient 添加到我的 test.js(TestCafe 文件)代码中。
- 在此代码中,我试图只显示 "connected to database"。
- 我从未在航站楼或其他任何地方看到 "connected to database"。
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 collection 中的关键字插入 Google 搜索框并按下搜索按钮。这应该很简单:
- 运行 测试
- 假设连接到 MongoDB,从 "example_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:\Users\Eric\Google Drive\!GIF
PROJECT\JavaScript\NodeJS\TestCafe\Whosebug_example.js:69:7)
at test (C:\Users\TestCafe\PROJECT\JavaScript\NodeJS\TestCafe\Whosebug_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 失败(2 秒)
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);
});
如何从 MongoDB 中获取值到实时 运行 TestCafe 测试中,以便它在测试期间输入该值?我有一个 MongoDB collection 充满数据的数据,我希望 TestCafe 在实时测试期间使用这些数据。我希望在 TestCafe 文件中使用 .toArray() 但我什至无法获得 MongoDB 连接。我还没有在网上找到解决方案。
我已经尝试按照以下步骤操作:
- 将 MongoClient 添加到我的 test.js(TestCafe 文件)代码中。
- 在此代码中,我试图只显示 "connected to database"。
- 我从未在航站楼或其他任何地方看到 "connected to database"。
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 collection 中的关键字插入 Google 搜索框并按下搜索按钮。这应该很简单:
- 运行 测试
- 假设连接到 MongoDB,从 "example_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:\Users\Eric\Google Drive\!GIF
PROJECT\JavaScript\NodeJS\TestCafe\Whosebug_example.js:69:7)
at test (C:\Users\TestCafe\PROJECT\JavaScript\NodeJS\TestCafe\Whosebug_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 失败(2 秒)
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);
});