javascript - CasperJS 超时时跳过步骤

标签 javascript casperjs

我的 casperjs 测试中有一个页面包含图像,我不知道要等到该页面加载后才能进入下一步。我该怎么做 ? 我试过这个方法

var casper = require("casper").create({
   onStepTimeout: function() {
                        this.echo("TIMEOUT" + this.requestUrl,"RED_BAR");
   // Some skip page controlling code 
   },
);
var timeout = ~~casper.cli.get(0);

casper.start("http://127.0.0.1/index2.php", function () {
    this.echo("FIRST GOOD PAGE", "GREEN_BAR");
    casper.options.stepTimeout = timeout;
});

casper.thenOpen("http://127.0.0.1/slowpage.php", function() {
        this.echo("SECOND PAGE LOADED - I want to be called even  have received Timeout!", "GREEN_BAR");
});

casper.then(function() {
    this.echo("THEN!", "GREEN_BAR"); 
});

但 casper 只是调用 onStepTimeout 直到 slopage.php 加载完毕。

最佳答案

您可以在 casper 步骤中添加 request.abort(); 来结束该步骤并继续下一步:

casper.then(function() {
  request.abort();
  this.echo('You will never see me');
});

casper.then(function() {
  this.echo('I execute next');
});

您还可以根据open请求检查是否要中止。此函数将检查 url 是否匹配,然后在打开之前中止,并返回 http 状态代码:

casper.on('page.resource.requested', function(requestData, request) {
    if (requestData.url.indexOf('slowpage.php') !== -1) {
        request.abort();
    }
});

您可以更改 waitTimeoutstepTimeout 设置。另外,在 pageSettings 下,您可以使 casper 不加载图像。示例:

var casper = require('casper').create ({
    waitTimeout: 10000,
    stepTimeout: 10000,
    verbose: true,
    viewportSize: {
      width: 1400,
      height: 768
    },
    pageSettings: {
      "userAgent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
      "loadImages": false,
      "loadPlugins": false,         
      "webSecurityEnabled": false,
      "ignoreSslErrors": true
    },
    onWaitTimeout: function() {
        //throw new Error
    },
    onStepTimeout: function() {
        //throw new Error
    }
});

您可以使用casper waitFor等待页面完全加载。只需返回 true。它甚至有自己的超时功能。所以你可以这样做:

casper.waitFor(function check() {
    casper.thenOpen("http://127.0.0.1/slowpage.php", function() {
        //+++ casper will wait until this returns true to move forward. 
        //+++ The default timeout is set to 5000ms
        this.evaluate(function() {
          //checks for element exist
          if (document.getElementById('someElement')) {
            console.log('Im loaded!');
            return true;
          }
        });
    });   
}, function then() {    // step to execute when function check() is ok
    //+++ executes ONLY after the 'casper.thenOpen' returns true.
    this.echo("THEN!", "GREEN_BAR");
}, function timeout() { // step to execute if check has failed
    //+++ code for on timeout.  This is different than onStepTimeOut.
},1000);// custom timeOut setting.

关于javascript - CasperJS 超时时跳过步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20399453/

相关文章:

javascript - Owlcarousel设置上传到服务器时不显示

javascript - JS Lint 错误 : Don't make functions within a loop - No workaround

javascript - 如何使用 Javascript 防止浏览器发出 clang

javascript - 我如何在 firestore 中按日期和位置可用性进行过滤?

javascript - 我有一个元素已声明为影子 dom,但样式正在影响其他元素

ssl - SlimerJS 忽略 SSL 错误

selenium - 具有下载功能的 headless 浏览器测试?

javascript - 无法使用 CasperJS 在没有表单的情况下填充输入元素

javascript - 带有链式选择的 CasperJs 和 Jquery

ajax - 如何使用 casperjs 捕获和处理 XHR 响应中的数据?