0

I try to create casperjs script to test my page. Actual algorithm looks that (1-5 works):

1) load url (login page)
2) take ss and save as img1.png
3) find inputs (login & pass)
4) fill inputs and click submit
5) take ss of new page and save as img2.png
6) change tab (page with two tabs - I want click on second tab)
7) do something

I am not 100% sure if code is ok so I paste it here:

casper.start(url, function(){});
casper.then(function() { this.capture('img1.png'); });

casper.thenEvaluate(function() {
    document.querySelector('input[name="user"]').setAttribute('value', 'jamal');
    document.querySelector('input[name="pass"]').setAttribute('value', 'asd');
    document.querySelector('input[name="login"]').click();
});
 casper.then(function() { this.capture('img2.png'); });

casper.thenEvaluate(function() {
    document.getElementById('keyword_form').getElementsByTagName('div')[0].getElementsByTagName('ul')[0].getElementsByTagName('li')[1].getElementsByTagName('a')[0].click();

    // this commented line works perfectly
    //document.getElementById('keyword_form').getElementsByTagName('div')[0].getElementsByTagName('ul')[0].getElementsByTagName('li')[1].getElementsByTagName('a')[0].style.background = "yellow"; 
});

casper.then(function() { 
    this.capture('img3.png');
});

On the last screen (img3) I see that tabs isn't changed. It's still default first tab. I really don't know why that happen. Any idea ?

Piotr Wu
  • 1,362
  • 3
  • 14
  • 31

1 Answers1

0

You actually have a problem in .click().

You need to

  1. either do this using jQuery where .click() is supported or
  2. follow the correct way - elem.onclick.apply(elem);

Read: How can I programmatically invoke an onclick() event from a anchor tag while keeping the ‘this’ reference in the onclick function?

Note: You can also inject the jquery.js while casperJS loads a page.

 casper = require('casper').create({
    clientScripts:  [
        'jquery.min.js' // injected in remote DOM on every request
    ],
    pageSettings: { ... }
});
Community
  • 1
  • 1
sudipto
  • 2,472
  • 1
  • 17
  • 21