0

I am trying to utilise the global variables to store some data in my spock specs but not sure about the implementation. The reason for this is to utilise the values from the profile of the user and verify it at the time of check out. To be precise, I login and navigate to the user profile. I need to be able to store the address details and user details in Global variables. When I order something, I would like to verify the delivery details before checking out an item.

Is there way to utilise Global variables in When block.

An Edited example below:

Declared a global variable inside the Spock Specification (called "buyerNameValue")

And tried to store the value of an element inside that variable to be used later:

def "Store User details"() {

when:
    assert at(UserProfilePage)
    buyerNameValue      << buyerName.text() 
    println(buyerNameValue) // To see if the value is being captured
    buyerAddressValue   << buyerAddress.text()
    println(buyerAddressValue)
    landingPage.click()
then:
    assert at(LandingPage)  

}

vijay pujar
  • 1,683
  • 4
  • 19
  • 32

1 Answers1

0

I declared the global variables but when I tried to fetch the values from an element it was throwing an error stating the assignment operator is not allowed in when block.

Also I couldn't use the <<operator either. These are only allowed in the where block of Spock spec.

New Alexandria
  • 6,951
  • 4
  • 57
  • 77
  • Please see the example added – vijay pujar Nov 05 '13 at 21:42
  • I get the following error: java.lang.NullPointerException: Cannot invoke method leftShift() on null object at login.tests.DetailsSpec.Store User details(B_LoginSpec.groovy:71) – vijay pujar Nov 05 '13 at 21:43
  • 1
    You can declare a `@Shared` field in the class, then use `=` to assign it. If the test methods are dependent, you should annotate the class with `@spock.lang.Stepwise`. – Peter Niederwieser Nov 12 '13 at 15:14
  • Thanks very much Peter. I just realized that the "<<" can only be used in the where block for iteration. The "=" was all I needed. Thanks again. – vijay pujar Nov 20 '13 at 11:18