arttuladhar
6/12/2017 - 10:06 PM

REST API Testing Using Spock

REST API Testing Using Spock

package specs

import geb.spock.GebReportingSpec

class GebDemoSpec extends GebReportingSpec {

    def "Testing Basic Page Contents"(){
        setup:
        go "http://localhost:9000/#/demo/geb-demo"

        expect:
        //Asserting Basic CSS Selector
        $("h2").text() == "Rock and Roll Hall of Fame"

        //Using Index
        $("h3", 0).text() == "Better than the Best"
        $("h3", 1).text() == "Subscribe Newsletter"

        //Finding and Filtering Descendants
        $("div.col-md-4", 0).find("strong").text() == "Top Songs List"
        $("div.panel-heading").has("strong").size() == 3;

    }

    def "Testing Form Interaction with Valid Email and CheckBox Checked"(){

        setup:
        go "http://localhost:9000/#/demo/geb-demo"

        when: "User Enters Valid Email"
        $("#exampleInputEmail").value("hellouser@gmail.com");
        $("input", type: 'checkbox',  name: "termsAndConditions").value(true)

        and: "Clicks Submit Button"
        $("button", type: "submit").click();

        then: "Redirects to Digi Marketplace Main Homepage"
        $("h2").text() == "Digi Marketplace";

    }

    def "Testing Form Interaction with InValid Email"(){

        setup:
        go "http://localhost:9000/#/demo/geb-demo"

        when: "User Enters Valid Email"
        $("#exampleInputEmail").value("hellouser.com");

        and: "Clicks Submit Button"
        $("button", type: "submit").click();

        then: "Should Remain in Same Page"
        $("h2").text() == "Rock and Roll Hall of Fame";

    }


}
package specs.api

import groovyx.net.http.RESTClient
import spock.lang.Specification

class StudentRESTApiSpec extends Specification{
    static String testURL = "http://localhost:3000"

    RESTClient restClient = new RESTClient(testURL)

    def 'User Should be able to perform Create Request'(){

        given :
        def requestBody = [firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com']

        when:
        def response = restClient.post(path: '/students', body: requestBody, requestContentType: 'application/json')
        def testUserId = response.responseData

        then:
        response.status == 200

        cleanup:
        deleteTestUser(testUserId)
    }

    def 'User should be able to perform Read Request'(){
        setup:
        def testUserId = createTestUser().responseData

        when:
        def response = restClient.get( path: '/students')

        then:
        response.status == 200

        and:
        response.responseData.id
        response.responseData.firstName
        response.responseData.lastName

        cleanup:
        deleteTestUser(testUserId)

    }

    def 'User should be able to perform Update Request'(){
        setup:
        def testUserId = createTestUser().responseData

        when:
        def updatedUser = [firstName: 'Doe', lastName: 'John', email: 'doe.john@gmail.com']
        def response = restClient.put(path: '/students/' + testUserId, body: updatedUser, requestContentType: 'application/json')

        then:
        response.status == 200

        cleanup:
        deleteTestUser(testUserId)
    }

    def 'User should be able to perform Delete Request'(){
        setup:
        def testUserId = createTestUser().responseData

        when:
        def response = restClient.delete(path: '/students/' + testUserId)

        then:
        response.status == 200

    }


    def createTestUser() {
        def requestBody = [firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com']
        return restClient.post(path: '/students', body: requestBody, requestContentType: 'application/json')
    }

    def deleteTestUser(def userId) {
        return restClient.delete(path: '/students/' + userId)

    }

}