Search code examples
iosrubyscreenshotcalabashcalabash-ios

change path of screenshot taken by calabash in ios simulator on scenario failure using ruby


i am using calabash to do testing on a iOS native app. calabash takes a screenshot and names it screenshot_0 on scenario failure and saves it in my project directory. i want to know how i can change the path of the screenshot and how i can change the name of the file.

i used the following:

dir_path = "/Users/bmw/Desktop/Calabash/screenshots "
unless Dir.exist?(dir_path)
    Dir.mkdir(dir_path,0777)
    puts "=========Directory is created at #{dir_path}"
else
    puts "=========Directory already exists at #{dir_path}"
end

#Run after each scenario
After do |scenario|
  #Check, scenario is failed?
  if(scenario.failed?)
         time = Time.now.strftime('%Y_%m_%d_%Y_%H_%M_%S_')
         name_of_scenario = time + scenario.name.gsub(/\s+/, "_").gsub("/","_")
         puts "Name of snapshot is #{name_of_scenario}"
         file_path = File.expand_path(dir_path)+'/'+name_of_scenario +'.png'
         page.driver.simulator.save_screenshot file_path
         puts "Snapshot is taken"
    puts "#===========================================================#"
    puts "Scenario:: #{scenario.name}"
    puts "#===========================================================#"
  end
end

i had seen page.driver.browser,simulator.save_screenshot somewhere... and replaced browser with simulator and that did not work... is there any way to change location where ios sim saves simulator without touching failure_helpers?


Solution

  • Calabash exposes and environment variable named SCREENSHOT_PATH you can use to set the path where to save the screenshots.

    As for the file name, you might want to try and use the screenshot API. Reading your comment you seem to have tried it already, but I think you might not have used the proper signature.

    Looking at the source for screenshot we see that it's defined like this:

    def screenshot(options={:prefix => nil, :name => nil})
      ...
    

    As you can see it's expecting a map, so what you should try is

    screenshot({:name => name_of_scenario })
    

    Also note that the documentation says that the use of screenshot_embed is preferred to screenshot.