Reputation: 521
For random number generation in Selenium RC, I have the code but it is not working properly in Webdriver. If I use the below code in webdriver, it is returning null. Please find the code below in webdriver
String Randnum = (String) ((JavascriptExecutor) driver).executeScript("var d=new Date().getFullYear()+new Date().getDate().toString()" +
"+new Date().getDay()" +
"+new Date().getHours()" +
"+new Date().getMinutes()" +
"+new Date().getSeconds()" +
"+new Date().getMilliseconds()");
Please help me out on this... Help will be appreciated.
Upvotes: 0
Views: 970
Reputation: 5667
Why can't you try the same thing using Java?
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter= new SimpleDateFormat("MM_dd_yyyy_hh_mm_ss");
String randomName = formatter.format(currentDate.getTime());
Upvotes: 1
Reputation: 187
the javascript code below
function displayRandum()
{
var d = new Date().getFullYear()+new Date().getDate().toString() + new Date().getDay() + new Date().getHours()+new Date().getMinutes() + new Date().getSeconds()+new Date().getMilliseconds();
alert(d);
}
works perfectly. Can you try in your Selenium code, to keep everything in one line (no extra "+" and " characters).
Edit: (missed return statement. Saw Arran's reply and realized it).
function returnRandum()
{
var d = new Date().getFullYear()+new Date().getDate().toString() + new Date().getDay() + new Date().getHours()+new Date().getMinutes() + new Date().getSeconds()+new Date().getMilliseconds();
return d;
}
Upvotes: 0
Reputation: 25056
You are missing the return statement. Without, the javascript will run but not return anything.
String Randnum = (String) ((JavascriptExecutor) driver).executeScript("return new Date().getFullYear() + new Date().getDate().toString() + new Date().getDay() + new Date().getHours() + new Date().getMinutes() + new Date().getSeconds() + new Date().getMilliseconds()");
Upvotes: 1