1

As advised in How can I retrieve the current test's name within a Mocha test? a test title can be accessed in Mocha with this.test.title

However, in TS doing so results in the following error:

Property 'title' does not exist on type 'TestFunction'.ts(2339)

I have installed mocha types, but it didn't help:

npm install --save @types/mocha

I am new to TypeScript, so, I might be missing something. How do you solve this in your tests?

My test looks as follows:

import { Browser, Page } from 'playwright'
const playwright = require('playwright')
const myPage = require('../page-objects/mypage.ts')
const testData = require('../test-data/test-data.json')
let browser: Browser
let page: Page
let myPageCommands
const browserName = 'chromium'

describe('My Page Verification', () => {
    beforeEach(async () => {
        browser = await playwright[browserName].launch({ headless: false })
        page = await browser.newPage()
        myPageCommands = await new myPage(page)
        await myPageCommands .navigateTo('index')
    })

    afterEach(async () => {
        await browser.close()
    })

    it('Check Text Fields', async () => {
        await myPageCommands.fillTextField(testData)
        await myPageCommands.verifyTextField(testData)
        await console.log(this.test.title)
    })
})

1 Answers1

0

That is how it worked:

import { Context } from 'mocha';    

const context: Context = <any>this
        const test = context.currentTest || { state: 'undefined' };
        if (test.state === 'passed' && test.file) {
           
        }