2

我正在用 Java 中的 Selenium/WebDriver 编写自动化测试用例。我实现了以下代码来轮询现有的 WebElements,但由于我不是 Java 专家,我想知道是否有更简洁的方法来编写此方法:

/** selects Business index type from add split button */
    protected void selectBusinessLink() throws Exception
    {
        Calendar rightNow = Calendar.getInstance();
        Calendar stopPolling = rightNow;
        stopPolling.add(Calendar.SECOND, 30);
        WebElement businessLink = null;
        while (!Calendar.getInstance().after(stopPolling))
        {
            try
            {
                businessLink = findElementByLinkText("Business");
                businessLink.click();
                break;
            }
            catch (StaleElementReferenceException e)
            {
                Thread.sleep(100);
            }
            catch (NoSuchElementException e)
            {
                Thread.sleep(100);
            }
            catch (ElementNotVisibleException e)
            {
                Thread.sleep(100);
            }
        }
        if (businessLink == null)
        {
            throw new SystemException("Could not find Business Link");
        }
    }

这一行让我觉得代码有点脏:

 while (!Calendar.getInstance().after(stopPolling))
4

2 回答 2

2

你可以做这样的事情

long t = System.currentMillis();   // actual time in milliseconds from Jan 1st 1970.
while (t > System.currentMillis() - 30000 )  {
   ...
于 2012-11-02T15:28:45.060 回答
0

以毫秒为单位使用系统时间怎么样?

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 30);
long stopPollingTime = calendar.getTimeInMillis();
while (System.currentTimeMillis() < stopPollingTime) {
  System.out.println("Polling");
  try {
    Thread.sleep(100);
  } catch (InterruptedException e) {
  }
}
于 2012-11-02T15:28:49.580 回答