【问题标题】:selectValue on date picker does not work日期选择器上的 selectValue 不起作用
【发布时间】:2014-09-01 00:41:27
【问题描述】:

我正在使用 UIAutomation 来测试我们的应用。我想要做的是:在今天之前的任何一天(同年)在 datePicker 上选择一个随机日期。我已经得到了随机的月份和日期值。这是我的代码:

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);

我检查了valueAtIndex 变量确实返回一个月,例如“三月”,randomDay 变量确实返回一天,例如“9”。

只要我使用selectValue() 方法,仪器就会抛出错误:- selectValue requires a valid value.

setValue() 方法也不起作用。

我将如何告诉选择器生成随机的月份和日期?

【问题讨论】:

  • 我认为这条线Math.floor((Math.random() * (32)) + 1)可以返回32作为可能的一天。 32 * .99 向下舍入是 31 + 1 是 32。也许这就是导致错误的原因?
  • @Braains 这不是问题,32 不包含在返回值中

标签: javascript ios ipad ui-automation ios-ui-automation


【解决方案1】:

从我在互联网上找到的其他答案看来,您似乎不需要月份和日期之前和之后的“\”。

请看这里: UIAPickerWheel.selectValue() not working for text values within UIDatePicker

我最近一直在使用 UI 自动化,但我还没有使用选择器,所以我可能错了。我会在等待测试完成时再研究一下!

这也可能有帮助:UI Automation Testing: How do I select UIAPickerWheel values?

情况似乎是 UIADatePicker 删除了 UIAPickerWheel 类的一些功能,因为有多少可能性。所以如果直接选择日期不起作用,请使用 tapWithOffset 的方法,然后检查值。

【讨论】:

    【解决方案2】:

    我设法弄清楚它是什么。不需要添加"\""。所以我又创建了两个变量....

    var randomDayToString = randomDay.toString();

    var randomMonthToString = valueAtIndex.toString();

    我使用这两个值来选择日期和月份。

    this.window().popover().pickers()[0].wheels()[1].selectValue(randomMonthToString);

    this.window().popover().pickers()[0].wheels()[0].selectValue(randomDayToString);

    这似乎工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 2014-11-25
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      相关资源
      最近更新 更多