Using Context in Soap-UI/Ready API External Script Library

In the recent post we have seen how to use the external script library to perform a session id extraction from an application.

One interesting thing I recently come across is, we can also use soap-UI context in our external script library.

Context is a built-in variable automatically made available by soapUI.

You can able to define/add your own properties of context objects
For those who are familiar with writing groovy test steps in Soap-UI definitely should heard of this term before.

//An example to set property and get property using soap-UI context.
context.setProperty("URL", "")"URL"))

Lets see how this is going to be useful in our tests.

Let’s consider the same script that I have used before in my previous post
In that, have a look at the code written in “The Script Library to get the SessionID

The method session contains three parameters. URL, Username and Password.

If you are going to call this session method from inside your Soap-UI, you will need to pass the value for these three parameters.

Obviously, Things like URL, Username and Password will be usually stored in a Project Level Property to make it easy for configuration.

So you have to first get these three Project Level Property and then you have to call this external script library.

So if you have a close look at the section “How do we Call this Script inside our SoapUI test cases?” in this Post,

You can able to see that we have extracted the project level property in a variable and then we have used these variables as a parameter in when calling the session method.

def groovyUtils = new context );
String url = context.expand( '${#Project#AuthenticateRequestURL}' )
String username = "yourusername";
String password = "yourpassword";
Obj = new Headlessdriver();
String session = Obj.session(url, username, password);

Do we really need to do this every time when we call the external script library? Answer is NO.

We can still use the ‘context’ variable in our external script library also. So I have updated the Headlessdriver.groovy as follows.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;

class Headlessdriver {

	String url;
	String username;
	String password;
	String phantomPath;

	def Headlessdriver( context ) {        
        this.url = context.expand( '${#Project#URL}' );
		this.username = context.expand( '${#Project#username}' );
		this.password = context.expand( '${#Project#password}' );
		this.phantomPath = context.expand( '${#Project#phantomPath}' );

	String session () {

		DesiredCapabilities caps = new DesiredCapabilities();
		// caps.setCapability("phantomjs.binary.path", "ext\\phantomjs.exe");
		caps.setCapability("phantomjs.binary.path", phantomPath);
		String[] args = [ "--ignore-ssl-errors=yes" ];
		caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, args);
		WebDriver driver = new PhantomJSDriver(caps);
		driver.manage().timeouts().implicitlyWait(60L, TimeUnit.SECONDS);
		String sessionIdFull = driver.manage().getCookieNamed("sessionId").toString();
		String SessionId = sessionIdFull.split("; ")[0];
		return SessionId;

Now you don’t need to extract the project level property in your groovy script test step before calling this external script library.
Calling this Headlessdriver.session() method is simple as below.

Obj = new Headlessdriver( context );
String session = Obj.session();

6 thoughts on “Using Context in Soap-UI/Ready API External Script Library

  1. If you are using DataBase Containers in SoapUI, to hold some list of connection strings mapped against the physical names, then you can create a external groovy script like below.

    //External Scripts Starts Here – DatabaseHandler.Groovy
    import groovy.sql.GroovyRowResult;
    import groovy.sql.Sql;

    class DatabaseHandler {

    GroovyUtils utils;
    GroovyUtilsPro proUtils;
    Sql sqlConn;
    String basePath;

    //Pass the Database Physical Name from SoapUI to this Script
    def DatabaseHandler( String dbName, context ) {
    utils = new GroovyUtils( context );
    utils.registerJdbcDriver( “” );
    proUtils = new GroovyUtilsPro( context );
    sqlConn = proUtils.getGroovySql(dbName);
    //this.basePath = context.expand(‘${projectDir}’) + “\\Scripts\\”;

    def ExecuteQuery (String Query) {
    //External Script Ends Here – DatabaseHandler.Groovy

    Then from your SoapUI, you can create a Groovy Test Step and run a sample query like below.

    Obj = new DatabaseHandler(“MY_Physical_DB_Name”, context);
    ver = Obj.ExecuteQuery(“DELETE FROM MyTable”)

    Hope this helps . .



  2. hey,
    Bumped into this article and have got a question for you- how do you solve the problem where the external script makes a JDBC connection is databases using the physical name of the connection for which connection string is defined in the soapui tool. how can I pass the JDBC connection in the context.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s