Simulate an input event in Nightwatchjs

Posted by : at

Category : nightwatchjs


Adding a value to an input box is fairly straightforward in Nightwatchjs, even though .setValue() doesn’t seem to be working at the moment.

.execute(function () { document.querySelector("#calculator > div > form > div:nth-child(2) > input").value = "300000" })

But the page I was testing depends on a Javascript event firing on input. Simply setting the value like this wasn’t enough. Although the form was completed correctly the javascript variable used for the eventual calculation remained the same.

The solution is to fire dispatchEvent on the element after setting the value

.execute(function()document.querySelector("#homewise-calculator > div > form > div:nth-child(2) > input").dispatchEvent(new Event('input'))})

Some helpful references

This answer on StackOverflow describes the dispatch even though it’s a different event type [How to trigger event in Javascript][https://stackoverflow.com/a/50587874/3066321]


About Tim Smith

There are some who call me ... Tim. Once a journalist of sorts and now a coder of sorts. I also love music and medieval poetry.

Star
Useful Links