我想在TestNG中实现TimerTask,但它失败了。 看看我的代码
public class Task extends TimerTask {
static int i =0;
@Override
public void run() {
System.out.println(++i +" : Hi");
if(i==40){
System.out.println("inside run method");
cancel();
System.exit(0);
}
}
}
上层阶级是我要实施的任务
public class TestCount{
private static final long DELAY = 0;
private static final long PERIOD = 100;
@Test
public void test(){
Timer timer = new Timer();
timer.scheduleAtFixedRate(new Task(), DELAY, PERIOD);
}
}
输出:// 1:嗨
上面的代码,当我运行时只打印时间而不是40次。帮助我....
答案 0 :(得分:0)
我想出了这个问题的解决方案。对于那些不想使用fluentwa轮询的人,可以使用我发布的这个课程。而且我没有使用THREAD或SLEEPER就实现了这一目标。该课程将视为POLLING替代方案。
public class PollingClass {
private final long MAX_WAIT_IN_SECOND = 60;
private long MAX_TIME_COUNT_IN_MILLISECOND = System.currentTimeMillis()+MAX_WAIT_IN_SECOND*1000;
private long MIN_TIME_COUNT_IN_SECOND = 10;
private static int i = 0;
WebDriver driver;
WebElement element;
public PollingClass(WebDriver driver){
this.driver = driver;
}
public WebElement getPolling(String path){
try{
while(System.currentTimeMillis() < MAX_TIME_COUNT_IN_MILLISECOND)
{
try{
element = new WebDriverWait(driver, MIN_TIME_COUNT_IN_SECOND).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(path)));
if(element.isDisplayed()){
System.out.println("element"+ ++i+" is displayed ");
break;
}
}catch(NoSuchElementException e){
// e.printStackTrace();
continue;
}
}
}catch(NoSuchElementException|TimeoutException e){
// e.printStackTrace();
}
return element;
}
}