Search code examples
javascriptjqueryspockgeb

Spock non-initial feature methods fail when running class as a whole


I am using Geb and Spock together to test a front-end application. I am new to both of these tools. I have a Spock specification that extends GebReportingSpec.

I then have a setupSpec that logs into the application and navigates to a specific page. This is followed by various feature methods that perform actions on the webpage (such as clicking a drop down and then selecting an option).

All feature methods pass successfully when they are run by themselves. The first feature method simply selects a value from a drop down and asserts that the proper fields display as a result (the selected option controls which fields are displayed on the page). The second feature method fails to select a different option from the drop down list so the asserts for that method fail because it cannot find the fields. The option gets selected by the event of updating the new fields does not get triggered. My spec is below:

    class VerifyFields extends GebReportingSpec {
        def setupSpec() {
          // Log in
          via Dashboard
          loginForm.login("marco", "abc123")

          // Navigate to Page 1
          button1.click()
          button2.click()

          // Click Button 3
          button3.click()
    }

// This feature method passes successfully
    def "Verify Member fields exist"() {
        when: "User selects Member option for drop down 1"
        at Page1
        projSubject.click()
        projCoverMbrFields.projSubjectMbrOption.click()
        sleep(1500)

        then: "All expected member fields are displayed in the left panel"
        assert mbrName.present
    }

// This feature method fails when run after the first feature method
    def "Verify Provider fields exist"() {
        when: "User selects Provider option for subject type"
        at Page1
        projSubject.click()
        projCoverProvFields.projSubjectProvOption.click()
        sleep(1500)

        then: "All expected provider fields are displayed in the left panel"
        assert provName.present
    }

I have tried adding things to get the page to focus on the elements like the below lines:

driver.findElement(By.className("projSubject")).click()
driver.findElement(By.cssSelector(".projSubjectoption[value='Provider']")).click()

And changing the attribute to have selected="selected" with the below lines:

$("select.projSubject option[value='Provider']").jquery.attr("selected", true)
projCoverProvFields.projSubjectProvOption.jquery.attr('selected', 'selected')

But nothing I do can solve the issue where the second feature method fails to successfully trigger the drop down selection to update the fields on the page.


Solution

  • Your error could be because you are running setupSpec() which is run once per spec (rather than once per method). In order to maintain the session for all methods within the spec and run them in order you need to add the @Stepwise spock attribute the top of your class.

    @Stepwise
    class VerifyFields extends GebReportingSpec {
       ...
    }
    

    Another option, would be to use setup() rather than setupSpec(), here are the differences:

    def setup() {}          // run before every feature method
    def setupSpec() {}     // run before the first feature method
    

    By using setupSpec(), it will run before the first feature method, which is why both tests could be working individually. Try replacing setupSpec() with setup().

    Just remember that running setup() will increase your total test execution time as you will be repeating for every method. If possible i would use @Stepwise.