Hi.

I hope it is okay, even though I am not Marcin. ;-)

It looks like you you found the sample code in my SO answer here:
https://stackoverflow.com/a/50679606/1082681

Before we continue, making a possibly simple thing complicated: Are you
aware of the fact that if your specification extends GebReportingSpec,
it will by default have a screenshot reporter, so by a statement like
"report 'my screenshot'" you can just take a screenshot and it will
automatically take a screenshot if the test fails?

See https://gebish.org/manual/current/#testing-reporting and also search
the Book of Geb for "reporting" or "reporter". Just lately someone here
in this group posted about how to write a custom reporter in order to
use AShot for full page screenshot (if the page is bigger than the
viewport or even bigger than the screen size).

Now, having the basic information out of the way and assuming for a
minute that the default screenshot reporter or even a custom reporter
are inadequate for you (which I don't believe, BTW), here is how from
your global Spock extension's RunListener you could in principle access
browser and driver: First you need to get access to the specification
instance, then check if it is really a Geb specification (because not
every Spock spec is a Geb spec) and if so, you can use its methods in
order to access browser and driver:

------------------------------------------------------------------------

package de.scrum_master.testing.extension

import geb.spock.GebSpec
import org.spockframework.runtime.AbstractRunListener
import org.spockframework.runtime.extension.AbstractGlobalExtension
import org.spockframework.runtime.model.ErrorInfo
import org.spockframework.runtime.model.IterationInfo
import org.spockframework.runtime.model.SpecInfo
/**
 * See https://stackoverflow.com/a/50679606/1082681
 */
class TestResultExtension extends AbstractGlobalExtension {
  @Override
  void visitSpec(SpecInfo spec) {
    spec.addListener(new ErrorListener())
  }

  static class ErrorListener extends AbstractRunListener {
    ErrorInfo errorInfo

    @Override
    void beforeIteration(IterationInfo iteration) {
      errorInfo = null
    }

    @Override
    void error(ErrorInfo error) {
      errorInfo = error
    }

    @Override
    void afterIteration(IterationInfo iteration) {
      if (iteration.feature.spec instanceof GebSpec) {
        def driver = (iteration.feature.spec as GebSpec).driver
        driver.manage().window().maximize()
        takeScreenshot(driver)
      }
    }

  }
}

------------------------------------------------------------------------

BTW, you could also check if the spec is an instance of GebReportingSpec
and directly use the reporting feature there. But really, what you
probably want is just use the plain vanilla GebReportingSpec. Maybe,
maybe you want to use a custom reporter in combination with that.

Friendly regards
-- 
Alexander Kriegisch
https://scrum-master.de


GebUser schrieb am 22.12.2020 04:56 (GMT +07:00):
> hi Marcin,
> I am using a class test status tracker class that extends
> AbstractGlobalExtension.
> 
> 
> When an error occurs, I need to take a screenshot. Can you please tell how
> I can access the browser driver here?
> 
> 
> class TestResultExtension extends
> AbstractGlobalExtension
> {
> @Override
> void visitSpec(SpecInfo spec) {
> spec.addListener(new ErrorListener())
> }
> 
> static class ErrorListener extends AbstractRunListener {
> ErrorInfo errorInfo
> # Browser browser
> 
> @Override
> void beforeIteration(IterationInfo iteration) {
> errorInfo = null
> }
> 
> 
> void afterIteration(IterationInfo iteration){
> if (errorInfo!=null){
> driver.manage().window().maximize()
> 
> takeScreenshot(driver)
> }
> }

-- 
You received this message because you are subscribed to the Google Groups "Geb 
User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/geb-user/20201222012352.11C7C33806B0%40dd39516.kasserver.com.

Reply via email to