Search code examples
typescriptplaywrightautofill

Check autofilled value with Playwright


I am testing with Playwright and faced one problem.

On web I've a form with 4 fields. When 2 of them are filled, in third one appears calculated value. Expect.soft returns on this new appeared value received string: ""

As example I've this part of code:

await page.getByLabel('Buy up price').fill(buyUp); // fill Buy up price
await page.getByLabel('Percent of sale price').fill(percent); // fill Percent of sale price
if (percent != '') {
    let salePrice = ((+buyUp) * (+percent)/100 + (+buyUp)).toFixed(8);
    const predictPrice = page.locator('//*[@id="fvf_salePrice"]');
    await expect.soft(predictPrice).toHaveText(salePrice, {timeout: 5000});
} else {
    await page.getByLabel('Sale price', { exact: true }).fill(salePrice); // fill Sale price
} 

In debug mode I see value on selector //*[@id="fvf_salePrice"] exists, but expect.soft part allways fails, it returns: expected string: salePrice received string: "". I see that playwright catch placeholder, where value displyed, but can't get right value. Actually I've used all types of selectors (Xpath, CSS, etc.), used while loop to wait until data renew, but nothing helped. Testing is performed on chromium

Is it because of dinamically changing data, or something wrong in code?


Solution

  • Thanks to all, I've found a solution. As it was an input field, toHaveText() must be replaced with toHaveValue(). So await expect.soft(predictPrice).toHaveText(salePrice, {timeout: 5000}); must be replaced with expect.soft(predictPrice).toHaveValue(salePrice, {timeout: 5000});