使用Zombie.js在两个浏览器上测试socket.io

时间:2016-07-28 11:21:25

标签: javascript node.js testing socket.io zombie.js

我正在使用node和socket.io构建实时聊天应用。尝试使用Zombie进行测试时遇到问题。应用程序本身在浏览器中工作正常,但测试失败并显示消息

AssertionError: expected '' to include 'Hello'

在调试期间,似乎当Zombie按下发送按钮时,它不会触发“聊天消息”事件 - 尽管它正在开发中。

describe('chat feature', function() {

  beforeEach(function(done) {
    browser1 = new Browser({
      site: "http://localhost:3000"
    });
    browser2 = new Browser({
      site: "http://localhost:3000"
    });
    done();
  });

  beforeEach(function(done) {
    browser1.visit('/', done);
  });

  beforeEach(function(done) {
    browser2.visit('/', done);
  });

  describe("Chat page has been rendered", function() {
    beforeEach(function(done) {
      browser2.pressButton('Javascript testing');
      browser2.fill('.chatbox-input', 'Hello');
      browser2.pressButton('Send', done);
    });
    it('sends chat messages between browsers', function(done) {
      expect(browser1.text('li')).to.contain('Hello');
      done();
    });
  });
});

HTML(使用jquery动态加载脚本到内容div)

<html>

<head>
  <title>Global Code Network</title>
  <link rel='stylesheet' href='/stylesheets/style.css' />
</head>

<body>

  <div class="main">
    <h1>Global Code Network</h1>
    <div id="content"></div>
  </div>

  <div class="bottom-bar">
    <h2>Current requests:</h2>
    <div id="join-rooms"></div>
  </div>

  <script id="chat-template" type="text/template">
    <ul id="messages"></ul>
    <form id="chatbox" action="">
      <input type="text" class="chatbox-input" id="m" name="chat-input" autocomplete="off" />
      <input type="submit" value="Send"></input>
    </form>
  </script>
  <script id="end-chat-template" type="text/template"></script>
  <script src="/socket.io/socket.io.js"></script>
  <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
  <script src="/scripts/main.js"></script>
</body>
</html>

客户端JS

(function(exports) {

  var socket = io();
  
  socket.on('person joined', function(data) {
    $('.bottom-bar').remove();
    $('#content').html($('#chat-template').html());
    $('#chatbox').submit(function(e) {
      e.preventDefault();
      socket.emit('chat message', {
        roomID: data.roomID,
        message: $('#m').val()
      });
      $('#m').val('');
    });
    socket.on('chat message', function(data) {
      $('#messages').append($('<li>').text(data.message));
    });
  });

  exports.socket = socket;

})(this);

服务器端JS

io.on('connection', function(socket) {
  socket.on('join room', function(data) {
    socket.join(data.roomID);
    io.to(data.roomID).emit('person joined', {
      roomID: data.roomID
    });

    socket.broadcast.emit('update available rooms', {
      rooms: rooms
    });
  });

  socket.on('chat message', function(data) {
    io.to(data.roomID).emit('chat message', data);
  });
});

由于

0 个答案:

没有答案