I have a custom-keyword which create a random ssn number as below:
@Keyword
def randomSSN() {
Random rand = new Random();
// Generate random SSN.
int ssn1ssn = rand.nextInt(600)+100;
int ssn2ssn = rand.nextInt(89)+10;
int ssn3ssn = rand.nextInt(8999)+1000;
// Print random SSN
System.out.println("SSN: " + ssn1ssn + "-" + ssn2ssn + "-" + ssn3ssn);
}
I can see the output of the custom keyword when I call it from my test case as expected, but I cannot use it once I am in my test case, my question is, how do I retrieve ssn1ssn, ssn2ssn, ssn3ssn values and use them from my test case?
The main goal is to generate a random SSN and assign it to a new individual that I will enter in the App under test in the SSN field.
From my test case I execute as follows, but second line fails as shown below:
CustomKeywords.‘App.appFunctions.randomSSN’()
System.out.println("ssn1ssn: " + ssn1ssn);
2022-10-18 10:01:47.615 DEBUG testcase.SSN - 1: App.appFunctions.randomSSN()
Random Integers: 391
Random Integers: 25
Random Integers: 2145
SSN: 391-25-2145
2022-10-18 10:01:47.666 INFO k.k.c.m.CustomKeywordDelegatingMetaClass - App.appFunctions.randomSSN is PASSED
2022-10-18 10:01:47.666 DEBUG testcase.SSN - 2: out.println("ssn1ssn: " + ssn1ssn)
2022-10-18 10:01:47.682 ERROR c.k.katalon.core.main.TestCaseExecutor - Test Cases/APP/SSN FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: ssn1ssn for class: Script1666069162799
at SSN.run(SSN:22)
@josue.chavezduran try this,
@Keyword
def randomSSN() {
Random rand = new Random();
// Generate random SSN.
int ssn1ssn = rand.nextInt(600)+100;
int ssn2ssn = rand.nextInt(89)+10;
int ssn3ssn = rand.nextInt(8999)+1000;
def SSN = ssn1ssn + "-" + ssn2ssn + "-" + ssn3ssn);
return SSN;
// Print random SSN
// System.out.println(SSN);
}
Test case:
def SSN = CustomKeywords.‘App.appFunctions.randomSSN’();
System.out.println(SSN);
1 Like
Keyword:
package my
import com.kms.katalon.core.annotation.Keyword
public class RandomSSNGenerator {
private static Integer ssn1
private static Integer ssn2
private static Integer ssn3
private static generate() {
Random rand = new Random();
// Generate random SSN.
ssn1 = rand.nextInt(600)+100;
ssn2 = rand.nextInt(89)+10;
ssn3 = rand.nextInt(8999)+1000;
}
@Keyword
public static Tuple randomSSNasTuple() {
generate()
return new Tuple(ssn1, ssn2, ssn3)
}
@Keyword
public static List<Integer> randomSSNasList() {
generate()
return [ssn1, ssn2, ssn3]
}
@Keyword
public static Map<String, Integer> randomSSNasMap() {
generate()
return ["ssn1": ssn1, "ssn2": ssn2, "ssn3": ssn3]
}
}
Test Case:
import my.RandomSSNGenerator
Tuple ssnT = RandomSSNGenerator.randomSSNasTuple()
println("as Tuple: " + ssnT[0] + ", " + ssnT[1] + ", " + ssnT[2])
List<List> ssnL = RandomSSNGenerator.randomSSNasList()
println("as List: " + ssnL[0] + ", " + ssnL[1] + ", " + ssnL[2])
Map<String, Integer> ssnM = RandomSSNGenerator.randomSSNasMap()
println("as Map: " + ssnM["ssn1"] + ", " + ssnM["ssn2"] + ", " + ssnM["ssn3"])
Output:
as Tuple: 650, 88, 9075
as List: 417, 11, 2358
as Map: 491, 91, 9604
1 Like
How about concatenate the Integers that you made into a String and then return the String back to your program.
As an example,
@Keyword
def String randomSSN() {
Random rand = new Random();
// Generate random SSN.
int ssn1ssn = rand.nextInt(600)+100;
int ssn2ssn = rand.nextInt(89)+10;
int ssn3ssn = rand.nextInt(8999)+1000;
// return random SSN
return "${ssn1ssn.toString()}-${ssn2ssn.toString()}-${ssn3ssn.toString()}";
}
And then, like @Dave_Evers has:
Test case:
def SSN = CustomKeywords.'App.appFunctions.randomSSN'();
System.out.println(SSN);
1 Like