Jul 27, 2023
inputValue() and textContent() difference and when to use them in Playwright Java.
inputValue() is used to retrieve the value of input elements that you just typed.
textContent() is a method that is used to get the visible text content of an element on a web page. it can be a header tag. This method is useful for web scraping and verifying the content of a page.
Here is a code example
page.navigate("https://the-internet.herokuapp.com/forgot_password");
Locator inputTextField=page.locator("//input[@id='email']");
// Get input text of an element using inputValue();
String actualInputVal=inputTextField.inputValue();
System.out.println("actualInputVal=="+actualInputVal);
// Assert the value of it using hasValue
assertThat(inputTextField).hasValue("My input");
Locator forgetPasswordHeader=page.locator("//h2[text()='Forgot Password']");
String forgetPasswordHeaderActualText=forgetPasswordHeader.textContent();
System.out.println("forgetPasswordHeaderActualText=="+forgetPasswordHeaderActualText);
// you can't use hasValue here since it's not an input element so use hasText
assertThat(forgetPasswordHeader).hasText("Forgot Password");
This will print
actualInputVal==My input
forgetPasswordHeaderActualText==Forgot Password