[英]How can I enter tomorrow's date into a text field using Java and Selenium2/Webdriver
I am writing an automated test in Java using Selenium2/WebDriver. 我正在使用Selenium2 / WebDriver在Java中编写自动化测试。 I need to validate a birthday in the future is not allowed. 我需要在将来验证生日是不允许的。 To get tomorrow's date, I am using: 为了明天的约会,我正在使用:
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);
I am having trouble printing that to a text field, since .sendKeys requires characters. 我无法将其打印到文本字段,因为.sendKeys需要字符。 Any help is appreciated. 任何帮助表示赞赏。 I'm also unsure if that is the best way to get tomorrow's date. 我也不确定这是否是获得明天约会的最佳方式。
Your code is basically right. 你的代码基本上是对的。 Use a Calendar
to produce Date
objects: 使用Calendar
生成Date
对象:
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
Use SimpleDateFormat
to format the Date
as a String
: 使用SimpleDateFormat
将Date
格式化为String
:
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String todayAsString = dateFormat.format(today);
String tomorrowAsString = dateFormat.format(tomorrow);
System.out.println(todayAsString);
System.out.println(tomorrowAsString);
Prints: 打印:
09-Aug-2012
10-Aug-2012
You can use Selenium to send those String
objects to the date control (if it accepts keyed input, of course). 您可以使用Selenium将这些String
对象发送到日期控件(当然,如果它接受键控输入)。 You'll need to adjust the date pattern "dd-MMM-yyyy"
to match the format expected by the input control on the page, eg perhaps it's ( "MM/dd/YY"
)? 您需要调整日期模式"dd-MMM-yyyy"
以匹配页面上输入控件所期望的格式,例如,它可能是( "MM/dd/YY"
)?
First, don't you mean to add before defining the date? 首先,您不是要在定义日期之前添加吗? Then, you could do a 然后,你可以做一个
today.toString()
to assign via sendkeys, but IMO you should look first at the format the widget accepts (eg european vs US) and format your date accordingly (eg using something like this ). 通过的SendKeys分配,但IMO你应该先了解一下控件接受格式(例如欧洲VS美国)和(例如,使用类似相应的格式化你的日期此 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.