# How do I set a random number in a text field?

Hi Andrew

1, 2, 1, 1, 3, 1, 0, 010, 100

45, 45,000, 45, 45,000,000, -45, 4.5, -4.5, -0.45

In other words, can the numbers repeat? Can they be negative? Contain decimal values? In a specific range? Should zero be included? Are leading zeros required?

I’m not trying to be difficult, but within your simple question there are hidden “what ifs”

Personally, I use this library:

``````import org.apache.commons.lang.RandomStringUtils as RandStr
``````

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/RandomStringUtils.html

``````import java.util.concurrent.ThreadLocalRandom@Keyword
def randomNumber(TestObject to, int minimum, int maximum) {
def randomNumber = ThreadLocalRandom.current().nextInt(minimum, maximum + 1)
WebUI.findWebElement(to).sendKeys(String.valueOf(randomNumber))
}
``````
5 Likes

Russ Thomas said:

Hi Andrew

1, 2, 1, 1, 3, 1, 0, 010, 100

45, 45,000, 45, 45,000,000, -45, 4.5, -4.5, -0.45

In other words, can the numbers repeat? Can they be negative? Contain decimal values? In a specific range? Should zero be included? Are leading zeros required?

I’m not trying to be difficult, but within your simple question there are hidden “what ifs”

Personally, I use this library:

``````import org.apache.commons.lang.RandomStringUtils as RandStr
``````

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/RandomStringUtils.html

Numbers can repeat, however cannot be negative, and cannot contain decimals or commas.

I’m looking for numbers like these:

1947400365
2057305591
0946245722
1000344536

Also, it would be nice if the numbers don’t repeat. (optional)

Again, thanks for the help.

Russ Thomas said:

Hi Andrew

1, 2, 1, 1, 3, 1, 0, 010, 100

45, 45,000, 45, 45,000,000, -45, 4.5, -4.5, -0.45

In other words, can the numbers repeat? Can they be negative? Contain decimal values? In a specific range? Should zero be included? Are leading zeros required?

I’m not trying to be difficult, but within your simple question there are hidden “what ifs”

Personally, I use this library:

``````import org.apache.commons.lang.RandomStringUtils as RandStr
``````

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/RandomStringUtils.html

Sorry Andrew, if you’re looking for me to write the code for you, I don’t have the time.

Try doing it yourself, use the library I linked to. If you run into issues, post another question.

If you see my post (up a few) then there’s some code for a custom keyword.

andrew said:

I have practically no programming experience, can someone please create some simple code I can use to set a random number in a text field? Preferably 10 random numbers.

Thanks so much.

Here is a script using the keyword Tom provided you, that does what you have asked for.
It enters the random number in to the google search field and searches for it, you will need to use the object spy to create a test object for the google search input field and create the keyword named as randomNumberGenerator

``````import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpointimport static com.kms.katalon.core.testcase.TestCaseFactory.findTestCaseimport static com.kms.katalon.core.testdata.TestDataFactory.findTestDataimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObjectimport com.kms.katalon.core.checkpoint.Checkpoint as Checkpointimport com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactoryimport com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywordsimport com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobileimport com.kms.katalon.core.model.FailureHandling as FailureHandlingimport com.kms.katalon.core.testcase.TestCase as TestCaseimport com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactoryimport com.kms.katalon.core.testdata.TestData as TestDataimport com.kms.katalon.core.testdata.TestDataFactory as TestDataFactoryimport com.kms.katalon.core.testobject.ObjectRepository as ObjectRepositoryimport com.kms.katalon.core.testobject.TestObject as TestObjectimport com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywordsimport com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywordsimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUIimport internal.GlobalVariable as GlobalVariableimport com.kms.katalon.core.logging.KeywordLogger as KeywordLoggerimport org.openqa.selenium.Keys as Keys'Open browser'WebUI.openBrowser('')'Navigate to Google home page'WebUI.navigateToUrl('https://www.google.co.uk/?gfe_rd=cr&dcr=0&ei=e03DWq_gOdKN8Qepk4DICg&gws_rd=ssl')'Focus on search input field'WebUI.focus(findTestObject('Object Repository/Page_Google/input_q'))'Type random number'CustomKeywords.'randomNumberGenerator.randomNumber'(findTestObject('Object Repository/Page_Google/input_q'), 10000, 99999)'Type random number again to get to 10 characters as requested'CustomKeywords.'randomNumberGenerator.randomNumber'(findTestObject('Object Repository/Page_Google/input_q'), 10000, 99999)'Take a screenshot for report'WebUI.takeScreenshot()'Press Enter key to submit search query'WebUI.sendKeys(findTestObject('Object Repository/Page_Google/input_q'), Keys.chord(Keys.ENTER))'Short 2 second delay prior to closing browser'WebUI.delay(2)'Close browser'WebUI.closeBrowser()
``````

Try it out!

1 Like

Anthony said:

andrew said:

I have practically no programming experience, can someone please create some simple code I can use to set a random number in a text field? Preferably 10 random numbers.

Thanks so much.

Here is a script using the keyword Tom provided you, that does what you have asked for.
It enters the random number in to the google search field and searches for it, you will need to use the object spy to create a test object for the google search input field and create the keyword named as randomNumberGenerator

``````import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpointimport static com.kms.katalon.core.testcase.TestCaseFactory.findTestCaseimport static com.kms.katalon.core.testdata.TestDataFactory.findTestDataimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObjectimport com.kms.katalon.core.checkpoint.Checkpoint as Checkpointimport com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactoryimport com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywordsimport com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobileimport com.kms.katalon.core.model.FailureHandling as FailureHandlingimport com.kms.katalon.core.testcase.TestCase as TestCaseimport com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactoryimport com.kms.katalon.core.testdata.TestData as TestDataimport com.kms.katalon.core.testdata.TestDataFactory as TestDataFactoryimport com.kms.katalon.core.testobject.ObjectRepository as ObjectRepositoryimport com.kms.katalon.core.testobject.TestObject as TestObjectimport com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywordsimport com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywordsimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUIimport internal.GlobalVariable as GlobalVariableimport com.kms.katalon.core.logging.KeywordLogger as KeywordLoggerimport org.openqa.selenium.Keys as Keys'Open browser'WebUI.openBrowser('')'Navigate to Google home page'WebUI.navigateToUrl('https://www.google.co.uk/?gfe_rd=cr&dcr=0&ei=e03DWq_gOdKN8Qepk4DICg&gws_rd=ssl')'Focus on search input field'WebUI.focus(findTestObject('Object Repository/Page_Google/input_q'))'Type random number'CustomKeywords.'randomNumberGenerator.randomNumber'(findTestObject('Object Repository/Page_Google/input_q'), 10000, 99999)'Type random number again to get to 10 characters as requested'CustomKeywords.'randomNumberGenerator.randomNumber'(findTestObject('Object Repository/Page_Google/input_q'), 10000, 99999)'Take a screenshot for report'WebUI.takeScreenshot()'Press Enter key to submit search query'WebUI.sendKeys(findTestObject('Object Repository/Page_Google/input_q'), Keys.chord(Keys.ENTER))'Short 2 second delay prior to closing browser'WebUI.delay(2)'Close browser'WebUI.closeBrowser()
``````

Try it out!

Looks like the code works just fine for google, but not on what i’m using it for. Katalon just doesn’t recognize what i’m clicking on. For example, I see the red outline in the recorder, but in the manual script, it just reads “Div_1”

I’ve changed the code in the correct places, but i’m not entirely sure if it’s correct. Sadly i’m not sure if you can assist anymore, as i’m not allowed to share any code under company policy.

Thanks for the help, I’ll keep searching.

Edit: It also sometimes shows up as WebUI.doubleClick(findTestObject('input_

However, it still doesn’t work.

Can you just type something normal (not from the keyword) in to the required field? Is it just your test object is incorrect? Recorder is rubbish sometimes, may need to create your own custom test object

Anthony said:

Can you just type something normal (not from the keyword) in to the required field? Is it just your test object is incorrect? Recorder is rubbish sometimes, may need to create your own custom test object

I can type into the text area in the recorder, but when I play it back, it does not add the text in. It’s very strange.

The setText goes like this:

Finding text object with id:
Checking test object
Checking timeout
Finding web element with id:
Found 1 web element with id:
Clearing text of object
Checking timeout
Finding web element with id:
Found 1 web element with id:
Setting text of object … to value 123.

It says it’s completed, but no text or numbers are actually put in.

In the end, the test case is completed with no errors.

andrew said:

Anthony said:

Can you just type something normal (not from the keyword) in to the required field? Is it just your test object is incorrect? Recorder is rubbish sometimes, may need to create your own custom test object

I can type into the text area in the recorder, but when I play it back, it does not add the text in. It’s very strange.

The setText goes like this:

Finding text object with id:
Checking test object
Checking timeout
Finding web element with id:
Found 1 web element with id:
Clearing text of object
Checking timeout
Finding web element with id:
Found 1 web element with id:
Setting text of object … to value 123.

It says it’s completed, but no text or numbers are actually put in.

you never focus on the object, just like a real user you need to click or focus on the object, if you object is being found it is not the recorder being funny, but still I would create a custom test object and take the objects properties from the code manually to ensure its correct

1 Like

Anthony said:

andrew said:

Anthony said:

Can you just type something normal (not from the keyword) in to the required field? Is it just your test object is incorrect? Recorder is rubbish sometimes, may need to create your own custom test object

I can type into the text area in the recorder, but when I play it back, it does not add the text in. It’s very strange.

The setText goes like this:

Finding text object with id:
Checking test object
Checking timeout
Finding web element with id:
Found 1 web element with id:
Clearing text of object
Checking timeout
Finding web element with id:
Found 1 web element with id:
Setting text of object … to value 123.

It says it’s completed, but no text or numbers are actually put in.

you never focus on the object, just like a real user you need to click or focus on the object, if you object is being found it is not the recorder being funny, but still I would create a custom test object and take the objects properties from the code manually to ensure its correct

That helps a little bit. Now when I launch it, the same result occurs, however as soon as I manually IRL click the box it auto fills in the random text and closes/completes the test case.

I’ve played around with adding a custom test object and doing multiple clicks or double clicks but still no results.

However, I’ve decided that this is good enough. We can close the thread now.

hi,

``````for randon long

List <Long> numbers = new ArrayList<>()
Random r = new Random();
long xx = 100000000L;
long yy = 999999999L;
long number = 0L
number = xx+((long)(r.nextDouble()*(yy-xx)));
int counter = 8
while (counter >= 0){
number = xx+((long)(r.nextDouble()*(yy-xx)));
if(!numbers.contains(number)){
counter--
}
}

Collections.sort(numbers, Collections.reverseOrder());
println ("numbers are in list with reversed order "+numbers)
``````

`numbers are in list with reversed order [932012469, 778901567, 575714526, 549471576, 502466957, 397888960, 376620930, 210722289, 177715260, 149632210]`

1 Like

Hi @Russ_Thomas why I get crossed out when I import the library you attach on the link? works anyways but I don’t know what that means

But you didn’t…

Perhaps @Brandon_Hein will explain what’s going on with apache’s `lang` and `lang3`.

@Brandon_Hein, Delibrately doing this publically, mod-to-mod…

Feel free to hack the sh1t out of this thread. The “solution” is dubious, to say the least. The thread is well over a year old. My java/lib knowledge is shaky as hell. You have some rand stuff around here somewhere…

Remove any pointless diversions, take the OP, find an answer and install it as the solution. Community knowledge base comes first, right?

Over to you…

I’ll just leave this here…

There are plenty of ways to generate random data, these are just the ones that I’ve used that work well.

1 Like