Ale*_*eri 2 javascript testing phantomjs casperjs
嗨,我想知道如何使用casperjs获取输入值
这是我的html元素
<input type="hidden" name="key" value="newkey">
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
view = 'users/registered';
casper.test.begin("Activating account", 5, function register(test){
casper.start(webroot + view, function(){
}).then(function(){
this.echo("Retrieving data from hidden input key", "COMMENT");
activationKey = this.evaluate(function() {
return __utils__.getFieldValue('key');
});
}).then(function(){
this.echo("Activating account with the key \"" + activationKey + "\"", "COMMENT");
window.location = webroot + activationKey;
});
casper.run(function() {
this.echo('Account activated successfully', 'SUCCESS').exit();
test.done();
});
});
casper.viewport(page.width, page.height);
Run Code Online (Sandbox Code Playgroud)
在这种情况下返回 null
我也尝试过:
activationKey = __utils__.getFieldValue('key');
Run Code Online (Sandbox Code Playgroud)
但是给我这个错误:
FAIL ReferenceError: Can't find variable: __utils__
Run Code Online (Sandbox Code Playgroud)
Fan*_*nch 11
试试这个:
this.getElementAttribute('input[type="hidden"][name="key"]', 'value');
Run Code Online (Sandbox Code Playgroud)
如果动态设置该值,则this.getElementAttribute不起作用,也不起作用__utils__.getFieldValue.从1.1.3开始,唯一的方法是调用底层文档对象的querySelector.
var value = this.evaluate(function() {
return document.querySelector('[name="key"]').value
})
Run Code Online (Sandbox Code Playgroud)
有点冗长,但至少它有效.
另外,关于无法找到__utils__的原因,请使用以下命令导入它:
var __utils__ = require('clientutils').create()
Run Code Online (Sandbox Code Playgroud)
在OP的示例中,__ utils__位于evaluate()函数中.它不应该是,因为__utils__是一个casper的东西,而不是DOM/javascript的东西.
| 归档时间: |
|
| 查看次数: |
7252 次 |
| 最近记录: |