Jasmine 测试在测试运行中、Firefox/Chrome 之间以及检查器开/关时的结果不一致

Jasmine tests have inconsistent results across test runs, between Firefox/Chrome, and with inspector on/off

本文关键字:检查 不一致 结果 之间 测试运行 测试 Jasmine Chrome Firefox      更新时间:2023-09-26

我花了很长时间试图让茉莉花的测试通过。它位于包含 10 行的页面上。有一个按钮可以切换显示/隐藏第 4-10 行。手动测试时,该功能在 100% 的时间内运行良好。但是,当我运行下面的 Jasmine 测试(它可以显示总共 10 行)时,它有时会在 Firefox 上失败,并且在 Chrome 上大多数时间都有效(尽管有时打开 Chrome 检查器会更改测试行为)

这是测试:

describe('Show / Hide feature', function() {
   beforeEach(function(){
    loadFixtures('tools_generate__codes.html');
    GenerateCodes.bind();
    GenerateCodesFunctions.hideExtraRows();
    GenerateCodesFunctions.toggleShowHideMore();
  });
  it('defaults to showing 3 rows', function() {
    expect($('.filenames >tbody > tr:visible').length === 3).toBeTruthy();
  });
  it('can show 10 total rows', function() {
    $('.show_more').click();
    expect($('.filenames >tbody > tr:visible').length === 10).toBeTruthy();
  });
});

和代码:

GenerateCodesFunctions = {
  toggleShowHideMore: function() {
    $('.show_more').click(function(event) {
      event.preventDefault();
      if ($('.show_more').html() === 'Show More') {
        alert("WTF");
        GenerateCodesFunctions.showExtraRows();
      } else {
        GenerateCodesFunctions.hideExtraRows();
      }
    });
  },
  hideExtraRows: function() {
    for (var i = 4; i <= 10; i++) {
      $("#generate__codes_filename" + i).val('');
      $("#generate__codes_filename" + i + "_count").val('');
      $("#generate__codes_filename" + i + "_prefix").val('');
      $("#generate__codes_filename" + i).parent().parent().hide();
    }
    $('.show_more').html("Show More");
  },
  showExtraRows: function() {
    for (var i = 4; i <= 10; i++) {
      $("#generate__codes_filename" + i).parent().parent().show();
    }
    alert('showed extra rows');
    $('.show_more').html("Show Less");
  }
};

似乎正在发生的事情是单击.show_more按钮 2-3 次(警报"wtf"出现多次)。$('.show_more').click 代码在测试中只出现一次。在两者上,看起来第一次单击不会将按钮的文本更改为"显示更少"。在 Chrome 上,第二次单击会将文本更改为"显示更少",而在 Firefox 上则不会,我认为这是导致问题的原因。

为什么按钮

被多次单击,为什么它不更改按钮的文本。另外,为什么这种行为在 Firefox 和 Chrome 上有所不同?

找出导致问题的原因。我愚蠢地将页面中的直接html复制到夹具中,这可能会导致加载夹具重新加载所有javascript文件。我认为要么重新加载.js文件导致错误,要么加载速度减慢的事实导致错误,点击事件处理并不总是在预期之前完成(设置期望超时导致测试通过)。