Angular e2e Testacular Test:如何辨别可见性?

时间:2013-01-28 15:32:59

标签: twitter-bootstrap angularjs modal-dialog karma-runner

问题

如果在单击按钮时显示Bootstrap模式,是否有人成功测试过?

详情

我正在编写一个Testacular测试,用于检查单击按钮时是否显示Bootstrap模式。问题是,css('display')的调用返回'none',即使我可以看到窗口弹出并且可见。

我想知道引导程序模式是否有一些怪异的东西会复制一个html块,然后用不同的id显示它。我当然希望不会!

代码

方案

describe('e2e', function() {

  beforeEach(function() {
    browser().navigateTo('/chessClub/');
  });


  it('should display editor when add member button clicked', function() {
    expect(element('title').text()).toBe("Chess Club");

      expect(element('#myModal').css('display')).toBe('none'); //as expected, it is none
              //click the button to show the modal
      element('#addMemberButton','Add Member Button').click();

              //this 2nd expect still return 'none'
      expect(element('#myModal').css('display')).toBe('block');

  });

});

测试输出

Chrome 25.0 e2e should display editor when add member button clicked FAILED
    expect element '#myModal' get css 'display' toBe "block"
    /Users/jgordon/learning/chessClub/web-app/test/e2e/scenarios.js:17:4: expected "block" but was "none"

HTML

<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">{{editorLabel}}</h3>
    </div>
    <div class="modal-body">
        <form>
            <label>
                Name
            </label>
                <input type="text" ng-model="editedMember.name" placeholder="John Doe">
            <label>
                Grade
            </label>
                <input type="text" ng-model="editedMember.grade">
            <label>
                Ladder Position
            </label>
                <input type="text" ng-model="editedMember.ladderPosition">
        </form>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        <button class="btn btn-primary" ng-click="saveMember()">Save changes</button>
    </div>
</div>

3 个答案:

答案 0 :(得分:7)

一种方法,虽然有点hacky是使用:visible选择器并计算有多少匹配

expect(element('#someElement:visible').count()).toBe(1);

Source

答案 1 :(得分:2)

我最后只需要在测试中调用sleep(1)。我想显示模态的代码并不像测试样式那样快。

答案 2 :(得分:1)

我设法执行以下操作来检查元素是否隐藏:

expect(element("#id_element").css("display")).toBe("none");    

并检查此元素是否可见:

expect(element("#id_element").css("display")).not().toBe("none");

我相信这不是最好的方法。但这很简单,对我有用。

干杯!