0

我试图弄清楚如何使用 rspec 测试 jQuery fadeIn / fadeOut 行为。我确实有以下情况:

在页面上有一个复选框。如果选中该复选框,则会显示一个额外的输入字段。如果不是,则隐藏输入字段。我用来显示和隐藏输入字段的动画是 jQuery fadeIn / fadeOut 效果。

在我的功能测试中,我想检查勾选复选框时,输入字段是否显示在页面上,如果未选中,则不显示输入字段。现在的问题是,当我在我的 rspec 中调用 check('#checkbox_recurring') 然后立即测试时,如果输入字段在页面上,它可能会失败,因为 jQuery 动画尚未完成。

如何测试这样的场景?

谢谢你的帮助!

4

1 回答 1

1

我不知道如何让 jQuery fadeIn 工作,但我使用了切换。我希望这没问题。

<html>
<head>
<style>
  #input {
    font-weight: bold;
    font-size: 16px;
  }
 </style>
 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
 </head>
 <body>
<input type='checkbox' id='check'>Toggle</button>
<input id = 'input' placeholder = 'name'></input>
<script>
$( "#check" ).click(function() {
  $( "#input" ).toggle( "slow" );
});
</script>
</body>
</html>

您需要使用 rubysleep (time)​​ 等待 jQuery/AJAX 元素完成。这是一个解释Watir 等待方法的链接。

我使用 Rspec 和 Watir-Webdriver 来自动化浏览器。这是一个快速的片段,这可能更清晰,但它应该为您尝试完成的工作提供一些方向。

require 'rspec'
require 'watir-webdriver'


describe 'fade' do
  before(:all) do
    @browser = Watir::Browser.new :chrome
    @browser.goto('file:///C:/Users/bashir.osman/Desktop/test.html')
  end
  it 'checks fade' do
   puts "so...#{@browser.input(:id, 'check').exists?}"
   @browser.input(:id, 'check').click
   sleep 1
   exists1 = @browser.input(:id, 'input').visible?
   if exists1 == false
     puts 'Currently input is not visible'
     puts 'Will click input again'
     @browser.input(:id, 'check').click
     sleep 1
     exists2 = @browser.input(:id, 'input').visible?
     exists2.should == true
   end
  end
end

这就是测试的作用:

  1. 点击复选框
  2. 检查是否#input可见。这应该返回 false。
  3. 再次点击复选框
  4. 检查 `#input 是否可见。这应该返回 true

我不会使用.exists?,因为它总是返回 true,因为元素存在于 DOM 中。present?如果元素存在并且在页面上可见,则返回 true。visible?根据元素是否可见返回真/假。

于 2013-09-20T16:29:17.317 回答