【问题标题】:set timers / wait for nothing设置计时器/什么都不等
【发布时间】:2021-10-03 02:04:30
【问题描述】:

我的情况是,从我的应用程序中打开位智等待 25 秒,进入主屏幕,等待 10 秒,然后系统返回位智,然后我等待 15 秒,然后再次进入主页并在那里等待 10 秒,

mainScreen.openWaze();
TimeWatch watch = TimeWatch.start();
double passedTimeInSeconds = 0;
System.out.println("start");
while (passedTimeInSeconds < 26.0) {
    passedTimeInSeconds = watch.time(TimeUnit.SECONDS);
    System.out.println("the seconds of 1 " + passedTimeInSeconds);
    wazeInApp.validateWhereToField();
}
watch.reset();
passedTimeInSeconds = 0;
wazeInApp.goToHomeScreen();
while (passedTimeInSeconds < 11.0) {
    passedTimeInSeconds = watch.time(TimeUnit.SECONDS);
    System.out.println("the seconds of 2 " + passedTimeInSeconds);
    wazeInApp.validateWhereToField();
}
watch.reset();
passedTimeInSeconds = 0;
while (passedTimeInSeconds < 16.0) {
    passedTimeInSeconds = watch.time(TimeUnit.SECONDS);
    System.out.println("the seconds 3 " + passedTimeInSeconds);
    wazeInApp.validateWhereToField();
}
wazeInApp.goToHomeScreen();

我的控制台打印:

start
the seconds of 1 0.0
the seconds of 1 12.0
the seconds of 1 13.0
the seconds of 1 13.0
the seconds of 1 34.0
the seconds of 2 0.0
the seconds of 2 0.0
the seconds of 2 0.0
the seconds of 2 0.0
the seconds of 2 9.0
the seconds of 2 15.0
the seconds 3 0.0
the seconds 3 2.0
the seconds 3 4.0
the seconds 3 6.0
the seconds 3 9.0
the seconds 3 11.0
the seconds 3 13.0
the seconds 3 34.0

看起来很奇怪,打印的时间不正确,也超出了时间限制,而且这些是否是等待的正确解决方案(什么都不做)? 谢谢

【问题讨论】:

  • 你想做什么?暂停特定时间段的执行?
  • 是的,但我不想做 Thread.sleep
  • 调试代码以在运行时查看变量值。
  • @pburgr 做到了,它就是你看到的打印堆栈
  • 这些是等待(什么都不做)的正确解决方案吗? --> 你能把openWaze() 和validateWhereToField() 的代码贴出来吗?如果我们能看到,那么只有我们可以告诉你。你可以试试 int passTimeInSeconds = 0; 吗?

标签: java selenium appium


【解决方案1】:

我相信您正在尝试在这 26 秒内检查某些内容,因此您不喜欢 Thread.sleep() 我尝试了以下方法,它对我有用。在这里,我将系统当前秒数加 26(我们的预期时间),因此循环将从当前秒数迭代到 26 秒。

long passedTimeInSeconds = 0, earlierSecond = 0, currentSecond = 0;
long waitTillTime =  Instant.now().getEpochSecond() +26;
while(passedTimeInSeconds < waitTillTime){
    passedTimeInSeconds = Instant.now().getEpochSecond();
    earlierSecond = passedTimeInSeconds%60+1;
    currentSecond = Instant.now().getEpochSecond()% 60 + 1;
    if(currentSecond > earlierSecond) {
    System.out.println("Second: "+earlierSecond);
    }
 

输出

Second: 37
Second: 38
Second: 41
Second: 43
Second: 44
Second: 45
Second: 46
Second: 47
Second: 49
Second: 51
Second: 52
Second: 54
Second: 58
Second: 59
Second: 1

由于毫秒,有时条件可能会失败。

【讨论】:

  • 看起来不错,但问题是只打印一次 - 它不应该在一段时间内打印每个迭代吗?其次,它将时间打印为 1627384470,我想以秒为单位查看它。如何从 1970 年转换它?
  • @ness 现在它打印当前的秒数。请尝试让我知道。仅当第二个更改时才打印有点困难,因为如果我们添加条件并且它应该在打印语句之前比较旧时间和新时间,但是由于毫秒,条件有时可能会失败。我也为此添加了代码。
猜你喜欢
  • 2019-11-22
  • 2019-10-19
  • 1970-01-01
  • 2019-02-09
  • 2015-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多