Skip to content

Instantly share code, notes, and snippets.

@TuHuynhVan
Created February 15, 2019 03:41
Show Gist options
  • Save TuHuynhVan/10fc2aaffd7bf7dec343273502595856 to your computer and use it in GitHub Desktop.
Save TuHuynhVan/10fc2aaffd7bf7dec343273502595856 to your computer and use it in GitHub Desktop.
WebdriverIO custom command to input value in fields those need to be focused first
/**
* From David Ridgley
* https://github.com/Arximiro
*/
// Define a custom command
browser.addCommand('fillInput', function (el, val) {
browser.waitUntil(function () {
el.click();
return el.isFocused();
}, 5000, 'Input Timed Out', 200)
el.setValue(val);
});
// Usage example
browser.fillInput(this.nameField, formData.name);
browser.fillInput(this.descriptionField, formData.description);
browser.fillInput(this.maxUsesField, formData.max_uses);
browser.fillInput(this.targetField, formData.target);
@djridgley
Copy link

djridgley commented Feb 15, 2019

Turns out this still doesn't fix the problem of inputs sometimes getting filled too quickly and causing text to get cut off or end up in the wrong input. I changed it to this for now. Will update if I find a better way.

		browser.addCommand('fillInput', function (el, val) {
			browser.pause(150);
			el.setValue(val);
			browser.pause(150);
		});

edit: The above gist should still work if your issue is just that you need an input to be focused before using setValue for whatever reason. My issue was inputs getting filled too quickly and sometimes some text would end up in the next input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment