【问题标题】:watir test for export and import feature导出和导入功能的 Watir 测试
【发布时间】:2014-02-10 13:16:24
【问题描述】:

我的 web 应用程序中有一个导出和导入功能,我想使用 watir 测试导出到 xls 和从 xls 功能导入。请问谁能给我这个想法?

class TestBasicExport < MiniTest::Unit::TestCase
 def setup
   login_page = @@site.login_page.open # open the page to login
   search_page = login_page.login # login and land on the search page
   @@export_page = search_page.export  # click on the export link to goto export page
 end

 def test_basic_export_works
   export = @@export_page.export # it will click on the exprt button
   assert @@export_page.loaded?, "Export page failed to load"

 rescue Watir::Exception, Watir::Wait::TimeoutError => e
   puts "Some field not found: #{e}"
   assert(false, "Current page is " + @@export_page.browser.url)
 end
end

我可以使用上面的代码点击导出按钮,几秒钟后,它会抛出异常,这很明显(因为导出需要一些时间才能完成,具体取决于数据量):

Run options: --seed 23218

# Running tests:

E

Finished tests in 75.866195s, 0.0132 tests/s, 0.0000 assertions/s.

 1) Error:
test_basic_export_works(TestBasicExport):
Timeout::Error: Timeout::Error

请问,我该怎么做才能完成这个?

谢谢

【问题讨论】:

  • 那么,您是否预计需要“很长”的时间(比如几分钟)?如果是这样,也许你应该在你的代码中添加一个“服务员”。导入/导出完成后,等待元素出现或消失。你试过吗?
  • 是的,我在导出后为另一个事件添加了wait_until_present,现在它正在工作。谢谢
  • 在此处包含您的问题的答案,然后接受它,这样您的问题就不会再出现在未回答问题的搜索中。
  • 哦,是的。对不起。我会发布答案

标签: ruby-on-rails-3.2 automated-tests integration-testing watir watir-webdriver


【解决方案1】:

我的问题的解决方案是:

class TestBasicExport < MiniTest::Unit::TestCase
  def setup
    login_page = @@site.login_page.open # open the page to login
    search_page = login_page.login # login and land on the search page
    @@export_page = search_page.export
    assert @@export_page.loaded?, "Export page failed to load!"

    # find number of export.xls file before downloading and save it in 
    # some variable
    if @@export_page.count_export_file == 1
      @@export_page.remove_export_file
    end
    # delete export file if already exists and confirm it
    @number_of_files = @@export_page.count_export_file
    assert_equal(0, @number_of_files, "Export file already exists")

    @@site.download_directory_setup

    @@export_page.export
    assert @@export_page.completed?, "Export failed!"
  end

  def test_basic_export_works
    # verify file is downloaded
    assert_equal(1, @@export_page.count_export_file, "Export did not happened")

    # check for downloaded file is having valid rows
    assert_equal(true, @@export_page.verify_headers? , "Downloaded file in invalid")

    assert_equal(true, @@export_page.verify_download?, "Excel file is not having all datas")
  rescue Watir::Exception, Watir::Wait::TimeoutError => e
    puts "Some field not found: #{e}"
    assert(false, "Current page is " + @@export_page.browser.url)
  end
end

count_export_fileremove_export_file 看起来像:

# this method will count the number of export.xls file in project directory
def count_export_file
  Dir[File.join("#{file_path}/", 'export.xls')].count { |file| File.file?(file) }
end

# method for removing file from project directory
def remove_export_file
   File.delete("#{file_path}/" + 'export.xls')  
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-29
    • 2017-01-22
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    相关资源
    最近更新 更多