Solution for my problem is:
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_file
and remove_export_file
looks like:
# 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