I'm trying to clear input field using different ways:
- clearValue() - doesn't work
- send some combination ok keys - doesn't work
I'm trying to clear input field using different ways:
I have the following code which worked for me
.setValue('field, [
browser.Keys.CONTROL,
"a",
browser.Keys.DELETE,
])
Have you tried removing the Input with the Webriver-Key commands?
In my Project i do it like this:
for (let i = 0; i < amountChars; i++) {
browser.keys(WEBDRIVER_KEYS['Backspace']);
}
WEBDRIVER_KEYS is part of the spectron-keys npm package.
I´ve made a custom command like this:
this.waitForElementVisible(fieldSelector)
.execute(
function (fieldSelector) {
const field = document.querySelector(fieldSelector);
field.value = null;
},
[fieldSelector]
);
And then in the test file i just call it like this:
browser.clearField(fieldSelector)
For me, this works like a charm:
browser.getValue('css selector', '#id', (textValue) => {
for(let i = 0; i < textValue.value.length; i ++) {
this.setValue('#id', '\u0008');
}
});
Had to come up with this kind of a solution, since any of the provided ones didn't work for me.
browser.clearValue('yourTargetFieldSelector')
So in case of using Page object:
yourPageObjectInstance.clearValue('@yourTargetFieldSelector')
I really suffered with this one today, so I share my solution since none of the other solutions worked for me for chrome & firefox:
exports.command = function (fieldSelector) {
const browserName = this.options.desiredCapabilities.browserName;
if (browserName === 'chrome') {
this.getValue(fieldSelector, function (result) {
this.setValue(fieldSelector, [...(result.value || '')].map(char => '\u0008'));
});
} else {
this.clearValue(fieldSelector);
}
return this;
};
This is a command to do one thing for chrome and other one for firefox