I want to implement TimerTask in TestNG but it is failed. Have a look at my code
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);
}
}
}
The upper class is my Task that I want to implement
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);
}
}
Output:// 1 : Hi
The above code when I am running printing only time instead of 40 times. Help me....
I come up with the solution for this question. For those who don't want to use polling of fluentwait can use this class that I am posting. And I've achieved this without using THREAD or SLEEPER. This class will treat as POLLING alternative.
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.