Common tasks that you can perform with the Groovy Script test step

Posted mastermonkintemple

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:

 

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

 
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的主要内容,如果未能解决你的问题,请参考以下文章

How can you catch a process that is about to be launched, if you don’t know the PID yet?

You can find the timer(s) that are using too much CPU time using the Developer Tools timeline

英语口语练习(You know we can't afford that. How do other people do it? Other people make more twenty-f

DB2If ‘db2‘ is not a typo you can run the following command to lookup the package that contains th(代

You mean you can't take less?

git Please move or remove them before you can merge. 错误解决方案