Store current datetime in variable in Selenium IDE and use it for a random email address

I use variables all the time. And to be able to re-use a test over and over again, I need random email addresses whenever I fill in forms.

For this I define a variable with the current date and time and then a variable which will hold the email address which uses the current date and time.

My random email address will look like: selenium-20220318_122803@pauledenburg.com

Just store the following as 1 string into the ‘Target’ part of your command.

const date=new Date(); return String(date.getFullYear())  + String(date.getMonth()+1) + String(date.getDate()) + '_' + String(date.getHours() < 10 ? "0"+date.getHours() : date.getHours()) + String(date.getMinutes() < 10 ? "0"+date.getMinutes() : date.getMinutes()) + String(date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())

It will look like this in Selenium IDE:

storing the current datetime in a variable in Selenium IDE

Now you can use this to create your email address which is unique every time you run your test:

And use it when you want to fill a form.

Click Here to Leave a Comment Below

Ginger Oliver - 06/01/2023

As a total beginner to coding of any type, I would have liked to have seen how to add the “@organization.com” with the script and the next step.

For example – I found an example to set a variable in OverStack that had steps as:

select
store text – ex: blackhole_${timestamp}@email.com
echo

Reply
    Paul Edenburg - 06/01/2023

    Thanks for adding that Ginger Oliver!

    I extended the example with it

    Reply
Robert - 30/01/2024

Thanks Paul, this worked a charm!

Reply
Leave a Reply: