CasperJS skip step on timeout

后端 未结 2 1761
礼貌的吻别
礼貌的吻别 2020-12-06 15:36

I have one page in my casperjs test that has images , I dont what to wait until this page loaded to go to the next step. How can I do it ? I have tryed this way

<         


        
相关标签:
2条回答
  • 2020-12-06 16:16

    Indeed, in some cases a CasperJS's step can occasionally expire by timeout (if stepTimeout is specified in the settings) due to problems with connection. Default behaviour is to stop CasperJS by this.die. If it's required to not stop CasperJS, but rather continue executing next steps, one should provide a custom onStepTimeout. Unfortunately, request.abort is not defined in this context, because request is only accessible inside onResourceRequested handler (this is why the answer of @Topher Ellis is questionable). In other words, request.abort is useful to prevent a new request from being issued, but can not stop a request which is already in progress (and can be potentially timed out). For such situations I'm using the following code:

    var casper = require("casper").create(
    {
      ...
      onStepTimeout: function(timeout, step)
      {
        this.echo('step timeout');
        this.clear();
        this.page.stop();
      }
    });
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-06 16:24

    You can add request.abort(); in a casper step to end the step and proceed to the next step:

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

    You can also check if you want to abort based on open request. This function will check the url for a match and then abort before opening and will return an http status code:

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

    You can change the waitTimeout and stepTimeout settings. Also, under pageSettings you can make casper not load images. Example:

    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
        }
    });
    

    You can use casper waitFor to wait until the page has completely loaded. Just return true. It even has its own timeout function. So you could do something like this:

    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.
    
    0 讨论(0)
提交回复
热议问题