My requirement is to take a screenshot in android device and use that screenshot as an attachment for my test in run time

I am performing tests, which should take a screenshot of the screen and it should get stored in the device, my test should use that screenshot as an attachment. Is there any possibility to take screenshot in run time and use it as an attachment. If yes, kindly suggest me.

You can take a screenshot with

appium_driver.screenshot(local_path)

Note that this is a ruby solution, but should be supported similarly in other languages. About 5% of the devices I’ve tested do not support taking screenshots with Appium, so your mileage may vary

Hi,

Thanks for the reply,

i tried the command you sent but screen shot is getting saved into my local machine(project). My requirement is screenshot should get saved in the android device where my tests are executing, my tests should use that screenshot as an attachment.

kindly guide me.

@vinay, you can push the file back to the device and therefore put it in a known location (/sdcard):

  contents = File.read(source_file)
  appium_driver.push_file(path_on_device, contents)

Hi,

@willosser Thanks for the reply.

I am new to mobile automation and appium. could you please explain me the above code. I tried contents=File.read(source_file), What is contents here.? Is it is file or a variable.

When i used driver.push_file(), second parameter in this method is asking for Bytetype[], which i did not understand.

If possible could you send me sample code for pushing file back to device and also how to specify the location for putting file in device.

Thanks,
Vinay Kumar.

@vinay, that is my sample code

Contents represents the contents of source file. File.read is reading the contents of source_file and putting it into contents. Those contents are then pushed to the device through Appium.

If you were using adb directly rather than going through Appium, you would just use “adb push source_file path_on_device”. Since Appium’s server might not be on the same machine as the client, we “push” the file to the server, which then writes it to a file and uses adb push to move the file to the device.

This example is in Ruby, which may cause you problems if you are using another language and trying to use these ruby calls.