1

I am running android test using Cucumber.The reports of the test are being stored in /data/data/pkg_name/cucumber-reports.Now I need that content inside "cucumber-reports" folder.I used copy command to copy the cucumber reports folder from internal storage to Public External Directory(PICTURES) which is working in Marshmallow.But when I ran in Nougat,I am unable to copy that folder to External Directory.I am unable to access the folder on Android 7+ version device as due to the introduction of new file permission changes.

Without FileProviders,I am using execute Shell command of Instrumentation to copy"cp" folder from internal app dir to External Dir. Please find the copy code below:

   public static void copyReportsToPublicDirs(Scenario scenario) {
        try {
            File externalStoragePublicDirectory=  Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES);
            String extStrPath= externalStoragePublicDirectory.getPath()+"/"+"cucumber-reports";
            new File(extStrPath).mkdirs();
            Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
            String command = "cp -R -f "+"/data/data/"+ BuildConfig.APPLICATION_ID+"/cucumber-reports "+extStrPath;
            Log.println(Log.INFO,"Copy Reports to SD Card","Performing copy the reports to SD Card");
            ParcelFileDescriptor pfd =instrumentation.getUiAutomation().executeShellCommand(command);
            if(pfd.canDetectErrors())
            Log.println(Log.INFO,"Error displayed","Copy Error");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

The above code works fine on Marshmallow but on Nougat,I am not seeing the folder and its contents not copied from it. By what ways,can I retrieve the cucumber report folder inside data/data to External Dir?

Gopinath V S
  • 91
  • 1
  • 11
  • Which changes that might be? – greenapps Dec 14 '17 at 08:01
  • Hey @greenapps,Please have a look : [Nougat Restriction](https://developer.android.com/about/versions/nougat/android-7.0-changes.html) On the File System permission changes,they restricted the access to private app dirs and to access that,we need to use File Providers – Gopinath V S Dec 14 '17 at 09:41
  • There has not much changed. I wonder which problem you have exactly. Why us fileprovider mentioned in the subject and not in your post? – greenapps Dec 14 '17 at 11:17
  • I am using Cucumber Android for my Android Test where the reports are being generated in /data/data/ directory in Internal storage.I want to pull that entire directory from that folder.I can able to do it when I ran the android test in Android 6.0 but on Android 7.0,I am unable to pull that dir. So,I checked that post and thought that File Provider might help in getting that folder from Internal Storage. – Gopinath V S Dec 14 '17 at 12:14
  • Yes ....and.... can it? What is the problem? What is the question? – greenapps Dec 14 '17 at 12:23
  • `I want to pull that entire directory from that folder.` I have no idea what you want to do. Cant you take the effort to decently explain what you want? And after that telling which troubles you have doing so. And maybe you can formulate some question(s). – greenapps Dec 14 '17 at 12:25
  • Sorry for the messy question.Since I am running android test using Cucumber.The reports of the test are being stored in /data/data/pkg_name/cucumber-reports.Now I need that content inside "cucumber-reports" folder.I used copy command to copy the cucumber reports folder from internal storage to Public External Directory(PICTURES) which is working in Marshmallow.But when I ran in Nougat,I am unable to copy that folder to External Directory.Hope it is clear now. – Gopinath V S Dec 14 '17 at 16:22
  • Copy command? Please elaborate. I still do not know what you try. I dont understand that you do not make clear what you are doing. And this is a code site. And i see no code. – greenapps Dec 14 '17 at 16:39
  • I am executing shell command "cp" to copy the folder "cucumber-report" to the "Pictures" directory of the device. – Gopinath V S Dec 14 '17 at 17:35
  • O. I see now. Why are you using a shell command to begin with? Makes no sense to me. You can copy directories without them. – greenapps Dec 14 '17 at 18:30
  • And you do not need to use a file provider doiing so. – greenapps Dec 14 '17 at 18:36
  • I am copying the files on the "After" hooks of cucumber and I used shell command to do it every time when the test completes.Is there any way to copy without using commands & file providers? – Gopinath V S Dec 15 '17 at 11:43
  • Thats what i said yes. Search. Google. – greenapps Dec 15 '17 at 11:45

1 Answers1

0

What you need is here: https://stackoverflow.com/a/15559278/8361368
You can write test bash script like this:

adb install your.target.apk
adb install your.cucumber.apk
adb shell am instrument ...
adb backup -f xxx.ab $YOUR_CUCUMBER_PACKAGE_NAME
dd if=xxx.ab bs=1 skip=24 | python -c "import zlib,sys;sys.stdout.write(zlib.decompress(sys.stdin.read()))" | tar -xvf -

Then you got your whole cucumber report files on your host dirs.

Qi Chen
  • 128
  • 1
  • 9