lundi 7 décembre 2020

TestCafe - Obtaining Output After Test And Attach To Response

I have set up a few TestCafe tests within an Express.js environment. It is nice because I can send in the URL which I must test and it performs the test successfully.

However, I need to get the test results into the response. How do you do that?

Currently, I have set up the response to be a generic JSON message of {"message": "Tests started"}.

I want it to perform the test, then the test results will be in the response instead, like so:

{
    "button1": "clicked",
    "text_of_element": "Mr. Brown"
}

I noticed there exists reporters, but I am not sure how exactly to implement one so it puts together the results in the response of a request/response cycle.

Please if you know how to do this can you help me accomplish this?

My current Express controller code which executes the test (test is in a subfolder tests/gisTest.js):

// Express.js controller
exports.runTests = (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')
            .run()
        })
        .then(failedCount => {
            testcafe.close()
        })

        res.json({message: `Tests started`});
}

And the test:

// from tests/gisTest.js
import { ClientFunction, Selector } from 'testcafe';
require('dotenv').config()

const doc = process.env.PARAMS // this is expected to be a JSON string
const newDoc = JSON.parse(process.env.PARAMS)
console.log(`newDoc (from test)`, newDoc)
let dataToCheck = newDoc.datatocheck

console.log(`dataToCheck: `, dataToCheck)

fixture `Test Site`
    .page('https://mytestsite.com/');

test(`Interacting with test site`, async t => {
    const photoFieldForPaste = Selector('input#Ycyxxc')
    const searchButton = Selector('td#aoghAf > input')

await t
    .wait(1000)
    .click(photoFieldForPaste)
    .typeText(photoFieldForPaste, dataToCheck, {paste: true})
    .click(searchButton)
    
   // At this point I need it to say in the response the result of clicking the search button
    
})

I see a failedCount and I found I could put that into the response.

Unfortunately, I need more than failedCount.

Aucun commentaire:

Enregistrer un commentaire