cancel
Showing results for 
Search instead for 
Did you mean: 

[TechCorner Challenge #11] Change Socket Timeout for HTTP requests using Groovy Script

SOLVED
Highlighted
Community Manager

[TechCorner Challenge #11] Change Socket Timeout for HTTP requests using Groovy Script

Hi Community!🙂

Here's a new task for you to practice your skills of using the ReadyAPI tool.  Check out the participation rules and TechCorner Leaderboard here.

 

ReaydAPI allows you to set a global socket timeout value for HTTP requests through its preferences, however, there are times where this is not enough.

Some specific requests may require a different value to better suit your expectations for the test. In these cases, you can use a Groovy script to change the socket timeout value then change it back to its original value.

 

Task: Write a Groovy Script that will set socket timeout to a new value, then return it to the default value.

DifficultyStar_Gold.pngStar_gray.pngStar_gray.png

 

Good luck😊


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Tags (1)
2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Community Hero

Change Socket Timeout for HTTP requests using Groovy Script

Task: Write a Groovy Script that will set socket timeout to a new value, then return it to the default value.

 

This is a solution created for [TechCorner Challenge #11]

 

Here's what I managed to come up with 🙂

 

 

import com.eviware.soapui.SoapUI;
import com.eviware.soapui.settings.HttpSettings

// Get test step reference.
def testStep = context.testCase.testSteps["TestStep"];

// Get the settings of the test step.
def tsSettings =  testStep.getSettings();

// Get the value of the current time out.
def currentTimeOut = tsSettings.getString(HttpSettings.SOCKET_TIMEOUT, "");

// Adjust the timeout.
def newTimeOut = (currentTimeOut.toInteger() * 2).toString();

// Show the current timeout value.
log.info("The current Socket Timeout value is: " + tsSettings.getString(HttpSettings.SOCKET_TIMEOUT, ""));

// Set to the new timeout.
tsSettings.setString(HttpSettings.SOCKET_TIMEOUT, newTimeOut);

// Show the timeout value has been adjusted.
log.info("The adjusted Socket Timeout value is: " + tsSettings.getString(HttpSettings.SOCKET_TIMEOUT, ""));

// Revert back to original timeout.
tsSettings.setString(HttpSettings.SOCKET_TIMEOUT, currentTimeOut);

// Show the timeout value is the original value. 
log.info("Adjusted back to original timeout: " + tsSettings.getString(HttpSettings.SOCKET_TIMEOUT, ""));

 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

Tags (1)
Highlighted
Community Leader

Change Socket Timeout for HTTP requests using Groovy Script

Task: Write a Groovy Script that will set socket timeout to a new value, then return it to the default value.

 

This is a solution created for [TechCorner Challenge #11]

 

Hi @sonya_m :

 

I have came across two below solutions:

 

1. As the challenge states to change particular Test Step Setting, below groovy can be useful:

 

 

import com.eviware.soapui.SoapUI
import com.eviware.soapui.settings.HttpSettings

def testStep = testRunner.testCase.getTestStepByName("TEST__STEP__NAME");
def testSettings = testStep.getSettings();
def oldTime = testSettings.getString(HttpSettings.SOCKET_TIMEOUT,"");
log.info "OLD TIMEOUT :: "+oldTime
//setting new timeout
testSettings.setString(HttpSettings.SOCKET_TIMEOUT,"120000");
//save the settings
SoapUI.saveSettings();

log.info "NEW TIMEOUT :: "+testSettings.getString(HttpSettings.SOCKET_TIMEOUT,"");
//revert back old timeout
testSettings.setString(HttpSettings.SOCKET_TIMEOUT,oldTime);

log.info "REVERT BACK TO OLD TIMEOUT :: "+testSettings.getString(HttpSettings.SOCKET_TIMEOUT,"");
//save the settings
SoapUI.saveSettings();

 

 

 

2. If we want to modify global setting instead of particular test step setting, below groovy can be useful:

 

 

import com.eviware.soapui.SoapUI
import com.eviware.soapui.settings.HttpSettings

//Get soapUI settings from preferences > HTTP Settings > Socket Timeout (ms)
def settings = SoapUI.getSettings()
//Set New time
settings.setString(HttpSettings.SOCKET_TIMEOUT,"120000")
//save the settings
SoapUI.saveSettings();

 

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

View solution in original post

Tags (1)
3 REPLIES 3
Highlighted
Community Hero

Change Socket Timeout for HTTP requests using Groovy Script

Task: Write a Groovy Script that will set socket timeout to a new value, then return it to the default value.

 

This is a solution created for [TechCorner Challenge #11]

 

Here's what I managed to come up with 🙂

 

 

import com.eviware.soapui.SoapUI;
import com.eviware.soapui.settings.HttpSettings

// Get test step reference.
def testStep = context.testCase.testSteps["TestStep"];

// Get the settings of the test step.
def tsSettings =  testStep.getSettings();

// Get the value of the current time out.
def currentTimeOut = tsSettings.getString(HttpSettings.SOCKET_TIMEOUT, "");

// Adjust the timeout.
def newTimeOut = (currentTimeOut.toInteger() * 2).toString();

// Show the current timeout value.
log.info("The current Socket Timeout value is: " + tsSettings.getString(HttpSettings.SOCKET_TIMEOUT, ""));

// Set to the new timeout.
tsSettings.setString(HttpSettings.SOCKET_TIMEOUT, newTimeOut);

// Show the timeout value has been adjusted.
log.info("The adjusted Socket Timeout value is: " + tsSettings.getString(HttpSettings.SOCKET_TIMEOUT, ""));

// Revert back to original timeout.
tsSettings.setString(HttpSettings.SOCKET_TIMEOUT, currentTimeOut);

// Show the timeout value is the original value. 
log.info("Adjusted back to original timeout: " + tsSettings.getString(HttpSettings.SOCKET_TIMEOUT, ""));

 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

Tags (1)
Highlighted
Community Leader

Change Socket Timeout for HTTP requests using Groovy Script

Task: Write a Groovy Script that will set socket timeout to a new value, then return it to the default value.

 

This is a solution created for [TechCorner Challenge #11]

 

Hi @sonya_m :

 

I have came across two below solutions:

 

1. As the challenge states to change particular Test Step Setting, below groovy can be useful:

 

 

import com.eviware.soapui.SoapUI
import com.eviware.soapui.settings.HttpSettings

def testStep = testRunner.testCase.getTestStepByName("TEST__STEP__NAME");
def testSettings = testStep.getSettings();
def oldTime = testSettings.getString(HttpSettings.SOCKET_TIMEOUT,"");
log.info "OLD TIMEOUT :: "+oldTime
//setting new timeout
testSettings.setString(HttpSettings.SOCKET_TIMEOUT,"120000");
//save the settings
SoapUI.saveSettings();

log.info "NEW TIMEOUT :: "+testSettings.getString(HttpSettings.SOCKET_TIMEOUT,"");
//revert back old timeout
testSettings.setString(HttpSettings.SOCKET_TIMEOUT,oldTime);

log.info "REVERT BACK TO OLD TIMEOUT :: "+testSettings.getString(HttpSettings.SOCKET_TIMEOUT,"");
//save the settings
SoapUI.saveSettings();

 

 

 

2. If we want to modify global setting instead of particular test step setting, below groovy can be useful:

 

 

import com.eviware.soapui.SoapUI
import com.eviware.soapui.settings.HttpSettings

//Get soapUI settings from preferences > HTTP Settings > Socket Timeout (ms)
def settings = SoapUI.getSettings()
//Set New time
settings.setString(HttpSettings.SOCKET_TIMEOUT,"120000")
//save the settings
SoapUI.saveSettings();

 

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

View solution in original post

Tags (1)
Highlighted
Community Manager

Re: [TechCorner Challenge #11] Change Socket Timeout for HTTP requests using Groovy Script

@msiadak Thank you, that was a really fast answer! And, it works great, too.

 

@HimanshuTayal Awesome! Thank you for providing an alternative solution!

 


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors