PhantomJs登录测试
Login Test with PhantomJs
我的phantom Js代码有问题,如下所示,我有代码来测试我的朋友web服务器(使用节点Js制作)。事实上,它看起来简单而完美。
var page = require('webpage').create(); var address = "http://localhost:3333"; // Route "console.log()" calls from within the Page context to the // main Phantom context (i.e. current "this") page.onConsoleMessage = function(msg) { console.log("Console.log: ", msg); }; page.onAlert = function(msg) { console.log("Alert:", msg); }; page.open(address, function (s) { page.evaluate(function () { function click(el){ var ev = document.createEvent("MouseEvent"); ev.initMouseEvent( "click", true /* bubble */, true /* cancelable */, window, null, 0, 0, 0, 0, /* coordinates */ false, false, false, false, /* modifier keys */ 0 /*left*/, null ); el.dispatchEvent(ev); } document.getElementById('username').value = 'MyName'; document.getElementById('password').value = 'MyWord'; click(document.querySelector('input[type=submit]')); }); page.onNavigationRequested = function() { // console.log("Moved", JSON.stringify(arguments)) // to check whether send or not page.render("printscreen" + ".png"); }; setTimeout(function(){ page.render("nextprintscreen" + ".png"); phantom.exit(); }, 3000); });
但是
当我申报var userName = 'MyName';
var passWord = 'MyWord';
然后将其放置在
下方var address = "http://localhost:3333";
和交换document.getElementById('username').value = 'MyName';
document.getElementById('password').value = 'MyWord';
与document.getElementById('username').value = userName;
document.getElementById('password').value = passWord;
它从我的朋友网络服务器返回CCD_ 8。你能帮我弄清楚为什么会这样吗。这是我的第一个"javascript世界"代码。
我已经阅读了这个问题和另一个变体,然后建议
但这只会让我更加困惑。
谢谢,
Ahmad
问题是page.evaluate()
是沙盒的,因此无法访问幻影脚本的变量。
由于PhantomJS 1.6,JSON可序列化参数可以传递给page.evaluate()
。evaluate函数的参数和返回值必须是简单基元对象。但是,对象可以通过JSON进行序列化。
您可以将代码更改为:
page.evaluate(function (login, pwd) {
...
document.getElementById('username').value = login;
document.getElementById('password').value = pwd;
...
}, userName , passWord );
相关文章:
- 模糊事件的Javascript测试
- 我的单元测试选项是什么
- 测试索引值是否等于某个数字的倍数
- 使用acess代币登录Facebook,并通过网络应用程序离线发布
- 当Firefox尝试登录时,Selenium测试失败
- Protractor测试:如何设置登录表单中文本元素的值
- 如何使用已经登录的部分进行自动测试
- 嘿嘿我解决单元测试登录角度JS
- 如何在用户使用护照登录的情况下测试帆.js控制器.js
- js应用程序的登录和注销负载测试
- Protractor e2e测试登录重定向
- PhantomJs登录测试
- 使用量角器和角度带模式测试登录对话框时发生超时错误
- 如果需要登录,如何在Node.js服务器端使用Mocha Chai测试RESTful CRUD api
- 测试facebook javascript登录从localhost域
- 如何测试一个AJAX登录调用
- NodeJS/Passport -使用mocha和superagent测试用户登录
- 在本地测试HTML5登录表单运行良好,但在Phonegap中构建后什么都没有发生
- 量角器,登录到asp,net MVC登录页面,等待默认页面,然后重定向到angular页面并做测试…
- 是否可以在没有登录流的情况下使用Protractor测试流?