0

I'm having a lot of trouble understanding how to test a simple form that makes an AJAX request and either loads a new page or displays an error, depending on the response.

The scope of 'this' is changing after the request. Once the form is submitted 'this' refers to the XHR result of the query, not to the page, so no title found of course. My code:

casper.test.begin('Tester.assertField(): unfilled inputs', 3, function(test) {

casper.start('http://www.myste.com/roku/', function() {
    this.fill('form[id="id-lookup-form"]', {
        'code':       'chuck@norris.com'
    }, true);
    test.assertField('code', 'chuck@norris.com', 'Tester.assertField() works as expected with inputs');
});

casper.wait(5000, function() {
    test.assertField(this.title, 'myste.com', 'Title correct after form input');
});

casper.run(function() {
    test.done();
}); 
});

I can increase the wait as long as I like and it will still fail. I know it is remedial, forgive me, but I've been all over trying to find a solution. This is similar: Update whole page on Ajax request and maybe I'm out to lunch, but since the page is making an AJAX request, I don't want to have to duplicate the request-- I want to let the page make the request and then see what happens. We do a lot of AJAX requests like this, I don't want to be intercepting/rewriting/parsing them all for every page I need to test. Casperjs, how to only proceed after receiving response from an ajax call seems to also suggest this method (though perhaps function getSomethingFromMyServerViaAjax() is not sending the request, i can't tell.)

Thanks very much, this is kicking me.

Community
  • 1
  • 1
Rgrove
  • 31
  • 5
  • (1) Have you taken a screenshot to see what page you're on? It seems to me, this code should work as expected and only PhantomJS has some problem. (2) Which PhantomJS version do you use? (3) Please register to the `resource.error`, `page.error`, `remote.message` and `casper.page.onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf)). Maybe there are errors. – Artjom B. Aug 31 '15 at 19:49
  • I have taken a screenshot after the call is made, it returns an image of my JSON (don't know how to upload it here.) I'm using Phantom 1.9.8. Thanks very much, I'll register those handlers-- I'm working with the test module right now, so it tells me "Fatal: you can't override the preconfigured casper instance in a test environment." I'll get those working. Thanks very much for the response. – Rgrove Aug 31 '15 at 20:24
  • OK, registered the error messaging. Test runs and fails in the same way, and the message is: Console: ResourceError: { "errorCode": 5, "errorString": "Operation canceled", "id": 84, "url": "http://static.ak.facebook.com/connect/xd_arbiter/44OwK74u0Ie.js?version=41#cb=f3f8d24634&domain=www.cbs.com&origin=http://www.cbs.com/f7b8b39d&relation=parent&error=unknown_user" } This is a call being made elsewhere on the page, it doesn't hold up the test or page render. – Rgrove Aug 31 '15 at 20:31
  • It seems that PhantomJS doesn't understand that the JSON response is not an actual page. I have no idea why that is. A shot in the dark: run CasperJS with the `--web-security=false` commandline option (`casperjs test --web-security=false script.js`). You could try to update to PhantomJS 2.0.0, but then you will need to install CasperJS from git, because there is currently no release that supports PhantomJS 2. – Artjom B. Aug 31 '15 at 20:40
  • Thanks. Adding that commandline option doesn't change my result. I'm surprised as this scenario should be a basic test. In one of the posts I looked at- that you solved!- http://stackoverflow.com/questions/19779610/casperjs-how-to-only-proceed-after-receiving-response-from-an-ajax-call - there is a 'getSomethingFromMyServerViaAjax' function. I feel like that answer *might* work for me. But it seems that function wants to use Casper to make the AJAX call (I think- can't actually see it.) I would rather let the page make the call and see what happens. That makes more sense to me. – Rgrove Aug 31 '15 at 20:58
  • Well, that linked question is very strange. I have no idea what why the OP wanted to do that and I don't think it is a good solution in your case, because you would have to make that request yourself and somehow trigger everything else that happens after the response came back. – Artjom B. Aug 31 '15 at 21:06
  • Agreed, and glad to hear you say so. But again, I feel like my test scenario is a very basic one: what happens on the page after an AJAX request. Yet I'm not seeing much on this. I know this is an open ended question, but isn't this doable with Casper? Have you done it? Seen it done? Sorry, I'm just confounded. Thanks again for all the help, much appreciated. – Rgrove Aug 31 '15 at 21:35
  • This is probably some kind of bug in PhantomJS. I can't really help you here. – Artjom B. Aug 31 '15 at 21:38
  • I think I may be onto something with this problem. I'm seeing this error: "Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/local/Cellar/casperjs/1.1-beta3/libexec/bin/bootstrap.js. Domains, protocols and ports must match." Shouldn't that 'file:///' path just be '/' ? I get that message even when I just type 'casperjs' into the command line, it pops up with every casper command. I'm reinstalling casper now from git, to see if that will help. I'm thinking I must have something configured wrong. – Rgrove Sep 03 '15 at 20:40
  • It just means that you're using PhantomJS 1.9.8 which has a bug that prints some additional stuff when it exits. It's nothing to worry about. It's only polluting the log. – Artjom B. Sep 03 '15 at 20:42

0 Answers0