我想知道是否有一个类似的上下文变量t.ctx.data
,是否有办法将其值写入t.ctx.data
TestCafe JSON报告程序(或任何报告程序)?
我的代码:
// Called within Express.js by a request coming from req
const testMySite = (req, res) => {
process.env.PARAMS = JSON.stringify(req.body)
let testcafe = null;
console.log(`Running test on ports 1341 and 1342`)
createTestCafe('localhost', 1341, 1342, void 0, true)
.then(tc => {
testcafe = tc;
const runner = testcafe.createRunner()
return runner
.src(`${path.dirname(__filename)}/tests/gisTest.js`)
.browsers('firefox:headless')
.reporter('json', 'report.json')
.run()
})
.then(failedCount => {
testcafe.close()
})
res.json({message: `Success! Scraper has begun to process ${req.body}`});
}
我的测试代码:
import { ClientFunction, Selector } from 'testcafe';
const doc = process.env.PARAMS
const newDoc = JSON.parse(process.env.PARAMS)
console.log(`newDoc (from test)`, newDoc)
// const _id = newDoc._id
let data = newDoc.mydata
fixture `My Fixture`
.page('https://www.mysite.co')
.afterEach(async t => {
await t
// how do I get t.ctx.myData into the reporter??
console.log(`t.ctx.myData: `, t.ctx.myData)
})
test(`My Test`, async t => {
const photoIcon = Selector('div#sbtc div.LM8x9c > span')
const photoFieldForPaste = Selector('input#Ycyxxc')
const searchByImageButton = Selector('td#aoghAf > input')
const targetElement = Selector('div#jHnbRc span:nth-child(2) > a')
await t
.wait(1000)
.click(photoIcon)
.typeText(photoFieldForPaste, data, {paste: true})
.click(searchByImageButton)
if(await targetElement.exists && await targetElement.visible) {
await t.ctx.finalData = targetElement.innerText;
}
await t.ctx.finalData = null;
})
请看这部分// how do I get t.ctx.myData into the reporter??
。
我假设这是唯一可以将测试中的数据导入报告器的地方,但我不确定具体如何。
如果你知道如何获取上述代码中所示的t.ctx.myData变量以将其写入JSON报告器,我将不胜感激。
更好的办法是将t.ctx.myData值发送到响应中。
目前,你只能向测试和固定装置添加静态元数据。该元数据在报告中可用。请参阅以下文章以获取详细信息:https : //devexpress.github.io/testcafe/documentation/guides/basic-guides/organize-tests.html#specify-test-metadata
对于向报表发送动态数据,我们牢记此功能,但是我们无法对此做出任何估算。请跟踪以下问题:https : //github.com/DevExpress/testcafe/issues/3584
谢谢,我最终使用.afterEach方法并将ctx值写入testResult.json(在使用JSON.stringify(t.ctx.myData)将ctx值转换为JSON之后。测试完成后,我使用了fs模块以读取文件并通过res.json(JSON.parse(data))调用将数据发送回去。