CasperJS跳过超时

编程入门 行业动态 更新时间:2024-10-28 19:30:06
本文介绍了CasperJS跳过超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的casperjs测试中有一个页面有图像,我不要等到这个页面加载到下一步。我该怎么做 ? 我试过这种方式

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

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("127.0.0.1/index2.php", function () { this.echo("FIRST GOOD PAGE", "GREEN_BAR"); casper.options.stepTimeout = timeout; }); casper.thenOpen("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只是在加载slopage.php之前调用onStepTimeout。

But it casper just calls onStepTimeout until slopage.php is loaded.

推荐答案

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

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

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

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

您可以更改 waitTimeout 和 stepTimeout 设置。此外,在 pageSettings 下,您可以使casper不加载图像。示例:

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

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

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("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.

更多推荐

CasperJS跳过超时

本文发布于:2023-11-24 10:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624797.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跳过   CasperJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!