Hello Everyone,
Another question
I want to generate a random email address e.g thisismyaddress+currenttimestampinnumeric@Gmail.com
Gmail will ignore any number after + and all the transactions I will do on my test website will have notifications going to thisismyaddress@Gmail.com . Till now I am able to figure out how to generate a random number and then concatenate it with thisismyaddress+@Gmail.com
How do I solve this ?
Do you want a truly random number, or do you want a timestamp?
Return a random number/string/alphanumeric of a given length (you would append this yourself):
public String generateRandomAlphanumeric(final int length) {
String randomAlphanumeric = generateRandomCharSequence(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
return randomAlphanumeric;
}
public String generateRandomString(final int length) {
String randomString = generateRandomCharSequence(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
return randomString;
}
public String generateRandomNumber(final int length) {
String randomNumber = generateRandomCharSequence(length, "1234567890");
return randomNumber;
}
private String generateRandomCharSequence(final int length, final String possibleChars) {
StringBuilder builder = new StringBuilder();
Random rnd = new Random();
while (builder.length() < length) {
int index = (int)(rnd.nextFloat() * possibleChars.length());
builder.append(possibleChars.charAt(index));
}
return builder.toString();
}
Append timestamp:
public String appendTimestamp(final String text) {
return text += System.nanoTime();
}
1 Like
@Brandon_Hein Thank you for your quick reply.
I want to use current timestamp after +.
However I am very very basic in programming and not able to figure it where to use the code you just added and what is the variable it is passing time stamp. I am so sorry about that.
No problem!
Instead, letâs assume here that this is a one-off, and that youâre not trying to encapsulate this into a method for use elsewhere.
Try this:
String myEmailAddress = "thisismyaddress+" + System.nanoTime() + "@Gmail.com";
System.out.println(myEmailAddress);
2 Likes
Thank you @Brandon_Hein
This would work, I have 80+ scenarios in which I have to fill email address and currenttimestamp will ensure no duplication in email address.
I am going to give it a try right now
@Brandon_Hein
It worked exactly what I was hoping for.
You are a champion. Thanks buddy
1 Like
corey
May 20, 2019, 5:12pm
8
This post is kind of old but wanted to ask a question as Iâm doing the same thing as the original poster.
Iâm able to add the nanoTime into my emails but is there a way to put an actual time/date string into an email with this method?
Example: test+DATE-TIME@gmail.com = test+1.1.19-12.10.10@gmail.com
Any help is appreciated.
You will need to use SimpleDateFormat :
import java.text.SimpleDateFormat
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy-HH.mm.ss");
String date = simpleDateFormat.format(new Date());
String email = "test+" + date + "@gmail.com";
2 Likes
Thanks for this, really helped me
Hello everyone,
I have tried using above solution for âgenerating random email address with current timestampâ to create new user in API endpoint. I have added user email id in variable table as âQAA.Userâ + System.nanoTime() +"@gmail.com " but it is failing with 400 bad request. Can anybody help?
We would need more info on your problem. Can you share the full request and response data that you are seeing?
Thanks for posting this solution. This has helped for a project Iâm working on.
This is what Iâm using as a hardcoded solution:
String myEmailAddress = (âthisismyaddress+â + System.nanoTime()) + â@mail.co.uk â
System.out.println(myEmailAddress)
WebUI.sendKeys(findTestObject(âemail alerts/Email fieldâ), myEmailAddress)
I was wondering how I could convert this to a method to use with sendkeys?
Sure. I assume you already know how to create custom keywords, but if not, give this a look:
In addition to built-in keywords, you can define custom keywords to extend the capabilities of Katalon Studio. Once created, custom keywords can be used when implementing test cases, just like other built-in keywords.
Once you create your new keywords class, then you can add as many methods as you like. For your hardcoded stuff above, it would be:
@Keyword
def getRandomizedEmailAddress(String address) {
String randomizedAddress = address + System.nanoTime() + "@mail.co.uk"
return randomizedAddress
}
Then in your script:
String myEmailAddress = CustomKeywords.newpackage.newkeyword.getRandomizedEmailAddress("thisismyaddress")
WebUI.sendKeys(findTestObject(âemail alerts/Email fieldâ), myEmailAddress)
(youâll need to replace the newpackage.newkeyword bit with whatever you name your package and class)
You could modify this as much as you like. For instance, you could also parameterize the domain:
@Keyword
def getRandomizedEmailAddress(String address, String domain) {
String randomizedAddress = address + System.nanoTime() + domain
return randomizedAddress
}
etc. Let me know how it goes
1 Like
I managed to get this to work. There were quote marks missing from the custom keyword to reference this in the script but having looked at my previous custom keywords I just added this in and it worked!
Really appreciate the help
1 Like
Hello!
I managed to run a random email test case for a registration test by applying the following:
UUID.randomUID(). toString(). substring(0,10)+â@gmail.com â
This line of code I added in Variables(Script mode) of the test case, for the email field.
This is displayed as follows:
Please create static method in keywords and call it in testcase variable or global variable