标签: casperjs

Casper JS waitForResource带有一个安静的API

我们在使用casper.js进行功能测试时遇到了一些问题.

我们两次请求相同的资源,首先使用GET,然后使用POST方法.现在,当等待第二个资源(POST)时,它与第一个资源匹配,并直接转到"then"函数.

我们希望能够在"test"函数中检查HTTP方法,这样我们就可以正确地识别资源.现在我们使用状态代码(res.status),但这并没有完全解决我们的问题,我们真的需要http方法.

// create new email
this.click(xPath('//div[@id="tab-content"]//a[@class="button create"]'));

// GET
this.waitForResource('/some/resource', 
    function then() {
        this.test.assertExists(xPath('//form[@id="email_edit_form"]'), 'Email edit form is there');

        this.fill('form#email_edit_form', {
            'email_entity[email]': 'test.bruce@im.com',
            'email_entity[isMain]': 1
        }, true);

        // POST
        this.waitForResource(
            function test(res) {
                return res.url.search('/some/resource') !== -1 && res.status === 201;
            },
            function then() {
                this.test.assert(true, 'Email creation worked.');
            },
            function timeout() {
                this.test.fail('Email creation did not work.');
            }
        );
    },
    function timeout() {
        this.test.fail('Email adress creation form has not been loaded');
    });
Run Code Online (Sandbox Code Playgroud)

或者也许有更好的方法来测试这种情况?虽然这是一个功能测试,但我们需要在一次测试中保留所有这些步骤.

javascript functional-testing casperjs

5
推荐指数
1
解决办法
2452
查看次数

运行phantomjs casperjs时手动输入

在进入下一步之前,是否可以使脚本(phantomjs或casperjs)停止进行手动人工输入(键盘输入)?

例如,脚本将填写表单并将整个屏幕导出为.png,然后在单击提交之前等待用户输入填写最后一个字段.验证码是此要求的障碍之一.我不想处理Captcha破坏者/解算器或类似的事情,因为每次运行只有一次.

有帮助吗?

javascript automated-tests web-scraping phantomjs casperjs

5
推荐指数
1
解决办法
2206
查看次数

CasperJS远程调试选项

我正在尝试为casperJS使用远程调试选项.

我在端口上运行它6666.

$casperjs --remote-debugger-port=6666 myscript.js
Run Code Online (Sandbox Code Playgroud)

调试器启动,我可以访问url上的WebKit检查器

http://localhost:6666/ 
Run Code Online (Sandbox Code Playgroud)

我看到第一个URL为about:blank.

当我点击它时,网页检查器加载.

当我输入_run()控制台时,我得到了

引用错误,_run未定义错误.

我无法继续前进.如果我需要在某处进行更正,请告诉我.

另外,我是否必须在CasperJS脚本中设置显式断点.如果必须,那怎么样?

更新1

尝试--remote-debugger-autorun = yes

$casperjs --remote-debugger-port=6666 --remote-debugger-autorun=yes myscript.js
Run Code Online (Sandbox Code Playgroud)

这会立即按照帮助中的说明运行脚本.但是,调试器仍然运行,并且进程不会退出.

OUTPUT

现在我可以在下面看到请求的URL:空白.

当我点击about:blank或request url时,WebKit检查器会打开.

但我再次陷入困境.

更新2

还试图执行用phantomJS移植的示例.

结果是一样的.我被困在一个空白的WebKit Inspector窗口.

remote-debugging phantomjs casperjs

5
推荐指数
2
解决办法
2848
查看次数

从cron运行CasperJS

所以,我试图在我的服务器上运行casperJS作为cron作业,这是crontab:

* * * * * /usr/local/bin/casperjs /var/www/javascript/uat/prime.sh 2>&1
Run Code Online (Sandbox Code Playgroud)

这是prime.sh

#!/bin/bash

export PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs
/usr/local/bin/casperjs /var/www/javascript/uat/prime.js 2>&1
Run Code Online (Sandbox Code Playgroud)

我还将导出添加到用户的.bash_profile中,但是cron通过电子邮件发送给我

Fatal: [Errno 2] No such file or directory; did you install phantomjs?
Run Code Online (Sandbox Code Playgroud)

不知道还能做什么!有什么建议?

casperjs

5
推荐指数
1
解决办法
3028
查看次数

如何在casperjs中循环

我试图点击"下一个"按钮N次并每次抓取页面源.我知道我可以在远程网站上运行任意函数,所以我只使用远程函数nextPage()而不是click()我如何运行以下任意次:

var casper = require('casper').create();

casper.start('http://www.example.com', function() {

    this.echo(this.getHTML());
    this.echo('-------------------------');

    var numTimes = 4, count = 2;

    casper.repeat(numTimes, function() {
        this.thenEvaluate(function() {
            nextPage(++count);
        });

        this.then(function() {
            this.echo(this.getHTML());
            this.echo('-------------------------');
        });
    });

});
Run Code Online (Sandbox Code Playgroud)

'我'这是我试图在javascript for循环中使用的索引.

所以tl; dr:我想舔'下一个',打印页面来源,点击'下一步',打印页面来源,点击'下一步'...继续N次.

javascript phantomjs casperjs

5
推荐指数
1
解决办法
1万
查看次数

无法将屏幕截图保存到casperjs中的位置(windows7 64位)

当试图捕获屏幕截图并将其保存在casperjs中时,它会引发以下错误

[error] [phantom]无法将屏幕截图保存到本地目录.请检查权限

试过很多方法

  • 将其保存到公共目录等其他位置
  • 更改本地目录的权限.
  • 以管理员身份运行该过程.

我使用的是Windows 7 64位机器.

在此处浏览此链接

windows-7-x64 phantomjs casperjs

5
推荐指数
1
解决办法
2044
查看次数

将casperjs的默认浏览器更改为chrome(更改用户代理字符串)

我发现casperjs的默认浏览器是safari,因为当我尝试使用casper 访问此站点https://z1.expertchoice.com并创建了一个屏幕截图.

如何将默认浏览器更改为chrome?

javascript casperjs

5
推荐指数
1
解决办法
1万
查看次数

通过npm窗口安装casperjs时,不安全的javascript尝试访问框架

我通过npm在我的windows machina上安装了casperjs和phantomjs.但是我得到了这个问题.

C:\>casperjs sample.js

C:\>Unable to open file: sample.js
Unsafe JavaScript attempt to access frame with URL about:blank from frame
with URL file:///C:/Users/vini/AppData/Roaming/npm/node_modules/casperjs/bin/bootstrap.js.
Domains, protocols and ports must match.
Run Code Online (Sandbox Code Playgroud)

phantomjs casperjs

5
推荐指数
1
解决办法
2477
查看次数

在CasperJS中下载跨域文件

我无法使用CasperJS从Web服务器下载文件流:

  • 表单将发布到网址
  • url返回文件流

到目前为止,我已经验证了发布了正确的表单值.

var casper = require('casper').create({
    verbose: true, 
    logLevel: 'debug',
    viewportSize: {width: 1440, height: 800},
    pageSettings: {
        userName: '****',
        password: '****',
        webSecurityEnabled: false
    },
    waitTimeout: 200000
});

casper.start("***");

casper.then(function() {
    var exportForm = this.evaluate(function() {
        return $("#export_pdf_form").serialize();
    });

    var exportAction = this.evaluate(function() {
        return $("#export_pdf_form").attr('action');
    });

    var url, file;
    url = '***' + exportAction; (eg. https://webserver/export)
    file = "export.pdf";
    casper.page.settings.webSecurityEnabled = false;
    casper.download(url, fs.workingDirectory + '/' + file, "POST", exportForm);
});
Run Code Online (Sandbox Code Playgroud)

Casper错误"不幸的是casperjs无法进行跨域ajax请求",其次是"XMLHttpRequest Exception 101".在搜索之后,它指出将web安全性变量设置为false应该使其工作......但事实并非如此.还有什么我应该研究的吗?

casperjs - v1.1.1 phantomjs …

javascript cross-domain casperjs

5
推荐指数
1
解决办法
961
查看次数

使用casperjs下载资源图像文件

我阅读了这些文件,看起来你需要有slimerjs http://docs.casperjs.org/en/latest/events-filters.html才能responseData.bodypage.resource.received活动中获得.

我的用例是在页面加载时下载图像,所以我不再做往返:获取资源JSON,下载并保存任何图像文件,重新加载文件以检查图像尺寸,如果太小(图标) ) - >消除.

我想知道是否有更好的方法来做到这一点.我实际上可以evaluate img选择,但有些网站使用background-urlCSS,这很棘手.

javascript node.js phantomjs casperjs slimerjs

5
推荐指数
1
解决办法
403
查看次数