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:

Groovy

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.

Groovy

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:

Groovy

// 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:

Groovy

// 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:

Groovy

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:

Groovy

// 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:

Groovy

// 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:

Groovy

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

Groovy

// 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:

Groovy

// 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:

Groovy

// Write the username to the HTTP Request
testRunner.testCase.testSteps["HTTP Request"].setPropertyValue( "Username", username )

原文地址:https://www.cnblogs.com/MasterMonkInTemple/p/10439192.html

时间: 2024-08-08 10:53:26

Common tasks that you can perform with the Groovy Script test step的相关文章

ansible安装配置与简单使用

前言: AnsibleWorks成立于2012年,由自动化工具Cobbler及Func的开发者Michael DeHaan创建.其Ansible平台是一个开源的配置及计算机管理平台.可实现多节点的软件部署,执行特定任务并进行配置管理. Ansible 跟其他IT自动化技术的区别在于其关注点并非配置管理.应用部署或IT流程工作流,而是提供一个统一的界面来协调所有的IT自动化功能,因此 Ansible的系统更加易用,部署更快.受管理的节点无需安装额外的远程控制软件,由平台通过SSH(Secure S

[email protected]一个高效的配置管理工具--Ansible configure management--翻译(七)

如无书面授权,请勿转载 Larger Projects Until now, we have been looking at single plays in one playbook file. This approach will work for simple infrastructures, or when using Ansible as a simple deployment mechanism. However, if you have a large and complicated

addddd

Virtual Files A virtual file com.intellij.openapi.vfs.VirtualFile is the IntelliJ Platform's representation of a file in a file system (VFS). Most commonly, a virtual file is a file in your local file system. However, the IntelliJ Platform supports m

github上所有大于800 star OC框架

https://github.com/XCGit/awesome-objc-frameworks#awesome-objc-frameworks awesome-objc-frameworks ID Framework Images 1 AFNetworking/AFNetworking 19,058 A delightful iOS and OS X networking framework 2 rs/SDWebImage 10,139 Asynchronous image downloade

AngularJS是什么

先标明来源: https://code.angularjs.org/1.3.15/docs/guide/introduction 也就是官网针对1.3.15版的说明 What Is Angular? AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML’s syntax to express 

Maintaining Your Signing Identities and Certificates 维护你的签名标识和证书

Code signing your app lets users trust that your app has been created by a source known to Apple and that it hasn’t been tampered with. All apps must be code signed and provisioned to launch on a device, to use certain services, to be distributed for

Lua 5.1 参考手册

Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes 云风 译 www.codingnow.com Copyright © 2006 Lua.org, PUC-Rio. All rights reserved. 1 - 介绍 Lua 是一个扩展式程序设计语言,它被设计成支持通用的过程式编程,并有相关数据描述的设施. Lua 也能对面向对象编程,函数式编程,数据驱动式编程提供很好的支持.

sandy bridge

  SANDY BRIDGE SPANS GENERATIONS Intel Focuses on Graphics, Multimedia in New Processor Design By Linley Gwennap  {9/27/10-01} ................................................................................................................... Intel’s

《R in Nutshell》 读书笔记(连载)

R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshell") 第一部分:基础 第一章 批处理(Batch Mode) R provides a way to run a large set of commands in sequence and save the results to a file. 以batch mode运行R的一种方式是:使用系统