Reputation: 96
I am using UIAutomation to test our apps. What I want to do is: select a random date on a datePicker any day before today (in the same year). I already got the a random month and day value. Here is my code:
var currDate = new Date();
var year = currDate.getFullYear();
var month = new Array;
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var setMonthInt = currDate.getMonth();
var day = currDate.getDate();
var randomMonth = Math.floor((Math.random() * (setMonthInt + 1 - 0)) + 0);
var valueAtIndex = "\"" + month[randomMonth] + "\"";
if (randomMonth == setMonthInt){
var randomDay = "\"" + Math.floor((Math.random() * (day)) + 1) + "\"";
} else {
var randomDay = "\"" + Math.floor((Math.random() * (32)) + 1) + "\"";
}
this.window().popover().pickers()[0].wheels()[1].selectValue(valueAtIndex);
this.window().popover().pickers()[0].wheels()[0].selectValue(randomDay);
I have checked the valueAtIndex
variable does return a month for example "March" and the randomDay
variable does return a day for example "9".
As soon as I use the selectValue()
method instruments throw an error: - selectValue requires a valid value.
The setValue()
method does not work either.
How will I go about to tell the picker to get the random month and day generated?
Upvotes: 0
Views: 1661
Reputation: 96
I managed to figure out what it was. To add the "\""
isn't necessary. So I just created two more variables....
var randomDayToString = randomDay.toString();
var randomMonthToString = valueAtIndex.toString();
I used these two values to select the day and the month.
this.window().popover().pickers()[0].wheels()[1].selectValue(randomMonthToString);
this.window().popover().pickers()[0].wheels()[0].selectValue(randomDayToString);
This seems to work fine.
Upvotes: 1
Reputation: 606
From the other answers I have found on the Internet it seems like you don't need the '\' before and after the month and the day.
See here: UIAPickerWheel.selectValue() not working for text values within UIDatePicker
I've been working a lot with UI Automation lately but I haven't had to use a picker yet so I may be wrong. I'll look into it a little more while I wait for my tests to finish!
This may be of help too: UI Automation Testing: How do I select UIAPickerWheel values?
What seems to be the case is that the UIADatePicker removes some functionality of the UIAPickerWheel class because of how many possibilities there can be. So if directly picking the date doesn't work use the method of tapWithOffset and then check the value.
Upvotes: 0