I would like to know with selenium is there a way to capture current date,month,year as the applicaion which i use has a seperate date, month, year fields which we need to manually type in. I problem I have here is the test case will only accepts the date <=31 days in future and won't accepts any date in the past. so if I can have a way where I can capture the current date, month, year (Numerical values) which would be useful
for example
<tr>
<td>type</td>
<td>form1:txtCoverDateDay</td>
<td>02</td>
</tr>
<tr>
<td>type</td>
<td>form1:txtCoverDateMonth</td>
<td>11</td>
</tr>
<tr>
<td>type</td>
<td>form1:txtCoverDateYear</td>
<td>2011</td>
</tr>
We can select a date from a datepicker with Selenium webdriver using Python. To identify a particular date, first we have to use the find_elements method and identify all the dates having a common locator value. The find_elements returns a list of matching elements.
Record and playback is one of the key features of Selenium IDE, among other features that help manual testers get a taste of automation and its benefits. Selenium IDE has a rich set of commands powered by Selenese, allowing you to record various interactions of a web app with the browser.
use storeEval
The example below is a fully working selenium test that matches your requirements exactly, to run it open the IDE, click on the Source tab in the main window and paste the code between the <tbody>
tag
<tr>
<td>open</td>
<td>http://www.plus2net.com/php_tutorial/date-selection.php</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>new Date().setDate( new Date().getDate() + 31);</td>
<td>date</td>
</tr>
<tr>
<td>echo</td>
<td>${date}</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>d=new Date( new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 31); d=d.getFullYear()</td>
<td>year</td>
</tr>
<tr>
<td>type</td>
<td>name=year</td>
<td>${year}</td>
</tr>
<tr>
<td>echo</td>
<td>${year}</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>d=new Date( new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 31); d=d.getMonth() + 1</td>
<td>month</td>
</tr>
<tr>
<td>select</td>
<td>name=month</td>
<td>value=${month}</td>
</tr>
<tr>
<td>echo</td>
<td>${month}</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>d=new Date( new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 31).getDate(); d= d.toString().length == 1 ? "0" + d : d;</td>
<td>day</td>
</tr>
<tr>
<td>select</td>
<td>name=dt</td>
<td>label=${day}</td>
</tr>
<tr>
<td>echo</td>
<td>${day}</td>
<td></td>
</tr>
I've added in the javascript to return a date 31 days in the future.
You can also use a combination of runScript and storeEval to get virtually anything from the page.
A question I've asked demonstrates the idea
Load an external js file containing useful test functions in selenium
Any other questions add a comment, and I'll be happy to help :)
See @icc97 comment to add zero padding to the dates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With