casperjs doesn't work as expected on windows machine
Asked Answered
M

1

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

Mazzard answered 1/1, 2017 at 6:15 Comment(3)
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?Pimp
<html><head></head><body></body></html> 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.Mazzard
Yes, I am using that option. I have mentioned it in my question.Mazzard
U
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") });
Unhappy answered 11/1, 2017 at 20:23 Comment(13)
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.Unhappy
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 <html><head></head><body></body></html>.Mazzard
Here i had a similar problem with auth, i just used --cookies-file option in that case, see this issue -There was redirecting into an empty page, like in your case.Unhappy
Have you tried to get screenshot ss.png without submitting a form?Unhappy
Have you tried to log in, by using usual browser, e.g. Google Chrome?Unhappy
Yes, I have tried get screenshot without submitting a form, I get a blank page. So I printed the html, i got <html><head></head><body></body></html>. On usual browser, I get a security error, which I ignore, and then I am able to login properly.Mazzard
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 foundMazzard
i've updated the answer, can't wait for your results! I hope, we can solve the problem!Unhappy
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=trueMazzard
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-beta5Mazzard
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. :)Mazzard
It seems, this is a known PhantomJS bug (Fixed in 2.5 beta). You can download PhantomJS 2.5 beta from this page.Unhappy
It works. Thank you so much. :) I had searched a lot, but this never come up in my search results.Mazzard

© 2022 - 2024 — McMap. All rights reserved.