I want to write unit tests to download a file by clicking a form button using casperJS. I tried using
TypeError: 'undefined' is not a function (evaluating 'inputs.click()')
The segment of the code that didn't pass the test :
var inputs = document.getElementsByTagName('input');
When I tried executing the same code from the console in my browser, the button got clicked and the download window popped up.
I initially tried to check with the presence of the button with
assertExists function in casperJS. The assertion passed, indicating that the button exists. Also, inputs array has 2 elements, one is hidden input of CSRF token (I'm using Django) and the other is the button that I want to click.
Best How To :
PhantomJS (and by extension CasperJS) has two contexts. Only the page context (
evaluate()) can access the DOM. The other problem is that most elements don't implement the
element.click() function in PhantomJS, so you would need to use another type of click.
CasperJS provides a
casper.click() function which works in almost all cases. It can select the element that you want to click either by CSS selectors or XPath expressions. Since you want to click the second input, it is not possible to define a general CSS selector that does this. An XPath expression must be used in this case. CasperJS provides an XPath utility:
var x = require('casper').selectXPath;
Please note that counting starts with 1 in XPath expressions and CSS selectors.
PhantomJS (and by extension CasperJS) won't trigger a file download when you click on something. You will need to build and send the request yourself.
There are many ways of doing it. Here is one example.