allure-kotlin icon indicating copy to clipboard operation
allure-kotlin copied to clipboard

Calling allureScreenshot on a different thread does not add attachment

Open mattinger opened this issue 4 years ago • 1 comments
trafficstars

I'm submitting a ...

  • [ X] bug report
  • [ ] feature request
  • [ ] support request => Please do not submit support request here, see note at the top of this template.

What is the current behavior?

I am trying to monitor the destinations of my navController, and at each change in destination record an allure snapshot. The snapshot png files are produced in the allure-results directory. However, they are not referenced as attachments in the .json file. Instead i get an empty attachments array.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem

The function i'm using to monitor the nav controller:

fun takeScreenshotOnNavigation(testName: String, navController: NavController): Job {
    val counter = AtomicInteger(0)
    return GlobalScope.launch(Dispatchers.IO) {
        navController.currentBackStackEntryFlow.collect {
            val screenshotName = "${testName}-${counter.getAndIncrement()}"
            allureScreenshot(name=screenshotName)
        }
    }
}

NOTE: I also tried addOnDestinationChangedListener and got the same behavior.

And calling it here in my espresso test:

scenario.onActivity { activity ->
    screenshotJob = takeScreenshotOnNavigation(this::class.java.simpleName + "-" + "testPowerBatteries", activity.navController)
}

but the only attachment i see in the json is the main one from the ScreenshotRule:

    "attachments": [
        {
            "source": "027b14c8-e997-43ca-8c2e-a389104e7d58-attachment.png",
            "name": "failure-screenshot",
            "type": "image/png"
        }
    ],

But the allure-results directory has all the screenshots in it.

What is the expected behavior?

I would expect these attachments to be record in the results.json file.

What is the motivation / use case for changing the behavior?

Automating screenshots on navigation events.

Please tell us about your environment:

Allure version 2.2.7
Test framework [email protected]
Allure integration [email protected]

mattinger avatar Nov 12 '21 19:11 mattinger

I did figure out that this works a bit better using the above function.

fun <A: Activity> takeScreenshotOnNavigation(
    testName: String,
    scenario: ActivityScenario<A>,
    navControllerProvider: (A) -> NavController
): Job {
    lateinit var navController: NavController
    scenario.onActivity { activity ->
        navController = navControllerProvider(activity)
    }
    return takeScreenshotOnNavigation(testName, navController)
}

Presumably because i'm no longer starting the monitoring inside of the main thread (via scenario.onActivity)

mattinger avatar Nov 18 '21 14:11 mattinger