Warm tip: This article is reproduced from serverfault.com, please click

TestCafe

发布于 2020-12-08 01:29:39

I would like to know if I have a context variable like t.ctx.data, is there a way to get that to write the value of t.ctx.data to the TestCafe JSON reporter (or any reporter)?

My code:

// 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}`});
}

My test code:

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;

})

Please see the part // how do I get t.ctx.myData into the reporter??.

I am assuming this is the only place where I could potentially get the data from the test into the reporter but I'm not sure exactly how.

If you know how to get the t.ctx.myData variable as shown in the above code to be written to the JSON reporter, I would highly appreciate it.

Even better would be to have a way to send the t.ctx.myData value into the response.

Questioner
rom
Viewed
0
Alex Kamaev 2020-12-08 16:20:00

At present, you can add only static metadata to tests and fixtures. This metadata is available in reports. Please refer to the following article to get details: https://devexpress.github.io/testcafe/documentation/guides/basic-guides/organize-tests.html#specify-test-metadata

As for sending dynamic data to the reporter, we keep this feature in mind, however we cannot give any estimates on this. Please track the following issue: https://github.com/DevExpress/testcafe/issues/3584