Select a date from date picker using Selenium webdriver
The problem that many of the QA face when they have to select a date from date picker is that they don’t know exactly how to tell to the picker in which month it should be and on that specific month which day should I select. All this is because time never stops , it’s a dynamic mechanism and we need to deal with that.
Prerequisites to select a date from date picker:
We need not to think about selenium in the first instance , we need to find a way to provide the write information to selenium in order to be able to deal with the date picker :
Calculate the difference between two dates :
To be able to how many months we flip over the date picker months we need to know how many months are between 2 dates , this includes how many months in the future from today based on the number of days ahead , or how many months in the past based on number of days in the past provided.
Im using date library from Ruby so you should consider to require ‘date’ in top of your class/module.
Now we need to get the target date for the date picker. Now my date picker had the date format Month day, year eg :
August 25, 2017
So the method will return the date in my format but you can change this method according with your date picker.
Navigate through the date picker:
Using the informations that we have so far we can basically start thinking how we can navigate through the date picker .
Define select date method:
In this case I used the calculated date returned in a String format inside an xpath , and click on it.
Create select target date method:
Now we need to combine all methods from above in one method that will be used inside our step definition:
Create your cucumber step:
And finally here is your cucumber step and your step definition:
When I select that I'm planning to move in <nr_of_days> days
When(/I select that I'm planning to move in (.*) days$/) do |nr_of_days| $web.moving.select_target_date(nr_of_days) end