Re: [geb-user] Ability to skip setup() for certain tests

2020-05-10 Thread Alexander Kriegisch
Thanks Marcin,

but this does not work. In Spock 1.3 (Groovy 2.5) each test dies with
exceptions because in class FeatureInfo we have

  @Override
  public AnnotatedElement getReflection() {
throw new UnsupportedOperationException("getReflection");
  }

Same in 2.0-M2 (Groovy 3.0), only there the test runner does not print
anything and for each test just says that it does not find any tests to
run.

Best regards
-- 
Alexander Kriegisch


Marcin Erdmann schrieb am 11.05.2020 00:22 (GMT +07:00):
> 
> Thanks for sharing, Alexander. It's cool to see how powerful Spock's
> extension mechanism is and that it actually allows you to come up with a
> mechanism to skip certain lifecycle methods based on annotations.
> 
> 
> FWIW, your solution could probably be slightly improved by changing the
> extension to be a global one with the following implementation of
> IGlobalExtension.visitSpec():
> 
> void visitSpec(SpecInfo spec) {
> def featuresWithSkippedSetup = spec.allFeatures.findAll {
> it.getAnnotation(SkipSetup) }
> if (featuresWithSkippedSetup) {
> def interceptor = new SkipSetupMethodInterceptor(skippedFeatures:
> featuresWithSkippedSetup*.name)
> spec.addSetupInterceptor(interceptor)
> }
> }
> 
> Marcin
> 
> 
> On Sun, May 3, 2020 at 5:22 PM Alexander Kriegisch
> mailto:alexan...@kriegisch.name>
> > wrote:
> 
>> 
>> Hi Jeremy.
>> 
>> I agree with Marcin in everything he says. You really should refactor the
>> way he suggested. And as he also said the first time he answered your
>> question, this is a Spock rather than a Geb question.
>> 
>> But for what it is worth, I was a little bit bored just now and wanted to
>> find out if an annotation-driven extension
>> 
>> could solve the problem. Actually to quickly hack
>> 
>>  an annotation @SkipSetup which you can add to any feature method and
>>  a SkipSetupExtension extends
>>  AbstractAnnotationDrivenExtension with a
>>  visitFeatureAnnotation() method which gets triggered each time an
>>  annotated feature method is intercepted
>> 
>> 
>> is a matter of two minutes.
>> 
>> The complication here is the fact that actually nothing specific should
>> happen in the annotated method itself but in the feature's setup() method
>> which gets executed at another time (later, more precisely). So we have
>> to find a way to communicate to the setup() method that we want to skip
>> it. The way I did it is a bit contrived, but easy enough to implement:
>> 
>>  The first time our annotation-driven extension gets triggered (i.e. the
>>  first time Spock finds a feature method annotated by @SkipSetup), we
>>  create a special method interceptor which only intercepts setup()
>>  methods.
>>  We add that interceptor instance to the SpecInfo and also save a
>>  reference to it in the SkipSetupExtension in order to avoid creating a
>>  new interceptor each time we meet a @SkipSetup annotation. We only want
>>  to do that once.
>>  The interceptor itself gets a List skippedFeatures property
>>  which we update with each newly found feature method name carrying the
>>  marker annotation.
>>  During runtime the interceptor's interceptSetupMethod() method checks if
>>  it finds the current feature method name in its skippedFeatures list.
>>  Only if it does not, it proceeds to the setup method, otherwise it skips
>>  by just doing nothing.
>> 
>> 
>> Here is the source code for
>> 
>>  the annotation,
>>  the extension,
>>  the interceptor and
>>  two almost identical test classes (because we want to check if really
>>  one extension instance is created per specification so as not to get
>>  into trouble with our internal references) in order to check if it works
>>  as expected:
>> 
>> 
>> package de.scrum_master.testing
>> 
>> import org.spockframework.runtime.extension.ExtensionAnnotation
>> 
>> import java.lang.annotation.Retention
>> import java.lang.annotation.Target
>> 
>> import static java.lang.annotation.ElementType.METHOD
>> import static java.lang.annotation.RetentionPolicy.RUNTIME
>> 
>> @Retention(RUNTIME)
>> @Target(METHOD)
>> @ExtensionAnnotation(SkipSetupExtension)
>> @interface SkipSetup {}
>> 
>> package de.scrum_master.testing
>> 
>> import
>> org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension
>> import org.spockframework.runtime.model.FeatureInfo
>> 
>> class SkipSetupExtension extends
>> AbstractAnnotationDrivenExtension {
>> SkipSetupMethodInterceptor interceptor
>> 
>> @Override
>> void visitFeatureAnnotation(SkipSetup annotation, FeatureInfo feature) {
>> if (!interceptor) {
>> interceptor = new SkipSetupMethodInterceptor()
>> feature.spec.addSetupInterceptor interceptor
>> }
>> interceptor.skippedFeatures << feature.name
>>  
>> }

Re: [geb-user] How to retrieve system property within GebConfig.groovy when declared in a Gradle task

2020-05-10 Thread Marcin Erdmann
Yeah, getting a confirmation that what you are expecting to work should
work usually helps a great deal because if it doesn't then you can be
certain that it's because of some mistake on your part. I'm glad that it
works for you now, Lee.

Marcin

On Thu, May 7, 2020 at 3:56 PM Lee  wrote:

> Hi Marcin,
>
> I totally redid everything so I could try and reproduce the issue and get
> more error details. And of course, everything worked this time as
> expected.  ;-)
>
> I must have mis-typed one little thing someplace that caused the issue
> initially.
>
> It helped to know that this 'should' work though.
>
> Thanks again for taking a look and responding.
>
> Take care,
>
> Lee
>
>
> On Wednesday, April 29, 2020 at 8:08:11 PM UTC-5, Lee wrote:
>>
>> Thanks for responding Marcin.
>>
>> Unfortunately, I had to move forward with a proof of concept for this and
>> ended up implementing it a very ugly way for now.  I don't have the
>> original results available to provide here.  I will circle back around to
>> this and re-create what I did before and update this with the results in
>> the near future after I finish a higher priority issue.
>>
>> For what it's worth, it seemed like it just couldn't find "
>> http://localhost:/wd/hub; to execute against when I set '
>> standAloneURL' equal to that via the system property 'remoteUrl' that I
>> set in the Gradle task.
>>
>> But, if I hard-coded it in the gebconfig file it did work though.  Like
>> this:
>> standAloneURL = "http://localhost:/wd/hub;
>>
>> environments {
>>  chrome {
>>driver = {
>>  chromeOptions.addArguments("--window-size=1920,1080")
>>  chromeOptions.addArguments("--headless")
>>  chromeOptions.addArguments("--disable-gpu")
>>  new RemoteWebDriver(new URL(standAloneURL), chromeOptions)
>>}
>>   }
>> }
>>
>> Again, I'll try to re-create this soon so I can provide better info.
>>
>> Thanks much,
>>
>> Lee
>>
>> On Wednesday, April 29, 2020 at 4:54:51 PM UTC-5, Marcin Erdmann wrote:
>>>
>>> Hi Lee,
>>>
>>> The configuration you described looks correct to me and it should work
>>> as far as I can tell. The geb.env system property seems to be set because
>>> configuration for chrome environment is used so I don't see a reason why
>>> another property, set in the same way on the same gradle test task would
>>> not be picked up in your geb config script.
>>>
>>> When you say that:
>>>
>>> > This always fails though as it seems to not be able to get the
>>> property when it's declared in the Gradle task.
>>>
>>> how exactly does this failure manifest?
>>>
>>> Cheers,
>>> Marcin
>>>
>>> On Fri, Apr 24, 2020 at 12:55 AM Lee  wrote:
>>>
 I've created a Gradle file where I define a task to execute Geb tests
 pretty much as follows:

 task gebTest(type: Test {
  include 'somePathToTests'
  systemProperty 'geb.env', 'chrome'
 )

 I have the 'chrome' environment configured in my GebConfig.groovy file
 and that works fine.

 What I'd like to do is define a new system property in the Gradle task
 and be able to use that from within the GebConfig file.

 Example of modified Gradle task with new 'remoteUrl' system property
 declaration:

 task gebTest(type: Test {
   include 'somePathToTests'
   systemProperty 'geb.env', 'chrome'
   systemProperty 'remoteUrl', "http://localhost:/wd/hub;
 )



 How I'm trying to use it in the GebConfig file:

 standAloneURL = System.getProperty('remoteUrl')​​​

 environments {
  chrome {
driver = {
  chromeOptions.addArguments("--window-size=1920,1080")
  chromeOptions.addArguments("--headless")
  chromeOptions.addArguments("--disable-gpu")
  new RemoteWebDriver(new URL(standAloneURL), chromeOptions)
}
   }
 }

 This always fails though as it seems to not be able to get the property
 when it's declared in the Gradle task.

 But, if I update the GebConfig file by hard coding the 'standAloneURL'
 to be what I want, it works fine.

 Example:
 standAloneURL = "http://localhost:/wd/hub;


 This is confusing me as setting the system property 'geb.env' in my
 Gradle task works just fine.

 Are there any suggestions as to what I may be doing wrong?

 --
 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 geb-...@googlegroups.com.
 To view this discussion on the web visit
 https://groups.google.com/d/msgid/geb-user/49e631e5-d019-4a9b-81b6-1a677e106a82%40googlegroups.com
 
 .

>>> --
> You received this message because you are subscribed to the Google 

Re: [geb-user] Ability to skip setup() for certain tests

2020-05-10 Thread Marcin Erdmann
Thanks for sharing, Alexander. It's cool to see how powerful Spock's
extension mechanism is and that it actually allows you to come up with a
mechanism to skip certain lifecycle methods based on annotations.

FWIW, your solution could probably be slightly improved by changing the
extension to be a global one with the following implementation of
IGlobalExtension.visitSpec():

void visitSpec(SpecInfo spec) {
def featuresWithSkippedSetup = spec.allFeatures.findAll {
it.getAnnotation(SkipSetup) }
if (featuresWithSkippedSetup) {
def interceptor = new
SkipSetupMethodInterceptor(skippedFeatures: featuresWithSkippedSetup*.name)
spec.addSetupInterceptor(interceptor)
}
}

Marcin


On Sun, May 3, 2020 at 5:22 PM Alexander Kriegisch 
wrote:

> Hi Jeremy.
>
> I agree with Marcin in everything he says. You really should refactor the
> way he suggested. And as he also said the first time he answered your
> question, this is a Spock rather than a Geb question.
>
> But for what it is worth, I was a little bit bored just now and wanted to
> find out if an annotation-driven extension
> 
> could solve the problem. Actually to quickly hack
>
>- an annotation @SkipSetup which you can add to any feature method and
>- a SkipSetupExtension extends
>AbstractAnnotationDrivenExtension with a
>visitFeatureAnnotation() method which gets triggered each time an
>annotated feature method is intercepted
>
> is a matter of two minutes.
>
> The complication here is the fact that actually nothing specific should
> happen in the annotated method itself but in the feature's setup() method
> which gets executed at another time (later, more precisely). So we have to
> find a way to communicate to the setup() method that we want to skip it.
> The way I did it is a bit contrived, but easy enough to implement:
>
>- The first time our annotation-driven extension gets triggered (i.e.
>the first time Spock finds a feature method annotated by @SkipSetup),
>we create a special method interceptor which only intercepts setup()
>methods.
>- We add that interceptor instance to the SpecInfo and also save a
>reference to it in the SkipSetupExtension in order to avoid creating a
>new interceptor each time we meet a @SkipSetup annotation. We only
>want to do that once.
>- The interceptor itself gets a List skippedFeatures property
>which we update with each newly found feature method name carrying the
>marker annotation.
>- During runtime the interceptor's interceptSetupMethod() method
>checks if it finds the current feature method name in its
>skippedFeatures list. Only if it does not, it proceeds to the setup
>method, otherwise it skips by just doing nothing.
>
> Here is the source code for
>
>- the annotation,
>- the extension,
>- the interceptor and
>- two almost identical test classes (because we want to check if
>really one extension instance is created per specification so as not to get
>into trouble with our internal references) in order to check if it works as
>expected:
>
> --
>
> package de.scrum_master.testing
>
> import org.spockframework.runtime.extension.ExtensionAnnotation
>
> import java.lang.annotation.Retention
> import java.lang.annotation.Target
>
> import static java.lang.annotation.ElementType.METHOD
> import static java.lang.annotation.RetentionPolicy.RUNTIME
>
> @Retention(RUNTIME)
> @Target(METHOD)
> @ExtensionAnnotation(SkipSetupExtension)
> @interface SkipSetup {}
>
> --
>
> package de.scrum_master.testing
>
> import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension
> import org.spockframework.runtime.model.FeatureInfo
>
> class SkipSetupExtension extends AbstractAnnotationDrivenExtension 
> {
>   SkipSetupMethodInterceptor interceptor
>
>   @Override
>   void visitFeatureAnnotation(SkipSetup annotation, FeatureInfo feature) {
> if (!interceptor) {
>   interceptor = new SkipSetupMethodInterceptor()
>   feature.spec.addSetupInterceptor interceptor
> }
> interceptor.skippedFeatures << feature.name
>   }
> }
>
> --
>
> package de.scrum_master.testing
>
> import org.spockframework.runtime.extension.AbstractMethodInterceptor
> import org.spockframework.runtime.extension.IMethodInvocation
>
> class SkipSetupMethodInterceptor extends AbstractMethodInterceptor {
>   List skippedFeatures = new LinkedList<>()
>
>   @Override
>   void interceptSetupMethod(IMethodInvocation invocation) throws Throwable {
> if (!skippedFeatures.contains(invocation.feature.name))
>   invocation.proceed()
>   }
> }
>
> --
>
> package de.scrum_master.testing
>
> import spock.lang.Specification
>
> class SkipSetupTestA extends Specification {
>   def