31

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')
JakeDK
  • 966
  • 1
  • 9
  • 18

5 Answers5

50

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]/);
Yevhen Laichenkov
  • 7,746
  • 2
  • 27
  • 33
10

The easiest way is to use $eval. Here you see a small example:

const playwright = require("playwright");

(async () => {
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.setContent(`<input id="foo"/>`);
  await page.type("#foo", "New value")
  console.log(await page.$eval("#foo", el => el.value))
  await page.screenshot({ path: `example.png` });
  await browser.close();
})();
Max Schmitt
  • 2,529
  • 1
  • 12
  • 26
  • 1
    Thanks Max. I'm looking for an easy way to check the value of an HTML element handle. Is this possible? – JakeDK May 28 '20 at 12:49
  • 1
    Note for others: The essential code you're looking for is `await page.$eval("#foo", el => el.value)`. – rinogo Mar 12 '21 at 01:58
  • 1
    Note also that [`$eval()` doesn't perform actionability checks](https://playwright.dev/docs/actionability) (it doesn't autowait), so you might need to precede your `$eval()` with something like `await page.waitForSelector("#foo")` – rinogo Mar 12 '21 at 01:59
  • 2
    in case of having issues with type, you can set: `el as HTMLInputElement;` – André Kelling Jun 04 '21 at 09:23
6

From version 1.19 (and probably lower versions) Element Handler is not recomended. Instead of it use Locator.

page.locator(selector).innerText()

in your case with assertion it will be

expect(page.locator("input#my-input").innerText().includes("1")).toBeTruthy()

Read more on: https://playwright.dev/docs/api/class-elementhandle#element-handle-fill

Gaj Julije
  • 1,538
  • 15
  • 32
2

How to verify value from textbox?

In latest playwright version, toHaveValue combines getting and verifying input value in single step.

const locator = page.locator('input[type=number]');
await expect(locator).toHaveValue('1');

It ensures the Locator points to an element with the given input value. You can use regular expressions for the value as well.

Reference: https://playwright.dev/docs/api/class-locatorassertions#locator-assertions-to-have-value

Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23
1

This is to get the value through chrome context ( like executing in chrome devtools console ) . . As per the latest document it can be achieved using page.evaluate . https://playwright.dev/docs/evaluating .

Example

.

const linkhrefFromBrowser = await page.evaluate(async () => {
    // The below instructions will run in the browser console ( context )
    let element = element.getElementById("pagelink");
    let linkhref = element.getAttribute("href");
    console.log(linkhref); // will be printed in browser tab
    return linkhref; // whatever you return will be available in linkhrefFromBrowser variable
  });
Siva Kannan
  • 2,237
  • 4
  • 27
  • 39