13

I have a casperjs script which gives the desired result when I run on a linux server, but when I run the same from my laptop, it doesn't work.

How should I debug? Logs of the working one:

[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: http://caspertest.grsrv.com/, HTTP GET
[debug] [phantom] Navigation requested: url=http://caspertest.grsrv.com/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://caspertest.grsrv.com/my_app/"
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app/#/auth, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://caspertest.grsrv.com/my_app/#/auth"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/3 https://caspertest.grsrv.com/my_app/#/auth (HTTP 200)
[info] [remote] attempting to fetch form element from selector: 'form'
[debug] [remote] Set "null" field value to test
[debug] [remote] Set "null" field value to ****
[debug] [phantom] Capturing page to /home/grsrvadmin/gs/casper/ss.png
[info] [phantom] Capture saved to /home/grsrvadmin/gs/casper/ss.png
[debug] [phantom] Mouse event 'mousedown' on selector: input[id="loginButton"]
[debug] [phantom] Mouse event 'mouseup' on selector: input[id="loginButton"]
[debug] [phantom] Mouse event 'click' on selector: input[id="loginButton"]
[info] [phantom] Step anonymous 2/3: done in 1556ms.
[info] [phantom] Step _step 3/3 https://caspertest.grsrv.com/my_app/#/auth (HTTP 200)
[info] [phantom] Step _step 3/3: done in 1569ms.
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app/#/agreement/r8moskcfv7c80gpcd40fl12nmpf9e0nb, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://caspertest.grsrv.com/my_app/#/agreement/r8moskcfv7c80gpcd40fl12nmpf9e0nb"
[debug] [phantom] url changed to "https://caspertest.grsrv.com/my_app/#/agreement/r8moskcfv7c80gpcd40fl12nmpf9e0nb"
[info] [phantom] waitFor() finished in 217ms.
[info] [phantom] Step anonymous 4/4 https://caspertest.grsrv.com/my_app/#/agreement/r8moskcfv7c80gpcd40fl12nmpf9e0nb (HTTP 200)
[debug] [phantom] Mouse event 'mousedown' on selector: input[id="aggr_actionAccept"]
[debug] [phantom] Mouse event 'mouseup' on selector: input[id="aggr_actionAccept"]
[debug] [phantom] Mouse event 'click' on selector: input[id="aggr_actionAccept"]
[info] [phantom] Step anonymous 4/4: done in 1813ms.
[info] [phantom] Done 4 steps in 1826ms
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"

logs on windows machine:

[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: http://caspertest.grsrv.com/, HTTP GET
[debug] [phantom] Navigation requested: url=http://caspertest.grsrv.com/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://caspertest.grsrv.com/my_app/"
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app/#/auth, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://caspertest.grsrv.com/my_app/#/auth"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/3 https://caspertest.grsrv.com/my_app/#/auth (HTTP 200)
[info] [remote] attempting to fetch form element from selector: 'form'
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"

Script:

var casper = require('casper').create ({
  waitTimeout: 60000,
  stepTimeout: 60000,
  verbose: true,
  logLevel: "debug",
  viewportSize: {
    width: 1366,
    height: 768
  },
  pageSettings: {
    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0",
    "loadImages": true,
    "loadPlugins": true,
    "webSecurityEnabled": false,
    "ignoreSslErrors": true
  },
  onWaitTimeout: function() {
    casper.echo('Wait TimeOut Occured');
  },
  onStepTimeout: function() {
    casper.echo('Step TimeOut Occured');
  }
});

casper.start('http://caspertest.grsrv.com/', function() {
    this.fillSelectors('form', {
        'input[id="userName"]': 'test',
        'input[id="userPassword"]': 'test',
    }, false);
    this.capture('ss.png');
    this.click('input[id="loginButton"]')
});


casper.waitForSelector('#aggr_actionAccept', function() {
    this.click('input[id="aggr_actionAccept"]')
});

This is how I am executing:

casperjs --ignore-ssl-errors=true test.js

The resource is with a client and I am using a VPN on my windows machine to access the resource on my browser. The linux machine on which it is working is with the client itself

GrSrv
  • 551
  • 1
  • 4
  • 22
  • How about add `this.echo(this.getHTML()); this.capture('before_fill.png');` immediately before `this.fillSelectors` - maybe the page looks different or doesn't load as quickly from the windows box? – davejagoda Jan 02 '17 at 19:06
  • 1

    is the response of this.getHtML(). How can I check with what options casperjs is running? I want to know if it really ignores ssl errors or not. This is important to know because when I access with my browser it gives ssl certificate issue. Sorry for late reply.

    – GrSrv Jan 05 '17 at 15:51
  • 1
    Yes, I am using that option. I have mentioned it in my question. – GrSrv Jan 13 '17 at 12:23

1 Answers1

4

Add a resource.error event handler:
casper.on("resource.error", function(resourceError){
    console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
    console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
});

It seems, this is a known PhantomJS bug (Fixed in 2.5 beta).
You can download PhantomJS 2.5 beta from this page.

See also:
CasperJS/PhantomJS doesn't load https page
PhantomJS failing to open HTTPS site

You need to add the following callbacks, to catch all the errors:
casper
.on("error", function(msg){ this.echo("error: " + msg, "ERROR") })
.on("page.error", function(msg, trace){ this.echo("Page Error: " + msg, "ERROR") })
.on("remote.message", function(msg){ this.echo("Info: " + msg, "INFO") });
Community
  • 1
  • 1
  • 1
    As i can see from the log, something is going wrong at the time of submitting a form, it seems, CasperJS can't redirect you to the target page. –  Jan 11 '17 at 20:36
  • 1
    Hi, I ran with `--cookies-file` command-line option, no cookie file was generated. And why you asked to try twice? fillSelectors() is working fine on linux machine. Yes, it isn't getting redirected to the target page, and if it gets redirected the page doesn't load rendering just ``. – GrSrv Jan 12 '17 at 11:21
  • 1
    [Here](https://developer.amazon.com/login.html) i had a similar problem with auth, i just used `--cookies-file` option in that case, see [this issue](http://stackoverflow.com/questions/41391254/not-able-to-get-phantomjs-example-to-work/41396226#41396226) -There was redirecting into an empty page, like in your case. –  Jan 12 '17 at 18:50
  • 1
    Have you tried to get screenshot `ss.png` without submitting a form? –  Jan 12 '17 at 21:57
  • 1
    Have you tried to log in, by using usual browser, e.g. Google Chrome? –  Jan 12 '17 at 22:02
  • 2
    Yes, I have tried get screenshot without submitting a form, I get a blank page. So I printed the html, i got ``. On usual browser, I get a security error, which I ignore, and then I am able to login properly. – GrSrv Jan 13 '17 at 12:19
  • 2
    Earlier I was having waitTimeout and stepTimeout of 60 seconds, I later tried with 120 seconds as well. No gain. I added the callbacks that you mentioned, I did not get anything except : `error: CasperError: Errors encountered while filling form: form not found` – GrSrv Jan 13 '17 at 12:21
  • 1
    i've updated the answer, can't wait for your results! I hope, we can solve the problem! –  Jan 14 '17 at 05:05
  • 2
    Sorry, I couldn't check because it was a weekend. I'll update my findings in a few minutes. btw, I have already tried `--ssl-protocol=any --ignore-ssl-errors=true` – GrSrv Jan 16 '17 at 07:51
  • 2
    it still didn't work, but your event handler gave the reason `Error code: 6. Description: SSL handshake failed`; ideally, `--ssl-protocol=any` should solve it but doesn't for me. phantomjs --version :2.1.1, casperjs --version : 1.1.0-beta5 – GrSrv Jan 16 '17 at 11:04
  • 2
    It didn't work :(, still got `Error code: 6. Description: SSL handshake failed`. This is expected since `--ssl-protocol=any` didn't work. I really feel casper or phantom is ignoring this option. Thank you igor for being with me on this. :) – GrSrv Jan 16 '17 at 13:09
  • 1
    It seems, this is a [known PhantomJS bug](https://github.com/ariya/phantomjs/issues/12181#issuecomment-271987892) (Fixed in 2.5 beta). You can download PhantomJS 2.5 beta from [this page](https://groups.google.com/forum/m/#!topic/phantomjs/AefOuwkgBh0). –  Jan 16 '17 at 13:39
  • 2
    It works. Thank you so much. :) I had searched a lot, but this never come up in my search results. – GrSrv Jan 17 '17 at 10:09