2014-02-21 37 views
3

我有一个网页,点击一个按钮后会打开一个弹出窗口。新的弹出窗口有另一个按钮,当点击弹出按钮我弹出关闭和父窗口重新加载到新页面。错误:无法调用已删除的QObject函数

如果我不重装我的父窗口,我得到

FAIL Error: cannot access member `evaluate' of deleted QObject 
# type: uncaughtError 
# error: "Error: cannot access member `evaluate' of deleted QObject" 
# merge: undefined 
Error: cannot access member `evaluate' of deleted QObject 

如果我重装,有没有这样的错误,但casperjs脚本只是卡住,不执行进一步的步骤。

这是我的要求 我想点击open popup,然后弹出加载后再次单击close me按钮。当父窗口重新加载时,获取重新加载页面的内容。

这里是我的代码

1。主页:

<?php 

session_start(); 
$newPage = isset($_SESSION['newpage']) ? $_SESSION['newpage'] : ""; 
?> 
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script> 
<script type="text/javascript"> 
jQuery(document).ready(function() { 
    setTimeout(function() { 
     console.log("NEW PAGE URL = <?php echo $newPage; ?>"); 
     if ("<?php echo $newPage; ?>" != "") { 
      location = "<?php echo $newPage; ?>"; 
     } 
    }, 10000); 
}); 
</script> 
<button onclick="window.open('popup.php');">Click me</button> 

2.popup页:

<?php 
session_start(); 
$_SESSION['newpage'] = 'newpage.php'; 
echo $_SESSION['newpage']; 
?> 
<head> 
    <title>Popup</title> 
</head> 
<button onclick="window.close();window.opener.location.reload();" class="popupbutton">Close me</button> 

3.New页面(重装页) :

<?php 
session_start(); 
$_SESSION['newpage'] = ''; 
?> 
<head> 
    <title>Newpage</title> 
</head> 
This is new page. 
<button onclick="location = 'index.php';" class="popupbutton">Go back</button> 

我已经试过相同https://github.com/n1k0/casperjs/issues/644,但没有奏效。所以,我将代码修改为如下:这不是没有给出预期的结果。

卡斯帕脚本

var casper = require('casper').create({ 
    verbose: true, 
    logLevel: 'debug' 
}); 
var popupClosed = false; 
var popupOpened = false; 
casper.on('popup.closed', function() { 
    this.echo("POPUP CLOSED"); 
    popupClosed = true; 
    this.log(casper.popups.length); 
    this.echo("Page: " + this.getTitle()); 
    this.echo("URL = " + this.getCurrentUrl()); 
    this.then(function() { 
     this.echo(this.evaluate(function() { 
      return document.documentElement.innerHTML; 
     })); 
    }); 
    this.open('http://localhost/sample-app/index.php'); 
    this.then(function() { 
     this.echo(this.getPageContent()); 
    }) 
}); 

casper.on('navigation.requested', function() { 
    this.echo("===" + this.getCurrentUrl() + this.getCurrentUrl().indexOf("newpage") > -1); 
    casper.echo(this.getPageContent()); 
    if (this.getCurrentUrl().indexOf("newpage") > -1) { 
     casper.then(function() { 
      this.echo(this.evaluate(function() { 
       return document.documentElement.innerHTML; 
      })); 
     }); 
    } 
}); 

    casper.start('http://localhost/sample-app/index.php'); 
    casper.then(functi 

on() { 
    this.waitForSelector("button", function() { 
     this.echo("URL = " + this.getCurrentUrl()); 
     this.thenClick("button", function() { 
      this.echo("click to open popup"); 
     }); 
     casper.waitForPopup(/popup\.php/gi, function() { 
      popupClosed = false; 
      popupOpened = true; 

     }); 
     casper.waitFor(function() { 
      return popupOpened; 
     }, function() { 
      casper.withPopup(/popup\.php/gi, function() { 
       this.echo("Popup url: " + this.getTitle() + ", " + this.getCurrentUrl()); 
       this.waitForSelector("button.popupbutton", function() { 
        this.click("button.popupbutton"); 
       }, function() { 
        this.log("No button on popup"); 
       }); 
      }); 
     }, function() { 
      this.echo("Wait TIMEOUT"); 
     }, 60000); 


     casper.then(function() { 
      this.echo("Its here..."); 
      this.waitFor(function() { 
       return popupClosed; 
      }, function() { 
       this.echo("Page: " + this.getTitle()); 
      }, function() { 
       this.echo("TIME OUT"); 
      }, 60000); 
     }); 
    }, function() { 
    }); 
    this.log("COMPLETED"); 
}); 

casper.run(function() { 
    this.exit(); 
}); 

版本:phantomjs -1.9 casperjs - 1.0.2。

回答

0

您的casperjs代码有多个问题。

第一个:事件处理程序永远不会被调用,因为您从不真正发出任何事件。

所以每当你设置popupClosed = true;你也有叫

casper.emit("popup.closed"); 

您也可以致电casper.emit("navigation.requested");下才有意义(不是evaluate块内)。

二:casper.then功能和所有casper.wait*创建将每隔目前预定步骤后安排了一步。

其他已安排的步骤可以包含页面转换,如点击或表单提交,在这种情况下,新安排的步骤将在错误的页面上执行。看来你的错误deleted QObject直接转换为这种情况。

这意味着,如果您等待弹出窗口出现以下功能,则应该减少嵌套或嵌套所有内容。

您与执行指数代码:

casper.waitFor(function() { 
    return popupOpened; 
}, function() { 
    // executed at position 1 
    casper.withPopup(/popup\.php/gi, function() { 
     // executed at position 3 
     this.echo("Popup url: " + this.getTitle() + ", " + this.getCurrentUrl()); 
     this.waitForSelector("button.popupbutton", function() { 
      // executed at position 4 
      this.click("button.popupbutton"); 
     }, function() { 
      this.log("No button on popup"); 
     }); 
    }); 
}, function() { 
    this.echo("Wait TIMEOUT"); 
}, 60000); 


casper.then(function() { 
    // executed at position 2 
}); 

解决方案1: UNNEST,但这并不与弹出的快捷很好地工作,并取决于静态等待严出:

casper.waitFor(function() { 
    return popupOpened; 
}, function() { 
    // executed at position 1 
}, function() { 
    this.echo("Wait TIMEOUT"); 
    this.exit; 
}, 60000); 

casper.withPopup(/popup\.php/gi, function() { 
    // executed at position 2 
    this.then(function(){ 
     this.echo("Popup url: " + this.getTitle() + ", " + this.getCurrentUrl()); 
    }); 
    this.waitForSelector("button.popupbutton"); 
    this.thenClick("button.popupbutton"); 
}); 

casper.then(function() { 
    // executed at position 3 
}); 

编辑:看起来,没有必要等待一段明确的时间。您也可以使用waitFor*函数,但是不要进一步嵌套。看到这个天真的方法工作的这个gist

解决方案2:让每一步都更深更深。这对弹出式窗口不起作用,因为上下文无法在更深的步骤中切换回页面。

相关问题