I'm writing a test to assert the default text value within an <input> tag. However, it's not playing ball:
Assert.assertThat(webDriver.findElement(By.id("inputTag")).getText(), Matchers.is("2"));
I'm writing a test to assert the default text value within an <input> tag. However, it's not playing ball:
Assert.assertThat(webDriver.findElement(By.id("inputTag")).getText(), Matchers.is("2"));
The most reliable solution I found, is executing a JavaScript script and return the value attribute of the HTMLInputElement from JavaScript.
I have tested this in several ways and the value was always correct, even after changing the input field value.
$input = $driver->findElement( WebDriverBy::id( 'inputTag' ) );
$value = $driver->executeScript( 'return arguments[0].value', $input );
// A custom PHP function that returns the value of an input field:
function getValue(RemoteWebDriver $driver, WebDriverBy $by ) {
return $driver->executeScript(
'return arguments[0].value',
$driver->findElement( $by )
);
}
// Sample usage:
$value = getValue( $driver, WebDriverBy::id( 'inputTag' ) );
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement inpElement = driver.findElement(By.id("inputTag"));
String text = (String) js.executeScript("return arguments[0].value", inpElement);;