1

I am writing a simple test in playwright that checks if a located value falls within a specific percentage either way of a provided test value. The function returns a boolean. Something like this:

export const percentValueMatch = (expectedValue, testedValue, decimalPercentageOfExpectedValue = 0.3) => {
    const [numericalValue, numericalTestedValue] = [expectedValue, testedValue].map(
        value => Math.abs(parseFloat((typeof value === 'string' ? value : value.toString()).replaceAll(',', '')))
    )
    const percentageOfValue = numericalValue * decimalPercentageOfExpectedValue
    const lower = numericalValue - percentageOfValue
    const upper = numericalValue + percentageOfValue
    return lower <= numericalTestedValue && numericalTestedValue <= upper
}

It is trivial to write a function which then reduces the percentage to the smallest possible value, and therefore return exactly how close the located value falls close to the expected value. For instance:

test(`Value is ${modalOverviewValues[i]} ± 3% maximum`, async () => {
    const modalOverviewValueLocator = await screen.locator(example-locator).innerText()
    let percentage
    for (let j = 0.03; percentValueMatch(modalOverviewValues[i], modalOverviewValueLocator, j) && j > 0; j = j - 0.001) percentage = j
        expect(percentValueMatch(modalOverviewValues[i], modalOverviewValueLocator, percentage)).toEqual(true)
})

What I want is to return the finally determined minimal percentage value as a figure in the test title. However, I currently can't get the test titles to generate that way, since I assume the titles are produced before any calls to other functions and locators are processed.

Might there be a convenient way around this I am missing?

Thanks.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63

1 Answers1

1

Assuming the title comes from the testInfo class, then it can not be modified as it is readonly.

AJG
  • 476
  • 2
  • 5