我有一个网页,可以在点击按钮时弹出一个弹出窗口。新弹出窗口有另一个按钮,点击弹出按钮后弹出窗口关闭,父窗口重新加载到新页面。
如果我没有重新加载我的父窗口,我正在
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 page:
<?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.新页面(重新加载页面):
<?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相同,但它没有用。所以,我修改了代码如下:这不是没有给出预期的结果。
Casper脚本
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 :(得分:0)
您的casperjs代码存在多个问题。
首先:永远不会调用casper.on
事件处理程序,因为您实际上从未发出任何事件。
因此,无论何时设置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:更深入,更深入地嵌套每一步。这不适用于弹出窗口,因为上下文无法在更深层次的步骤中切换回页面。