pedrovgs / Shot

Screenshot testing library for Android
Apache License 2.0
1.18k stars 116 forks source link

Using Shot with Custom TestRunner #308

Open rootsavaris opened 2 years ago

rootsavaris commented 2 years ago

I have one project running with default UI and E2E tests using Custom TestRunner (working with HILT). I'm trying incorporate shot library on screenshots tests but it does not work.

If I use:

testInstrumentationRunner "com.karumi.shot.ShotTestRunner"

it works very well, but if I use:

class HiltTestRunner : AndroidJUnitRunner(){

  privave val shotTestRunner = ShotTestRunner()

  override fun newApplication(...){
   // hilt application  
  }

 override fun onCreate(args: Bundle){
   shotTestRunner.onCreate(args = arguments)
 }

 override fun finish(resultCode: Int, results: Bundle){
  shotTestRunner.finish(resultCode, results)
}}

When I use custom TestRunner, the screen shots doesn't save. Is it a bug ? Is there another way to wotk with Custom TestRunners ?

HeyAlex commented 2 years ago

@rootsavaris we are using a custom test runner as well, and it works well via inheritance. Just make something like here in sample project

soyo2 commented 1 year ago

Hi!

Any progress with this issue?

HeyAlex commented 1 year ago

@soyo2 just inherit from ShotTestRunner and it will works as expected

soyo2 commented 1 year ago

But if I use a 2nd runner, say a runner for allure tests aggregate, then I cannot inherit from ShotTestRunner as I don't own the runner from Allure.

HeyAlex commented 1 year ago

@soyo2 sorry for late reply. it's still possible, just copy/paste code from ShotTestRunner and add dependency for plugin com.facebook.testing.screenshot:plugin, since ScreenshotRunner is added by this plugin. After that you don't need ShotTestRunner and can inherit from AndroidJUnitRunner. I didn't try this solution, but by a quick lookup it's something that should work.

soyo2 commented 1 year ago

Hi Alex

No worries. Yeah, we've did something similar but the other way around. We've copied the contents of the other runner and we decorated our runner which is derived from ShotTestRunner with that content and we've solved it that way, but it would be great to have a possibility of making it work without copying the source code and just relying entirely on the dependencies.

Any chance in the future of adding a way to provide another runner to ShotTestRunner for it to use or making ShotTest more flexible and not requiring it to be used as a runner so we can somehow inject it's operation, maybe via @Rule or something?