Search code examples
javascriptinputplaywright

Getting value of input element in Playwright


How do I return the value of elem so that I can verify that it is in fact 1?

const elem = await page.$('input#my-input')
await elem.fill('1')

Solution

  • Also, since v1.20 there's the toHaveValue matcher that can verify an element's value:

    await expect(page.locator('input#my-input')).toHaveValue('1');
    

    inputValue method has been added in Playwright v1.13.0

    await page.inputValue('input#my-input');
    

    Locator:

    await page.locator('input#my-input').inputValue();
    

    It returns input.value for the selected <input> or <textarea> element. Throws for non-input elements. Read more.

    Meanwhile, if you want to assert the value you can use toHaveValue

    const locator = page.locator('input#my-input');
    
    await expect(locator).toHaveValue(/[0-9]/);