Java TimerTask不';不能使用testng
我想在TestNG中实现TimerTask,但失败了。 看看我的代码Java TimerTask不';不能使用testng,java,selenium,webdriver,testng,Java,Selenium,Webdriver,Testng,我想在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");
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:Hi
上面的代码在我运行时只打印一次,而不是40次。帮帮我……我想出了这个问题的答案。对于那些不想使用fluentwait轮询的人,可以使用我发布的这个类。我在没有使用线程或睡眠器的情况下实现了这一点。此类将被视为轮询备选方案
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;
}
}
公共类轮询类{
私人最终最大等待时间(秒)为60;
private long MAX_TIME_COUNT_IN_毫秒=System.currentTimeMillis()+MAX_WAIT_IN_毫秒*1000;
私有长最小时间计数秒=10;
私有静态int i=0;
网络驱动程序;
网络元素;
公共轮询类(WebDriver){
this.driver=driver;
}
公共WebElement getPolling(字符串路径){
试一试{
while(System.currentTimeMillis()
1TimerTask
已被ScheduledExecutorService
取代;2.你到底为什么要这么做?TestNG的@Test
有一个超时
参数,实际上我想建立自己的轮询方法。在我的代码中,我试图通过TimerTask实现这一点……有其他选择吗?