The following examples are mainly used to store data in files and Send HTTP request. This page will be updated regularly to provide more examples as we grow the Magellan Community.
How to store data on a txt file and append to the file each time you run the test
@And("^I store data (\\S+) on file (\\S+)\$")
@Documentation(value = "Store data into a file", link = "logonAsCatapult")
public static void storeData(String data, String file) {
def newFile = new File(file)
newFile.append(data + '\n')
}
From the Feature file calls the Step as:
And I store data Joe Doe on file C:/Temp/test.txt
How to send a HTTP request through (POST)
@And("^I post in url (\\S+) with data (\\S+)\$")
@Documentation(value = "Post request with parameters", link = "postParameter")
public void sendRequest(String urlString, String paramString) {
def postmanPost = new URL(urlString)
def postConnection = postmanPost.openConnection()
postConnection.requestMethod = 'POST'
assert postConnection.responseCode == 200
def form = paramString
postConnection.doOutput = true
def text
postConnection.with {
outputStream.withWriter { outputStreamWriter ->
outputStreamWriter << form
}
text = content.text
}
assert postConnection.responseCode == 200
postConnection.close()
}
From the Feature file call the Step as:
Given I post in url https://postman-echo.com/post with data param1=parameter
How to get HTTP request through (GET)
@Given("^I request from url (\\S+) and confirm response code is (\\S+)\$")
@Documentation(value = "Get request and expect Code", link = "getParameter")
public void getRequest(String urlString, int code) {
def postmanGet = new URL(urlString)
def getConnection = postmanGet.openConnection()
getConnection.requestMethod = 'GET'
assert getConnection.responseCode == code
}
From the Feature file call the Step as:
Given I request from url https://postman-echo.com/get and confirm response code is 200
Example of How Magellan will show the error:

0

