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:

 

def case = testRunner.testCase;

 

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.fail method. 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.

 

if (testObject == null)
{
  testRunner.fail("testObject was not found") // Stops the test run
}

 
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:

 

// Script 1
context.foo = "My Value"

// Script 2
log.info(context.foo)

 

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 getTestStepByName method of the testCase object.

  • Use the addAssertion method 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的更多相关文章

  1. [整理归档]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 ...

  2. 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 ...

  3. ansible common modules

    ##Some common modules[cloud modules] [clustering modules] [command modules]command - executes a comm ...

  4. Selenium click不生效 报错selenium.common.exceptions.InvalidArgumentException

    记录在使用selenium过程中踩的坑------ 在使用selenium时,用click点击网站弹出的文件上传框的"上传文件"按钮不生效,报错selenium.common.ex ...

  5. [转载]Java 8 日期&时间 API

    Java 8 日期和时间 声明 本文转自http://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant,以mark ...

  6. 数据上下文【 DnContext】【EF基础系列7】

    DBContext: As you have seen in the previous Create Entity Data Model section, EDM generates the Scho ...

  7. [asp.net core] Tag Helpers 简介(转)

    原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro What are Tag Helpers? ...

  8. sandy bridge

      SANDY BRIDGE SPANS GENERATIONS Intel Focuses on Graphics, Multimedia in New Processor Design By Li ...

  9. AngularJS是什么

    先标明来源: https://code.angularjs.org/1.3.15/docs/guide/introduction 也就是官网针对1.3.15版的说明 What Is Angular? ...

随机推荐

  1. 运用active和hover实现导航栏的页面切换

    .nav ul li a:hover{ background: #3E6EDD;}.nav ul li a:hover img{ display: block;}.nav ul li a.active ...

  2. 新手如何学习Java——Java学习路线图

    推荐初学者阅读:新手如何学习Java——Java学习路线图

  3. session of express

    [session of express] 1.express-session 一个提供session功能库 npm install express-session --save var session ...

  4. EasyUI值的清空与获取

    清空: 一般值 $("#searchx").val(""); 时间选择框 $('#starttime').datetimebox('setValue', '') ...

  5. spring boot 中统一异常处理

    基于 spring boot 对异常处理的不友好,现在通过其他的方式来统一处理异常 步骤一:自定义异常类 public class UserNotExistException extends Runt ...

  6. jQuery权威指南(第2版) 学习一 jQuery操作DOM

    jQuery操作DOM 获取元素的属性 attr(name) 获取元素属性的语法格式如下: attr(name) 其中,参数 name 表示属性的名称. 例子: <img alt="& ...

  7. docker使用以及dockerfile编写

    一 docker常用命令 1. service docker start 2. docker images        显示所有镜像 3. docker ps [-a]          显示正在运 ...

  8. Codeforces Round #541 (Div. 2)

    Codeforces Round #541 (Div. 2) http://codeforces.com/contest/1131 A #include<bits/stdc++.h> us ...

  9. [剑指Offer]59-队列的最大值(题目二待补)

    题目一:滑动窗口的最大值 题目链接 https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788?tpId=13&tqI ...

  10. web项目no such method exception

    昨天更新包后出现这个异常,经过仔细全面排查,项目源码是没问题的. 怀疑jvm被重写了,肉眼也没找到证据.怀疑是操作系统问题,这个也不会没办法排查 于是给客户重新发了个war包,客户运行后出现 异常: ...