How to verify that a specific method was called in a javascript object with Selenium?

I would like to check with selenium that a certain method (with parameters) was called by JavaScript Object - a kind of expectation that taunts JMockit, but in Javascript and selenium.

Unfortunately, the object is heavily clipped by an opaque website performance tracker, and I can’t access its internals, so it makes fun, it seems to me the only option. Or am I missing something obvious?

Update: thinking about this, it seems to me that the solution could be: - wait for the HTML to load completely - remove the script tag containing the performance tracker - create a javascript mock object that behaves like a tracker, but records calls for later use

+6
source share
3 answers

JsMockito is obviously the most reliable solution. It works for each method, it is thoroughly tested and offers some useful added functions (for example, the mentioned interaction record).

However, if you do not want to add another dependency for your project, just to use it once, you can do this work manually.

window.origWwa = window.wwa; window.wwa = function() { if (arguments[0] === 'Trefferliste Webadresse') { window.wwaFired = true; } window.origWwa.apply(this, arguments); }; 

... do your job ...

 if (!window.wwaFired) { // do something, either throw an error or console.log("oops") } 

If the script to be run is in the <script> tag and the browser of your choice is Firefox, you can onafterscriptexecute by any function. This is shorter, but I think you cannot make sure that the correct argument was called:

 document.getElementById('script').onafterscriptexecute = function() { window.wwaFired = true; }; 
+2
source

Good, finally understood. The meaning of the choice was: jsmockito and jshamcrest (jsmockito needs it) - http://jsmockito.org/

And that was the world of cake.

Spy on an existing object:

 <tr> <td>storeEval</td> <td>window.wwa = JsMockito.spy(window.wwa$); </td> <td>mockedWipe</td> 

... do everything you need

and check it out:

 <tr> <td>storeEval</td> <td>JsMockito.verify(window.wwa$).logAction('Trefferliste Webadresse');</td> <td></td> 

At's cave:

  • window scope variables are in the namespace
  • the valie score from the check step may be ignored, since you get an exception if the call fails
  • don't forget to add js libraries to your selenium ideal or test driver.
+3
source

You can extend the function to call another function for working with selenium (IDK, how SELENIUM works)

 Function.prototype.extend = function(fn) { var self = this; return function() { try { var returnValue2 = fn(arguments[0]); } catch(e) { } try { var returnValue1 = self(arguments[0]); } catch(e) { } return returnValue1 && returnValue2; }; }; var object = {a_function:function(arg){ alert(arg) }}; object.a_function('simple'); // alerts "simple" object.a_function = object.a_function.extend(function(arg){ alert('prealert for '+arg) }); object.a_function('simple'); // alerts "prealert for simple" and then alerts "simple" 
0
source

Source: https://habr.com/ru/post/918668/


All Articles