Tuesday, September 30, 2014

SoapUI Groovy Set Headers to Test Steps

During the development of my project we had to develop multiple REST endpoints.

A call to each of these REST endpoints needed to be authenticated by passing an oauth-token in their header.

Post development we had to change the core REST endpoints code to incorporate design changes.

In such situations it is necessary to do a quick regression test before pushing the code to QA.

In the following groovy snippet in soap ui we do the following:

1. Get the oauth token from the first test step
2. Parse the JSON reponse (refer to my previous post)
3. Iterate through each of the test steps and set the headers for each of them


import groovy.json.JsonSlurper;
import com.eviware.soapui.support.types.StringToStringMap;
//get the response from the test step that makes the call to get the authentication token
def authResponse = testRunner.testCase.getTestStepByName("GetToken").getPropertyValue("response");
//parse the json response
def authResponseJSON = new JsonSlurper().parseText(authResponse);
//access the access_token property in the respose
def oauthToken = authResponseJSON.access_token;
//create a new empty map for headers
def headers = new StringToStringMap();
//create a key value pair in the headers mao
headers.put("OAuth-Token", oauthToken);
//obtain the list of test steps
def steps = context.testCase.getTestStepList();
//iterate thru each of the test steps and set the oauth token to each of the steps
steps.each{
// This block will loop through the test steps and set headers for each of them
if (it.name != 'GetToken' && it.name != 'TransferOauthToken')
it.testRequest.setRequestHeaders(headers);
}

SoapUI Groovy parse json

I came across a situation where I had to use groovy scripts in to test some REST endpoints that return JSON responses.

So following is a groovy code snippet which does the following:
1. Import the json parsing library
2. Fetch the JSON response from a test step
3. Parse the JSON response
4. Access properties in the JSON response

//import library to parse JSON
import groovy.json.JsonSlurper;
//get the JSON response from the test step that makes the call to some test step to get a JSON repsonse
def response = testRunner.testCase.getTestStepByName("TestStepName").getPropertyValue("response");
//parse the json response
def responseJSON = new JsonSlurper().parseText(response);
//access the propertyname
def property = responseJSON.propertyname;

Wednesday, September 24, 2014

Selenium Web Driver node.js set chrome options

The following code snippet demonstrates on how to set command line options to the chrome webdriver in selenium.

Here we are using the selenium-webdriver npm available for node.js
//import the selenium web driver
var webdriver = require('selenium-webdriver');
var chromeCapabilities = webdriver.Capabilities.chrome();
//setting chrome options to start the browser fully maximized
var chromeOptions = {
'args': ['--test-type', '--start-maximized']
};
chromeCapabilities.set('chromeOptions', chromeOptions);
var driver = new webdriver.Builder().withCapabilities(chromeCapabilities).build();