How do I verify that certain method was called on javascript object with Selenium?
assertion, javascript, mocking, selenium, testing
Solution
JsMockito is obviously the most robust solution there is. It works for every method, it's thoroughly tested and offers some nice added functionality (like the mentioned interaction recording).
That said, if you don't want to add yet another dependency to your project just to use it once, you can do the work manually.
window.origWwa = window.wwa;
window.wwa = function() {
if (arguments[0] === 'Trefferliste Webadresse') {
window.wwaFired = true;
}
window.origWwa.apply(this, arguments);
};
... do your work ...
if (!window.wwaFired) {
// do something, either throw an error or console.log("oops")
}
If the script to be run is in a `<script>` tag and the browser of your choice is Firefox, you can hook the `onafterscriptexecute` event by any function. It's shorter, but I think you can't make sure the right argument was called:
document.getElementById('script').onafterscriptexecute = function() {
window.wwaFired = true;
};
Problem
I would like to verify with selenium that certain method (with parameters) was called on JavaScript Object - kind of expectation mocking with JMockit, but in Javascript and selenium. Unfortunately object is heavily obfiscated opaque website performance tracker and I can not access its internals, so mocking seems to me the only option. Or do I miss something obvious? Update: after thinking about it, it seems to me that solution could be: - wait for HTML to load completely - remove certain script tag containing performance tracker - create javascript mock object behaving like tracker but recording invocations for later use