Skip to content

Instantly share code, notes, and snippets.

@megabites2013
Forked from arttuladhar/GebDemoSpec.groovy
Created October 1, 2018 22:03
Show Gist options
  • Save megabites2013/10269437691c40b80f7de6fef1bf8b82 to your computer and use it in GitHub Desktop.
Save megabites2013/10269437691c40b80f7de6fef1bf8b82 to your computer and use it in GitHub Desktop.
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("[email protected]");
$("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: '[email protected]']
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: '[email protected]']
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: '[email protected]']
return restClient.post(path: '/students', body: requestBody, requestContentType: 'application/json')
}
def deleteTestUser(def userId) {
return restClient.delete(path: '/students/' + userId)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment