Unique Value of my custom Variable On every Test Execution

Hi,
I want to give the value from my custom variable e.g " def x=1 " in
WebUI.setText(findTestObject(‘objectRepository/Facebook/email’), myVARIABLE).
Here by writing my variable’s name I want to print the value of that variable on execution. But it is not happening giving error MISSINGMETHODEXCEPTION.

Hi Zain,

WebUI.setText() method takes TestObject and String as parameters. When you assign 1 to your variable , Groovy thinks it is an integer and it sets the type to int. You can try to use strong typing - define your variable as String, not def (weak typing from Groovy)

String yourVar = "1"
WebUI.setText(findTestObject('objectRepository/Facebook/email'), yourVar)

edit: OR you can use def, but put 1 into double quotes to tell the compiler that it is a string

def var1 = 1
def var2 = "1"

println var1.class
println var2.class

// console output

04-06-2018 01:26:36 PM - [START]  - Start action : Statement - println(class)
class java.lang.Integer
04-06-2018 01:26:36 PM - [END]    - End action : Statement - println(class)
04-06-2018 01:26:36 PM - [START]  - Start action : Statement - println(class)
class java.lang.String
04-06-2018 01:26:36 PM - [END]    - End action : Statement - println(class)

1 Like

Thanks alot !!! It worked. Thanks again. :slight_smile:

Mark,
Could you please help me in one more thing.
Actually I want to give a unique number in my website’s integer input field on every time test case execution, so I decided to do increment of 1 in the variable each time my test case runs. But I don’t know how to save the value in the variable during the execution of the test.
Please help on this.

1 Like

You have multiple options how to get unique number value.

The easiest way is to get current timestamp and put it into variable.

// this will give you current timestamp in seconds
long ts = System.currentTimeMillis() / 1000L

// you need string, so simply convert long to String
String tsAsString = ts.toString()

// and finally, use your unique number in setText method
WebUI.setText(findTestObject('objectRepository/Facebook/email'), tsAsString)

You can also use incremented value, but the approach would be much harder - you would have to save your value to permanent memory (to a file, database, …), then read the value and increment it. I won’t recommend it unless you really need some smaller numbers than timestamp is.

2 Likes

Absolutely Looking for this type of solution. Thanks alot Mark.