简体   繁体   English

在PhantomJS中找不到变量:页面

[英]Can't find variable: page in PhantomJS

I'm beginner in test, both in Unit Test and UI Test 我是单元测试和UI测试的初学者

I'm trying to create a UI test for my login page using following code: 我正在尝试使用以下代码为我的登录页面创建UI测试:

console.log("Teste de Login");

var page = require('webpage').create();
page.open('http://localhost/login', function(status) {
    console.log("Page loadeed");

    if(status === "success") {
        page.render('example1.png');
    }

    page.evaluate(function() {
        // $("#numeroUsuario").val("99734167");
        document.getElementById('numeroUsuario').value = "99734167";
        page.render('exampl2.png');

        // $("#formLogin").submit();
        page.render('example3.png');
    });

    phantom.exit();
});

But this code returns the following error: 但是此代码返回以下错误:

> phantomjs.exe ./testLogin.js
Teste de Login
Page loadeed
ReferenceError: Can't find variable: page

  phantomjs://webpage.evaluate():4
  phantomjs://webpage.evaluate():8

Where element $("#numeroUsuario") exists. 元素$("#numeroUsuario")存在于哪里。 What have I done wrong? 我做错了什么?

The documentation says the following about the page context (emphasis mine): 文档说明了关于页面上下文的内容(强调我的):

The execution is sandboxed , the web page has no access to the phantom object and it can't probe its own setting . 执行是沙箱 ,网页无法访问phantom对象, 无法探测自己的设置

This means that variables defined outside of the page.evaluate() callback function are not accessible inside of it. 这意味着在page.evaluate()回调函数之外定义的变量不能在其中访问。 It also means that this refers to the window object. 它还意味着this指的是window对象。

You can of course split the page.evaluate() call into multiple calls and move the move the calls that use outer variables in between the page.evaluate() calls like Platinum Azure showed , but this doesn't work if you want to call some PhantomJS function from inside of a callback inside of the page.evaluate() callback. 您当然可以将page.evaluate()调用拆分为多个调用,并移动在使用外部变量的调用之间,如在Platinum Azure中显示page.evaluate()调用,但是如果要调用则不行一些PhantomJS函数来自page.evaluate()回调内部的回调。

The solution would be to use the window.callPhantom and page.onCallback pair . 解决方案是使用window.callPhantompage.onCallback This is perfect for asynchronous functions: 这非常适合异步功能:

var renderId = 0;
page.onCallback = function(data){
    console.log("Callback: " + data.type);
    if (data.type === "exit") {
        phantom.exit();
    } else if (data.type === "render") {
        page.render(data.fname || ("screenshot_" + (renderId++) + ".png"));
    }
};

page.onConsoleMessage = function(msg){
    console.log("remote> " + msg);
};

var getUrl = "http://example.com";
page.open(url, function(){
    page.evaluate(function(getUrl){
        $.get(getUrl, "", function(data){
            console.log(JSON.stringify(data));
            window.callPhantom({ type: "render" });
            window.callPhantom({ type: "exit" });
        });
    }, getUrl);
});

The exit might interfere with the previously triggered render operation. 出口可能会干扰先前触发的渲染操作。 It surely is possible to delay the exit in that case by some fixed amount of time such as half a second: 在这种情况下,肯定可以延迟退出一段固定的时间,例如半秒:

if (data.type === "exit") {
    setTimeout(function(){
        phantom.exit();
    }, 500);
}

Furthermore it isn't possible to pass the page object into the page context, because only serializable objects can be passed in: 此外,无法将page对象传递到页面上下文中,因为只能传入可序列化的对象:

Note: The arguments and the return value to the evaluate function must be a simple primitive object. 注意: evaluate函数的参数和返回值必须是一个简单的原始对象。 The rule of thumb: if it can be serialized via JSON, then it is fine. 经验法则:如果它可以通过JSON序列化,那么它很好。

Closures, functions, DOM nodes, etc. will not work! 闭包功能,DOM节点等等都不行!

Pretty sure that in a page.evaluate environment, you can't reference anything from the Phantom script. 很确定在page.evaluate环境中,您无法从Phantom脚本中引用任何内容。

In your case, you could actually have multiple evaluate calls: 在您的情况下,您实际上可以有多个评估调用:

console.log("Teste de Login");

var page = require('webpage').create();
page.open('http://localhost/login', function(status) {
    console.log("Page loadeed");

    if(status === "success") {
        page.render('example1.png');
    }

    page.evaluate(function() {
        // $("#numeroUsuario").val("99734167");
        document.getElementById('numeroUsuario').value = "99734167";
    });

    page.render('exampl2.png');

    page.evaluate(function() {
        // $("#formLogin").submit();
    });

    page.render('example3.png');

    phantom.exit();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM