Common tasks that you can perform with the Groovy Script test step
https://support.smartbear.com/readyapi/docs/soapui/steps/groovy.html
Get test case object
To obtain the object which refers to the containing test case, use the following code snippet:
By using the testCase object, you can access and manipulate test items of the project.
Fail the test run
There are two ways to fail the test run from your scripts:
To fail only the Groovy Script test step, throw an exception in your script:
throw new Exception("Result not as expected!")If the Abort test if an error occurs option is enabled in TestCase Options, the test will stop. Otherwise, it will continue.
To fail the entire test run, use the
testRunner.failmethod. This marks the test failed and stops the test run regardless of the Abort test if an error occurs option.testRunner.fail("Result not as expected!")
Stop test execution
The testRunner scripting object has two methods to stop the current test run:
cancel(String reason)– Stops the test run and marks it as Canceled. The string argument specifies the reason.fail(String reason)– Stops the test run and marks it as Fail. The string argument specifies the reason.
Run test step by name
You can run any test step in the current test case. To do this, use the runTestStepByName method of the testRunner object. This method runs the specified test step and returns the result. For example, the following code snippet runs ten random requests before executing the remaining script:
// Run ten random requests
for( i in 1..10 )
{
if( Math.random() > 0.5 )
testRunner.runTestStepByName( "Request 1")
else
testRunner.runTestStepByName( "Request 2")
}
// Do something else
Create context related property
The following code snippet creates a foo property that will be available in another groovy script:
The typical scenario is to save the needed counters and collections to the context and use them to control the test flow as required.
Branch test case
By using the gotoStepByName method of the testRunner object, you can command ReadyAPI to jump the test execution to the specified test step after the script has finished. For example the following script randomly selects the next test step:
if( Math.random() > 0.5 )
testRunner.gotoStepByName( "Request 1")
else
testRunner.gotoStepByName( "Request 2")
// do something else before executing one of the requests
Create an assertion
To create an assertion:
Obtain a test step by using the
getTestStepByNamemethod of thetestCaseobject.Use the
addAssertionmethod to create an assertion.Specify the assertion name as a string.
ReadyAPI will create a new assertion with the specified name and default settings.
Note: If you already have an assertion with the same name, you will be prompted to specify the unique assertion name. The test will not continue until you close the dialog.
For example, the following code snippet shows how to create a Valid HTTP Status Codes assertion for the Test Request test step:
// Get the test step object
def ts = testRunner.testCase.getTestStepByName("Test Request")
// Add the assertion
def vas = ts.addAssertion("Valid HTTP Status Codes")
Modify assertion
You can modify the created assertion by using assertion-specific methods.
For example, to change the code checked by Valid HTTP Status Codes and Invalid HTTP Status Codes by using the setCodes method of the Assertion object.
You can get this object when creating it in the following way:
// Get the test step object
def ts = testRunner.testCase.getTestStepByName("Test Request")
// Add the assertion
def vas = ts.addAssertion("Valid HTTP Status Codes")
// Set assertion codes
vas.setCodes("200,202")
If you have already created an assertion, you can access it in the following way:
// Get the test step object
def ts = testRunner.testCase.getTestStepByName("Test Request")
// Search all assertions
for ( e in assertionsList)
{
// If the assertion name matches
if (e.getName() == "Valid HTTP Status Codes")
{
// Set assertion codes
e.setCodes("503, 504")
}
}
Remove assertions
To remove an assertion, use the removeAssertion method.
You need to specify the assertion object to remove.
Here is the sample code that will remove the assertion created in the example above.
// Get the test step object
def ts = testRunner.testCase.getTestStepByName("Test Request")
// Search all assertions
for ( e in assertionsList)
{
// If the assertion name matches
if (e.getName() == "Valid HTTP Status Codes")
{
// Delete the assertion
ts.removeAssertion(e)
}
}
Get property value
To get a property value:
Obtain the containing object.
Use the
getPropertyValue()method to get a property object.
For example, the following code snippet gets a test suite property:
// Get username property from the test suite object
def username = testRunner.testCase.testSuite.getPropertyValue( "Username" )
Set property value
To write a value to a property, use the setPropertyValue() method. For example, the following code snippet, specifies the Username parameter of the HTTP Request test step:
// Write the username to the HTTP Request
testRunner.testCase.testSteps["HTTP Request"].setPropertyValue( "Username", username )
Common tasks that you can perform with the Groovy Script test step的更多相关文章
- [整理归档]30 common tasks you perform using the GUI that you can do faster in Windows PowerShell
主要内容来自于 http://channel9.msdn.com/Events/TechEd/Australia/2014/DCI316 可以下载PPT以及视频,个人只是整理一下平时常用的 NetWo ...
- Update UI from an asynchronous thread
One of the most common tasks you need to perform in a Windows Phone application is updating the UI f ...
- ansible common modules
##Some common modules[cloud modules] [clustering modules] [command modules]command - executes a comm ...
- Selenium click不生效 报错selenium.common.exceptions.InvalidArgumentException
记录在使用selenium过程中踩的坑------ 在使用selenium时,用click点击网站弹出的文件上传框的"上传文件"按钮不生效,报错selenium.common.ex ...
- [转载]Java 8 日期&时间 API
Java 8 日期和时间 声明 本文转自http://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant,以mark ...
- 数据上下文【 DnContext】【EF基础系列7】
DBContext: As you have seen in the previous Create Entity Data Model section, EDM generates the Scho ...
- [asp.net core] Tag Helpers 简介(转)
原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro What are Tag Helpers? ...
- sandy bridge
SANDY BRIDGE SPANS GENERATIONS Intel Focuses on Graphics, Multimedia in New Processor Design By Li ...
- AngularJS是什么
先标明来源: https://code.angularjs.org/1.3.15/docs/guide/introduction 也就是官网针对1.3.15版的说明 What Is Angular? ...
随机推荐
- 2018面向对象程序设计(Java)第10周学习指导及要求
2018面向对象程序设计(Java)第10周学习指导及要求(2018.11.1-2018.11.4) 学习目标 理解泛型概念: 掌握泛型类的定义与使用: 掌握泛型方法的声明与使用: 掌握泛型接口的定 ...
- 1.5.7、CDH 搭建Hadoop在安装之前(定制安装解决方案---配置单用户模式)
配置单用户模式 在传统的Cloudera Manager部署中,管理每台主机上的Hadoop进程的Cloudera Manager Agent以root用户身份运行.但是,某些环境会限制对root帐户 ...
- JMeter学习(三十三)测试报告优化(转载)
转载自 http://www.cnblogs.com/yangxia-test 如果按JMeter默认设置,生成报告如下: 从上图可以看出,结果信息比较简单,对于运行成功的case,还可以将就用着.但 ...
- 截图软件FastStone
屏幕截图软件(FastStone Capture) 好用,可以粘贴 / 复制. 可以做页面设计,有屏幕标尺,取色器.
- java并发:CopyOnWriteArrayList简单理解
Java集合的快速失败机制 “fail-fast” "fail-fast"是java集合的一种错误检测机制,当多个线程对集合进行结构上的改变的操作时,有可能会产生 fail-fas ...
- laravel框架目录结构详解
- swift - xcode - pod升级版本和降级版本
1. 查看当前版本 pod --version 2.如果安装过pod,更新命令 新版 sudo gem install -n /usr/local/bin cocoapods --pre 旧版 sud ...
- linux命令学习之:top
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按键来不 ...
- Runnable如何传参
private class TimerUpdateTask implements Runnable{ private boolean isUnion = false; public TimerUpda ...
- Bootstrap(6)辅组类和响应式工具
一.辅助类 Bootstrap 在布局方面提供了一些细小的辅组样式,用于文字颜色以及背景色的设置.显示关闭图标等等. 1.情景文本颜色 各种色调的字体 <p class="text-m ...