Protractor 无法在量角器中使用公钥加密字符串

Protractor 无法在量角器中使用公钥加密字符串,protractor,jsencrypt,Protractor,Jsencrypt,我正在尝试调用下面提到的加密函数: var encryptor = require("./jsencrypt.js"); this.encrypt = function () { var key="LxVtiqZV6g2D493gDBfG0BfV6sAhteG6hOCAu48qO00Z99OpiaIG5vZxVtiqZV8C7bpwIDAQAB"; encryptor = new JSEncrypt(); encryptor.setPublicKey(key); var newS

我正在尝试调用下面提到的加密函数:

var encryptor = require("./jsencrypt.js");
this.encrypt = function () {
  var key="LxVtiqZV6g2D493gDBfG0BfV6sAhteG6hOCAu48qO00Z99OpiaIG5vZxVtiqZV8C7bpwIDAQAB";
  encryptor = new JSEncrypt();
  encryptor.setPublicKey(key);
  var newString = encryptor.encrypt('Password');
  console.log("Encrypted password =",newString);
}
最初,我得到了未定义JSEncrypt的引用错误。 所以我下载了jsencrypt.js文件并添加了
var encryptor=require(“./jsencrypt.js”)开始时

现在我遇到以下错误:

Message:
ReferenceError: navigator is not defined
Stacktrace:
ReferenceError: navigator is not defined
at e:\Praveen Data\Projects\ECP\CentralRegistryUI\TestScripts\Utils\jsencrypt.js:73:13
at Object.<anonymous> (e:\Praveen Data\Projects\ECP\CentralRegistryUI\TestScripts\Utils\jsencrypt.js:4342:3)
at require (module.js:385:17)
消息:
ReferenceError:未定义导航器
堆栈跟踪:
ReferenceError:未定义导航器
在e:\Praveen Data\Projects\ECP\CentralRegistryUI\TestScripts\Utils\jsencrypt.js:73:13
反对。(e:\Praveen Data\Projects\ECP\CentralRegistryUI\TestScripts\Utils\jsencrypt.js:4342:3)
根据需要(module.js:385:17)

尝试在jsencrypt.js中使用windows.navigator,但无效。

量角器测试不是在浏览器环境中运行,而是在node.js中运行,因为那里没有可用的navigator对象。JSEncrypt依靠它在客户端跨不同浏览器和版本工作


JSEncrypt代码中的许多地方都引用了它,因此我的最佳选择是切换到适合您的服务器端加密库,或者在不可能的情况下模拟一个具有所有预期属性/方法的global navigator json对象,就像它是一个Chrome浏览器一样-node.js在Chrome的js引擎上运行,因此应该可以工作很好。

量角器测试不是在浏览器环境中运行,而是在node.js中运行,因为导航器对象在那里不可用。JSEncrypt依靠它在客户端跨不同浏览器和版本工作


JSEncrypt代码中的许多地方都引用了它,所以我的最佳选择是切换到适合您的服务器端加密库,或者在不可能的情况下模拟一个具有所有预期属性/方法的全局导航json对象,就像它是一个Chrome浏览器一样-node.js在Chrome的js引擎上运行,因此应该可以正常工作。

其中一个我的同事帮我解决了这个问题。 这里我有一个加密函数:

this.initializeEncryptedPassword = () => {
    //console.log("before calling encrypt... ");
    browser.executeScript(() => {
      //console.log("Starting to return encryptor...");
      return window.loginEncryptor.encrypt(window.loginPassword);
    }).then((encryptedPassword) => {
      this.encryptedPassword = encryptedPassword;
    });
    //console.log("after calling encrypt...");
}
此函数由以下人员调用:

export default class Encryptor {

  constructor($window, $http) {
    'ngInject';
    this.encryptor = new $window.JSEncrypt();
    //Need to use HTTP here instead of resource since the resource does not return plain text.
    //Getting Public Key by hitting a rest uri.
    $http({method: "GET", url: "/xyz/authenticate"}).success((item) => {
        this.encryptor.setPublicKey(item);
        //set the current encryptor on the window so that testing can use it
        $window.loginEncryptor = this.encryptor;
    });
  }

  encryptPassword(credentials) {
    credentials.password = this.encryptor.encrypt(credentials.password);
  }

}

希望这对其他人有所帮助。

我的一位同事帮助我解决了这个问题。 这里我有一个加密函数:

this.initializeEncryptedPassword = () => {
    //console.log("before calling encrypt... ");
    browser.executeScript(() => {
      //console.log("Starting to return encryptor...");
      return window.loginEncryptor.encrypt(window.loginPassword);
    }).then((encryptedPassword) => {
      this.encryptedPassword = encryptedPassword;
    });
    //console.log("after calling encrypt...");
}
此函数由以下人员调用:

export default class Encryptor {

  constructor($window, $http) {
    'ngInject';
    this.encryptor = new $window.JSEncrypt();
    //Need to use HTTP here instead of resource since the resource does not return plain text.
    //Getting Public Key by hitting a rest uri.
    $http({method: "GET", url: "/xyz/authenticate"}).success((item) => {
        this.encryptor.setPublicKey(item);
        //set the current encryptor on the window so that testing can use it
        $window.loginEncryptor = this.encryptor;
    });
  }

  encryptPassword(credentials) {
    credentials.password = this.encryptor.encrypt(credentials.password);
  }

}
希望这对其他人有所帮助。

在需要('jsencrypt')之前,您可以先编写:

const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;
global.window = window;
global.document = window.document;
global.navigator ={userAgent: 'node.js'};

const { JSEncrypt } = require('jsencrypt')
const{JSDOM}=require('JSDOM');
const jsdom=新的jsdom(“”);
const{window}=jsdom;
global.window=窗口;
global.document=window.document;
global.navigator={userAgent:'node.js'};
const{JSEncrypt}=require('JSEncrypt')
在require('jsencrypt')之前,您可以先编写:

const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;
global.window = window;
global.document = window.document;
global.navigator ={userAgent: 'node.js'};

const { JSEncrypt } = require('jsencrypt')
const{JSDOM}=require('JSDOM');
const jsdom=新的jsdom(“”);
const{window}=jsdom;
global.window=窗口;
global.document=window.document;
global.navigator={userAgent:'node.js'};
const{JSEncrypt}=require('JSEncrypt')

您可以通过执行以下操作进行模拟:

global.navigator={appName:'量角器'}

global.window={}


const JSEncrypt=require('JSEncrypt')。默认值

您可以通过执行以下操作进行模拟:

global.navigator={appName:'量角器'}

global.window={}


const JSEncrypt=require('JSEncrypt')。默认值

谢谢你,汤姆。正如您所说,我将尝试全局导航json对象并使其工作。我会用解决方案代码更新这篇文章,如果我能让它工作的话。谢谢你,汤姆。正如您所说,我将尝试全局导航json对象并使其工作。如果我能使它工作,我将用解决方案代码更新这篇文章。