【发布时间】:2013-12-11 03:18:55
【问题描述】:
我正在使用 Selenium 的 Ruby Bindinds 和 Test::Unit,通过 Ruby 使用 Selenium 对 UI 交互进行问题单元测试。它在很大程度上是成功的,但我遇到了一个我无法开始工作的测试用例。我需要做的是等待表格加载到页面上,然后循环并单击类为sorting 的表格标题,记录表格的第一行并将其与点击后的第一行进行比较。跳过没有sorting 类的标题并确定所有可排序的标题是否在浏览器中工作。
现在的问题。测试因 Selenium 超时导致 assert_nothing_raised 测试失败。在哪里?我不知道。它引用 [test_cases/tc_commodities.rb:6] 但第 6 行是 assert_nothing_raised do。由于测试从那开始,所以没有太多信息。然后我开始通过删除代码来缩小我的代码范围。问题出在columns.each 循环中,这意味着wait.until 块。这没有任何意义,因为我可以在超时期间看到页面上显示的标题。页面关闭,测试失败。我再次运行测试,停止执行并查看 DOM。所有正确的元素都在那里并正在显示。剧本有足够的时间来接他们。可以肯定的是,我输入了sleep 15。等待标题显示仍然超时。
为什么会超时?我从来没有遇到过页面上显示的项目没有为 .displayed 返回 true 的问题?
这是这个测试用例的最佳方法吗?谁能提出更好的解决方案?
失败的测试输出
$ ruby test_cases/tc_commodities.rb
Run options:
# Running tests:
Column Found
F
Finished tests in 46.652411s, 0.0214 tests/s, 0.0214 assertions/s.
1) Failure:
test__sort_columns_on_stories_table(TestCommodities) [test_cases/tc_commodities.
rb:6]:
Exception raised:
<#<Selenium::WebDriver::Error::TimeOutError: timed out after 10 seconds>>.
1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
硒代码
def test__sort_columns_on_stories_table
assert_nothing_raised do
@browser.get 'some url'
@wait.until do
@browser.find_element(:css, "#dataTable tbody tr:nth-child(2)").displayed?
end
columns = @browser.find_elements(:css, "#dataTable thead th.sorting")
columns.each do |column|
puts "Column Found"
@wait.until do
column.displayed?
end
puts "Column Displayed"
first_story_before = @browser.find_element(:css, "#dataTable tbody tr:nth-child(1)").attribute('id')
column.click
first_story_after = @browser.find_element(:css, "#dataTable tbody tr:nth-child(1)").attribute('id')
assert_not_equal(first_story_after, first_story_before)
end
end
end
编辑
我正在使用 Chrome 和 Firefox 网络驱动程序。两者的问题相同。我可以删除column.displayed? 的wait.until,但是它会抛出一个错误,当它明显存在时它不会显示在页面上。
【问题讨论】:
标签: html ruby unit-testing selenium