gpt4 book ai didi

mongodb - 有没有办法将 MongoDB 与 TestCafe 一起使用?

转载 作者:行者123 更新时间:2023-11-28 21:17:09 25 4
gpt4 key购买 nike

如何从 MongoDB 获取值到实时运行的 TestCafe 测试中,以便它在测试期间输入该值?我有一个充满数据的 MongoDB 集合,我希望 TestCafe 在实时测试期间使用这些数据。我希望在 TestCafe 文件中使用 .toArray() 但我什至无法获得它似乎的 MongoDB 连接。我还没有在网上找到解决方案。

我已经尝试按照以下步骤操作:

  1. 将 MongoClient 添加到我的 test.js(TestCafe 文件)代码中。
  2. 在此代码中,我试图只显示“已连接到数据库”。
  3. 我从未在终端或其他任何地方看到“连接到数据库”。
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);

});


因此,我尝试展示一个简单的谷歌搜索,尝试将我的 MongoDB 集合中的关键字插入谷歌搜索框并按下搜索按钮。这应该很简单:

  1. 运行测试
  2. 假设要连接到 MongoDB,从“example_collection”中获取单个关键字
  3. 将该关键字输入 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 失败(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 - 有没有办法将 MongoDB 与 TestCafe 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56016619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com