0

I need browse one page, retrieve data from it and send data to my service via HTTP requests. As far as I know, CasperJS/PhantomJS can work simultaneously with only one web resource. How to bypass this limitation?

UPD: I need make request from CasperJS/PhantomJS side, not from page context through evaluate().

Interloper
  • 790
  • 7
  • 20
  • possible duplicate of [Getting remote data for a page using AJAX in CasperJS](http://stackoverflow.com/questions/13301031/getting-remote-data-for-a-page-using-ajax-in-casperjs) – Artjom B. Jul 02 '15 at 06:10
  • @Artjom B. : I do not want to use AJAX. – Interloper Jul 02 '15 at 06:54
  • Why do you have that constraint? AJAX requests are requests as any other. If you don't want that, then you will need to open that page. – Artjom B. Jul 02 '15 at 07:01
  • I had to test a web chat app and ran into this limitation as well: casperjs can't process two windows simultaneously. Phantomjs *can* do it but the API is clunky. So I searched around on npm and github and found horseman.js. Unlike casper, horseman is synchronous and therefore it's possible to have two web pages open at the same time. On top of that horseman.js supports browser tabs if you don't want to open a separate "window" to manipulate another page. – slebetman Jul 02 '15 at 07:21
  • @slebetman CasperJS is perfectly capable of opening [another tab](http://stackoverflow.com/a/24883847/1816580). It's just a little clunky and doesn't work in the test environment. – Artjom B. Jul 02 '15 at 07:32
  • @ArtjomB. That's opening another window. Which horseman.js can do as well but in addition, horesman.js can open actual "tabs" and can tab-switch. This is because the underlying phantom.js windows have tabs. – slebetman Jul 02 '15 at 08:25
  • @slebetman PhantomJS has no UI, so there is no distinction between windows and tabs. Everything is a page. Pages may have subpages such as popups. – Artjom B. Jul 02 '15 at 08:59
  • I am wondering whether it is possible to work simultaneously with two windows. If it is not, I will use AJAX. – Interloper Jul 02 '15 at 09:18
  • @ArtjomB. There is a distinction when you want to test weather your new window will show up as a new tab or actually pop up a new window. There is also a distinction in the PhantomJS API which horseman.js exposes but casper.js does not. – slebetman Jul 02 '15 at 14:22

1 Answers1

0

@Interloper - To be able to perform this integration, you need two basic requirements:

1 - Your page will be requested, must be habilidada to receive information via HTTP access control (CORS). This will greatly depend on the type of your application! For example, if you are using C # in this project, you'll have to add the following code in your web.config in system.webServer:

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
    </customHeaders>
</httpProtocol>

With this, your project / page will be enabled so that you can make access / exchange of information from any other domain ...

2 - So you can call this URL from any domain, and in our case, a code CasperJS, you can perform a HTTP call using AJAX (jQuery / JS), with the following example:

function get_response(output) {
    $.ajax({
        type: "POST",
        url: "URL",
        dataType: "text",
        async: false,
        data: { param1:value1 },
        success: function (response) {
            var $doc = $.parseXML(response);
            output($($doc).find('TagsXML').find('TAG_RESPONSE1')[0].textContent + '|' + $($doc).find('TagsXML').find('TAG_RESPONSE2')[0].textContent);
        },
        error: function (response) {
            output('');
        }
    });
}

get_response(function (output) {
    test.pass('output: ' + output);
    var VALUE1 = output.split('|')[0];
    var VALUE2 = output.split('|')[1].replace(/\n/g, "");
});

With this code, you can you perform the "remote access" and the feedback information (if necessary)

Diego Borges
  • 342
  • 3
  • 7