使用量角器的AngularJS-E2E测试:如何通过Websocket连接到服务器



我的应用程序基于在Felix服务器上运行的OSGI模型。GUI在角度。我正在与后端开发器并行开发GUI测试。还没有实施太多的GUI屏幕,因此使用量角器来测试GUI功能是较早的。

但是,我想"假装欺骗gui",并通过菲利克斯(Felix(发送和接收一些活动。这样,我可以测试一些由Frontend发射的主要已知后端功能。

,但我不知道如何将事件发送给Felix-Server!我想打开与Felix-Server的Websocket连接,然后向其发送事件。这是我的代码:

//websocket2Felix.js
var websocket =null;
module.exports = {
    openConnection: function() {
        try{
           websocket = new WebSocket("ws://localhost:8080/myproj");
           websocket.onopen = function(evt) { onOpen(evt) };
           websocket.onclose = function(evt) { onClose(evt) };
           websocket.onmessage = function(evt) { onMessage(evt) };
           websocket.onerror = function(evt) { onError(evt) };
        }
        catch(err){
            browser.logger.info( err.message + "/n");
        }
      },
      doSend: function(message) {
          browser.logger.info("SENT: " + message);
           websocket.send(message);
      }
};   
function onOpen(evt) {
  }
function onClose(evt) {
  }
function onMessage(evt) {
  }
function onError(evt) {
  }

我在测试案例定义中使用此代码,我使用cucumber-syntax编写了该代码:

//home_steps.js
var websocketOnFelix = require ('./websocket2Felix.js');
var homeSteps = function() {
    this.Given(/^I am on the home$/, function () {
      browser.get("path/to/home");
     });
   this.When(/^I click on help button$/, function(){
      //open websocket to felix to send gui-events to it
       browser.pause();
       websocketOnFelix.openConnection();
       browser.pause();
       /////// here to send the click event to felix//////
    });
  this.Then(/^The help screen should be displayed$/, function(){        
      el= element(by.css('#help-screen'));
      expect( el.isPresent() ).to.be.ok;
    });
}); 

这是我的cucmber-configuration:

//cucumConfig.js
'use strict';
exports.config = {
    seleniumAddress: 'http://127.0.0.1:4444/wd/hub',
    getPageTimeout: 60000,
    allScriptsTimeout: 500000,
    framework: 'custom',        
    frameworkPath: require.resolve('protractor-cucumber-framework'),
    output:'./output.json',                
    capabilities: {
        'browserName': 'chrome'
      },
    specs: [
        '*.feature'
      ],
    cucumberOpts: {
        require: '*_steps.js',
        tags: false,
        format: 'pretty',
        profile: false
      },          
};

我打电话

protractor cucumbConfig.js

此行" websocketonfelix.openconnection((;"行之后。我在控制台上从未达到测试案例中的下一行;

添加了try-catch-block后,该行引起的错误

websocket = new WebSocket(...);

是:"未定义的Websocket"。

任何想法,我在做什么错?非常感谢。

看起来您需要安装WebSocket软件包(NPM安装(,然后调用('require' 按照链接中的指示。

最新更新