在PhantomJS中找不到变量:页面 [英] Can't find variable: page in PhantomJS

查看:457
本文介绍了在PhantomJS中找不到变量:页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是测试的初学者,在单元测试和UI测试中

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:

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)存在。我做错了什么?

Where element $("#numeroUsuario") exists. What have I done wrong?

推荐答案

非常确定在 page.evaluate 环境,你不能从Phantom脚本中引用任何东西。

Pretty sure that in a page.evaluate environment, you can't reference anything from the Phantom script.

在你的情况下,你实际上可以有多个评估调用:

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();
});

这篇关于在PhantomJS中找不到变量:页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆