1

I've been using PhantomCSS to write visual regression tests with screenshots for a website. I want to simulate a click with CasperJS on every a element on a page. I am able to retrieve every href and use open to visit each link, but I want to simulate a click event to have JavaScript interactions, etc. For some reason I haven't been able to find a way to do this.

I can get all the links on a page with:

  links = casper.evaluate(function(){
    var tags = __utils__.findAll('a');
    return Array.prototype.map.call(tags, function(elem){
      return elem.href;
    });
  });

But casper.click() receives a selector as a parameter, and I have very generic links across this site. Seems like a pretty trivial task but for some reason I haven't found a way to do it.

Fernando Briano
  • 7,699
  • 13
  • 58
  • 75
  • You can also reduce your snippet to `casper.getElementsAttribute('a', 'href')` because `getElementsAttribute` is new in casper version 1.1 – Artjom B. Jun 05 '14 at 21:21

1 Answers1

3

You can select a specific a element by the already collected href, but you should keep in mind how you want to structure your tests, because you need to begin with the web page for every link.

var url = "",   // some url
    links = []; // collected by your snippet
casper.eachThen(links, function(link){
    casper.thenOpen(url);
    casper.thenClick("a[href='"+link.data+"']"); // what you are looking for
    casper.then(function(){
        // your tests
    });
});

For this to work, I assume that the links are relatively unique: not all hrefs filled with javascript:void(0) or #. If this does not hold, you should clarify.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222