Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

unable to update generated variable via script.

goofy78270
goofy78270 Posts: 133
edited May 2020 in SOAtest

I am trying to create a variable that is the abbreviated day of the week (MON, TUE, WED, ...) which I do either via the data generator tool or script. However, I am then trying to convert the variable to uppercase, but this conversion is not working, as expected.

import com.parasoft.api.*;
import groovy.util.*;
import groovy.json.JsonSlurper;
import java.text.SimpleDateFormat;

public String dayOfWeek (Object input, ExtensionToolContext context) {
    def date = new Date()
    def sdf = new SimpleDateFormat("EEE")
    dow = sdf.format(date)
    //dow = context.getValue("Generated Data Source", "db_dayOfWeek")

    dow = dow.toUpperCase()

    Application.showMessage ("Day of week for " + date + " is " + dow)
    context.setValue("db_dayOfWeek", dow)
    Application.showMessage ("Day of week for " + date + " is " + context.getValue("Generated Data Source", "db_dayOfWeek"))
}

This script results in the following:
Day of week for Mon May 18 14:00:16 MDT 2020 is MON
Day of week for Mon May 18 14:00:16 MDT 2020 is null

You can see in the first line, that the variable dow is set correctly, but it never gets assigned to the db_dayOfWeek variable for use in later steps.

different try with above script and data generator tool
Using the data generator tool to set the db_dayOfWeek, then running the above script by commenting out the def steps sdf and dow and uncommenting the second dow step, I get the following output, but you can see in the console output that the variable is indeed set:

---------- Test 2: determine Date and Time Variables ----------
Test 2: determine Date and Time Variables - success
set db_currentDT=2020-05-18 14:12:51
set db_startTime=15:00
set db_endTime=1600
set db_dayOfWeek=Mon
Test succeeded
---------- Test 3: set Day Of Week to UpperCase ----------
Test 3: set Day Of Week to UpperCase - failure

failure is:
Error during script execution. View Details for more information.
Cannot invoke method toUpperCase() on null object

running 2020.1 version and Use data source is checked in the script

Comments

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited May 2020

    Use a test suite variable. In other words, open the test suite editor, click the Variables tab, then click Add to define a variable of type String.
    If you use a Data Generator tool, configure it to write to that test suite variable and not a custom column name.
    If you use a script or Extension Tool, you can set a test suite variable using context.setValue("db_dayOfWeek", dow) or get the value using context.getValue("db_dayOfWeek").

  • goofy78270
    goofy78270 Posts: 133

    so only test suite variables can be set...not self generated variables like when you create one with a databank?

  • jakubiak
    jakubiak Posts: 798 admin

    Correct - values set by data banks cannot be manually changed via script.

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited May 2020

    Data Banks can write to Data Bank columns (or test suite variables). This means that if you want to store a value from an Extension Tool into a data bank column then you can chain a Data Bank to your Extension Tool. See my answers in this other thread which has examples for making a script return values within an XML or JSON document so you can chain an XML Data Bank or JSON Data Bank: How to store array to writable data source using Extension tool

    However, I believe test suite variables are really intended for what you are doing, and not custom data bank column names.