lundi 7 décembre 2020

TestCafe - Can You Pass ctx (Context) Variables to reporter?

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.

Aucun commentaire:

Enregistrer un commentaire