Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I verify that certain method was called on javascript object with Selenium?

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

like image 435
Konstantin Pribluda Avatar asked Jun 21 '12 14:06

Konstantin Pribluda


1 Answers

Ok, finally got it. Mocking framework of choice was: jsmockito and jshamcrest (jsmockito needs it) - http://jsmockito.org/

And it was peace of cake.

Spy on existing object:

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

... do whatever necessary

and verify it:

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

Cave at's:

  • window scoped variables are in namespace window
  • evaluation valie from verification step can be ignored, as you get an exception if call is not satisfied
  • do not forget to add js libraries to your selenium ide or test driver
like image 144
Konstantin Pribluda Avatar answered Oct 21 '22 16:10

Konstantin Pribluda